connStr="data source=.; database=test;user id=da;password=sa" SqlConnection conn=new SqlConnection(connStr); SqlCommand comm=new SqlCommand(); comm.Connection=conn; comm.CommandText="update P_Product set usernam='dd' where id=2"; conn.Open(); int rows=comm.ExecuteNonQuery(); myConnection.Close(); *********************** SqlConnection conn=new Sqlconnection(conString) Sqlcommand comm=new Sqlcommand(update_str); comm.Connection=conn; conn.Open(); int results=comm.ExecuteNonQuery(); conn.Close(); ********************** Sqlcommand comm=new Sqlcommand(update_str,conn); ******************* string conString = "data source=127.0.0.1;Database=codematic;user id=sa; password="; SqlConnection myConnection = new SqlConnection(conString ); string strSql = "update P_Product set Name='电脑4' where Id=52"; string strSql2 = "update P_Product set Name='数码4' where Id=53"; myConnection.Open(); SqlTransaction myTrans = myConnection.BeginTransaction(); SqlCommand myCommand = new SqlCommand(strSql, myConnection, myTrans); try { int rows = myCommand.ExecuteNonQuery(); myCommand.CommandText = strSql2; rows = myCommand.ExecuteNonQuery(); myTrans.Commit(); myConnection.Close(); } catch { myTrans.Rollback(); }