这是我写的一些代码,
这样似乎无法更新到数据库,是不是要先new一个table,然后更新?
DataSet ds =getDS();
try
{
SqlDataAdapter sqlSda = new SqlDataAdapter("select * from AddressBook", sqlCon);
sqlSda.InsertCommand = new SqlCommand("insert into AddressBook values(@Name,@Address,@Mobile,@Profession,@Email)", sqlCon);
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Name",SqlDbType.VarChar, 20, "Name"));
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar, 50, "Address"));
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Mobile", SqlDbType.VarChar, 20, "Mobile"));
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Profession", SqlDbType.VarChar, 20, "Profession"));
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar, 30, "Email"));
//SqlCommandBuilder sqlScb = new SqlCommandBuilder(sqlSda);
sqlCon.Open();
sqlSda.Update(ds.Tables["AddressBook"]);
MessageBox.Show("保存成功!");向大家指教
这样似乎无法更新到数据库,是不是要先new一个table,然后更新?
DataSet ds =getDS();
try
{
SqlDataAdapter sqlSda = new SqlDataAdapter("select * from AddressBook", sqlCon);
sqlSda.InsertCommand = new SqlCommand("insert into AddressBook values(@Name,@Address,@Mobile,@Profession,@Email)", sqlCon);
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Name",SqlDbType.VarChar, 20, "Name"));
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar, 50, "Address"));
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Mobile", SqlDbType.VarChar, 20, "Mobile"));
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Profession", SqlDbType.VarChar, 20, "Profession"));
sqlSda.InsertCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.VarChar, 30, "Email"));
//SqlCommandBuilder sqlScb = new SqlCommandBuilder(sqlSda);
sqlCon.Open();
sqlSda.Update(ds.Tables["AddressBook"]);
MessageBox.Show("保存成功!");向大家指教
你上面那个只是更新了内存中的表,实际上并没有更新数据库。期待其它回答。