make sure you have a primary key for your table, and the key is in the SELECT list, also use a SqlCommandBuilder

解决方案 »

  1.   

    public static DataSet SelectSqlSrvRows(string myConnection, string mySelectQuery, string myTableName)
    {
       SqlConnection myConn = new SqlConnection(myConnection);
       SqlDataAdapter myDataAdapter = new SqlDataAdapter();
       myDataAdapter.SelectCommand = new SqlCommand(mySelectQuery, myConn);
       SqlCommandBuilder cb = new SqlCommandBuilder(myDataAdapter);   myConn.Open();   DataSet ds = new DataSet();
       myDataAdapter.Fill(ds, myTableName);   //code to modify data in DataSet here   //Without the SqlCommandBuilder this line would fail
       myDataAdapter.Update(ds, myTableName);   myConn.Close();   return ds;
    }