工資管理系統c#與SQL Server
表的建立:
Create table Usemen
(
使用者 nchar(10)primary key,
密碼 nchar(10)not null,
性別 nchar(3)not null,
年齡 int notnull,
員工號 nchar(10)not null,
職位 nchar(10)not null
);
Create table Salary
{
基本工資 int notnull,
津貼 int,
總工資 int notnull,
員工號 nchar(10)not null
};
Create teable Work
{
職位 nchar(10)not null,
基本工資 int notnull
};
Create table 價值表
{
職位 nchar(10)not null,
加班費/小時 int not null,
誤工費/小時 int not null
};
Create table 津貼
{
員工號 nchar(10)primary key;
加班時間 int,
誤工時間 int,
加班費 int,
誤工費 int,
獎金 int,
津貼 int
};
登陸
功能選擇介面
查詢任意一人的工資
新增員工
刪除員工
修改員工資訊
資料匯出
將所有人的工資資訊匯出到excel表,這裡運用的是excel2016版
附錄
//工資查詢
namespace 工資管理系統{
public partial class Form3 : Form{
SqlConnection conn = newSqlConnection("server=.;uid=sa;pwd=981210;database=工資資料庫");
public Form3(){
InitializeComponent();
}
private void Form3_Load(object sender,EventArgs e){
this.salaryTableAdapter2.Fill(this.工資資料庫DataSet6.Salary);
}
private voidtextBox1_TextChanged(object sender, EventArgs e){
try{
String strsql = "select *from Salary where 員工號='" + textBox1.Text + "'";
SqlCommand cmd = newSqlCommand(strsql, conn);
SqlDataAdapter da = newSqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource =ds.Tables[0];
}
catch (Exception ce){
MessageBox.Show(ce.Message);
}
finally{ }
}
}
}
//新增員工
namespace 工資管理系統{
public partial class Form5 : Form{
SqlConnection conn =new SqlConnection("server=.;uid=sa;pwd=981210;database=工資資料庫");
public Form5(){
InitializeComponent();
}
private void button1_Click(objectsender, EventArgs e){
try{
if ((textBox1.Text == "") || (textBox2.Text == " ") || (textBox3.Text == " ")|| (textBox4.Text == " ") || (textBox5.Text == "")||(textBox6.Text == " ")) {
MessageBox.Show("介面中的所有屬性值不能為空!");
}
else {
string number = textBox1.Text.Trim();
string name = textBox2.Text.Trim();
string age = textBox3.Text.Trim();
stringsex = textBox4.Text.Trim();
string work = textBox5.Text.Trim();
string id = textBox6.Text.Trim();
string password = number;
String strsql = "select * from Work where 職位='" + work + "'";
SqlCommand cmd = new SqlCommand(strsql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
string basedsalary = ds.Tables[0].Rows[0][1].ToString();
strsql = "insert into Usemen(使用者,密碼,員工號,年齡,性別,職位,系統身份)values('" + name + "','"+password+"','" + number +"','"+age+"','"+sex+"','"+work+"','"+id+"')"
+"insert into 津貼(員工號) values('" + number + "')"
+ "insert into Salary(基本工資,總工資,員工號)values('" + basedsalary + "','" + basedsalary + "','"+ number + "')";
cmd= new SqlCommand(strsql, conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("新增成功!");
conn.Close();
}
}
catch (Exception e2){
MessageBox.Show(e2.Message);
}
}
}
}
//修改員工資訊
namespace 工資管理系統{
public partial class Form11 : Form{
SqlConnection conn = newSqlConnection("server=.;uid=sa;pwd=981210;database=工資資料庫");
string sex, age, work,name11;
String strsql;
SqlCommand cmd;
SqlDataAdapter da;
DataSet ds;
public Form11(){
InitializeComponent();
}
private void button1_Click(objectsender, EventArgs e){
try{
age = textBox3.Text.Trim();
work = textBox4.Text.Trim();
String strsql = "select *from Work where 職位='" + work + "'";
SqlCommand cmd = newSqlCommand(strsql, conn);
SqlDataAdapter da = newSqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
int basedsalary =int.Parse(ds.Tables[0].Rows[0][1].ToString());
strsql = "select * from 津貼 where 員工號=(select 員工號 fromUsemen where 使用者='" + name11 + "')";
cmd = new SqlCommand(strsql,conn);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
int 津貼 = int.Parse(ds.Tables[0].Rows[0][5].ToString());
int 工資 = basedsalary + 津貼;
strsql = "update Usemen set 年齡='" + age + "',職位='" +work + "' where 使用者='" + name11 + "'"+"update Salary set 基本工資='" + basedsalary + "',總工資='" + 工資 + "'where 員工號=(select 員工號 fromUsemen where 使用者='" + name11 + "')";
cmd = new SqlCommand(strsql,conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("修改成功!");
conn.Close();
}
catch (Exception ce){
MessageBox.Show(ce.Message);
}
}
private void button3_Click(objectsender, EventArgs e){
strsql = "select * from Usemenwhere 使用者='" + textBox1.Text + "'";
try{
conn.Open();
SqlCommand com = newSqlCommand(strsql, conn);
name11 = textBox1.Text.Trim();
cmd = new SqlCommand(strsql,conn);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
sex =ds.Tables[0].Rows[0][2].ToString();
age =ds.Tables[0].Rows[0][3].ToString();
work =ds.Tables[0].Rows[0][5].ToString();
this.textBox2.Text = sex;
this.textBox3.Text = age;
this.textBox4.Text = work;
}
catch (Exception ce){
MessageBox.Show(ce.Message);
}
conn.Close();
}
}
}
//刪除員工記錄
namespace 工資管理系統{
public partial class Form6 : Form{
SqlConnection conn = newSqlConnection("server=.;uid=sa;pwd=981210;database=工資資料庫");
public Form6(){
InitializeComponent();
}
private void button1_Click(objectsender, EventArgs e){
try{
String strsql = "deletefrom Salary where 員工號='" + this.textBox1.Text +"'"
+ "deletefrom 津貼 where 員工號='"+ this.textBox1.Text + "'"
+"deletefrom Usemen where 員工號='" + this.textBox1.Text +"'";
SqlCommand cmd = newSqlCommand(strsql, conn);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("刪除成功!");
conn.Close();
}
catch (Exception ce){
MessageBox.Show(ce.Message);
}
finally{}
}
}
}
//資料匯出
namespace 工資管理系統{
public partial class Form13 : Form{
SqlConnection conn = newSqlConnection("server=.;uid=sa;pwd=981210;database=工資資料庫");
public Form13(){
InitializeComponent();
try{
String strsql = "select 使用者,基本工資,總工資,津貼.* from Usemen,Salary,津貼 where Usemen.員工號=Salary.員工號 and Salary.員工號=津貼.員工號";
SqlCommand cmd = newSqlCommand(strsql, conn);
SqlDataAdapter da = newSqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
catch (Exception ce){
MessageBox.Show(ce.Message);
}
finally{ }
}
private void button1_Click(objectsender, EventArgs e){
if (ExportDataGridview(dataGridView1,true)==true)
MessageBox.Show("匯出成功,請記得儲存!");
else
MessageBox.Show("匯出不成功,請檢查是否有錯!");
}
public boolExportDataGridview(DataGridView gridview, bool isshowExcle){
if (gridview.Rows.Count == 0)
return false;
Excel.Application excel = newExcel.Application();
excel.Application.Workbooks.Add(true);
excel.Visible = isshowExcle;
for (int i = 0; i <gridview.ColumnCount; i++){
excel.Cells[1, i + 1] =gridview.Columns[i].HeaderText;
}
for (int i = 0; i <gridview.RowCount-1; i++){
for (int j = 0; j <gridview.ColumnCount; j++){
if (gridview[j, i].ValueType== typeof(string)){
excel.Cells[i + 2, j +1] = "'" + gridview[j, i].Value.ToString();
}
else{
excel.Cells[i + 2, j +1] = gridview[j, i].Value.ToString();
}
}
}
return true;
}
}
}