private void buttonUpdate_Click(object sender, EventArgs e)
{
string source = "server=jiahui3722;integrated security=sspi;database=HeroSystem";
SqlConnection conn=new SqlConnection(source);
SqlCommand aCommand = new SqlCommand("empInsert", conn);
aCommand.CommandType = CommandType.StoredProcedure;
aCommand.Parameters.Add(new SqlParameter("@emp_ID",SqlDbType.NChar,5,"emp_ID"));
aCommand.Parameters.Add(new SqlParameter("@emp_name",SqlDbType.NVarChar,20,"emp_name"));
aCommand.UpdatedRowSource = UpdateRowSource.OutputParameters; string select = "SELECT * FROM employee";
SqlDataAdapter da = new SqlDataAdapter(select, conn);
DataSet ds=new DataSet();
da.Fill(ds, "employee");
DataRow r = ds.Tables["employee"].NewRow();
r["emp_ID"] = textID.Text;
r["emp_name"] = textName.Text;
ds.Tables["employee"].Rows.Add(r); da.InsertCommand = aCommand;
da.Update(ds, "employee");
}
在数据库新加一行要这么多代码啊?如果这张表有许多列的话岂不要一个一个sqlparameter的Add, 再一项一项的赋值?
那样就简单多了。另外,你这个是调用存储过程哦。
SqlConnection conn = new SqlConnection(source);
SqlCommand aCommand = new SqlCommand("empInsert", conn);
string select = "SELECT * FROM employee";
SqlDataAdapter da = new SqlDataAdapter(select, conn);
da.SelectCommand = new SqlCommand(select, conn);
DataSet ds = new DataSet();
da.Fill(ds, "employee");
DataTable mytable = mydataset .Tables ["employee"];
DataRow r = mytable.NewRow();
r["emp_ID"] = textID.Text;
r["emp_name"] = textName.Text;
mytable.Rows.Add(r );
SqlCommandBuilder mycb = new SqlCommandBuilder(da );
da.Update(ds, "employee");