private void button1_Click(object sender, EventArgs e)
{
string sql = @"数据库连接字符串";
string insert = "insert into db_Worker values('" + txtWorkerID.Text + "','" + txtWorkerName.Text + "','" + txtWorkerCall.Text + "','" + txtWorkerSex.Text + "','" + txtWorkerType.Text + "','" +textBox1.Text + "','" + txtWorkerAd.Text + "')";//插入数据
SqlConnection con = new SqlConnection(sql);
SqlCommand com = new SqlCommand();
com.CommandText = insert;
com.Connection = con;
con.Open();
int i = com.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("添加成功");
}
con.Close();
con.Dispose();
//刷新表格内容(datagridview控件)
string select = "select * from db_Worker";
SqlConnection con1 = new SqlConnection(sql);
SqlDataAdapter sda = new SqlDataAdapter(select, con1);
DataSet ds = new DataSet();
sda.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];
}
转载于:https://blog.51cto.com/kiakel/1425508