I think you should use DataAdapter, this is example in MSDN:
public static void CreateSqlDataAdapter() 
{
    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");
    SqlDataAdapter custDA = new SqlDataAdapter();
    custDA.MissingSchemaAction = MissingSchemaAction.AddWithKey;
   
    custDA.SelectCommand = new SqlCommand("SELECT CustomerID, CompanyName FROM CUSTOMERS", nwindConn);
    custDA.InsertCommand = new SqlCommand("INSERT INTO Customers (CustomerID, CompanyName) " +
                                            "VALUES (@CustomerID, @CompanyName)", nwindConn);
    custDA.UpdateCommand = new SqlCommand("UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
                                            "WHERE CustomerID = @oldCustomerID", nwindConn);
    custDA.DeleteCommand = new SqlCommand("DELETE FROM Customers WHERE CustomerID = @CustomerID", nwindConn);    custDA.InsertCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID");
    custDA.InsertCommand.Parameters.Add("@CompanyName", SqlDbType.VarChar, 40, "CompanyName");
 
    custDA.UpdateCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID");
    custDA.UpdateCommand.Parameters.Add("@CompanyName", SqlDbType.VarChar, 40, "CompanyName");
    custDA.UpdateCommand.Parameters.Add("@oldCustomerID", SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;    custDA.DeleteCommand.Parameters.Add("@CustomerID", SqlDbType.Char, 5, "CustomerID").SourceVersion = DataRowVersion.Original;
}

解决方案 »

  1.   

    chechy(我爱洁洁):
    噢!!
    我只是想连接数据库并简洁写,没想到用dataAdapter
    在程序里怎么正确应用dataAdapter?!
    初学者,望指教!
    我觉得用string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ;好麻烦而且还会错!!
      

  2.   

    xxqqhhnn(xxqqhh) :
    你出错是在哪一行?还有你的连接字符串怎么这么怪,八成有问题?
    开发中用数据库使用SQL语句比较多,因为可以在数据里调试它,充分利用数据库的功能
      

  3.   

    代码中看不到你对“xxqqhh”的声明呀?
      

  4.   

    我以为放xxqqhh的地方是放数据库名字的:(
    DataRow anyRow = xxqqhh.studentmessage.NewRow();!!!!!!!xxqqhh是不是要放数据库名??
    不知道要在那里放什么才对。
      

  5.   

    我建议你看看MSDN中的文章,我也是这么学来的:
    ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconpopulatingdatasetfromdataadapter.htm
    从 DataAdapter 填充 DataSet  
    ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconupdatingdatabasewithdataadapterdataset.htm
    使用 DataAdapter 和 DataSet 更新数据库
      

  6.   

    当然,这些都是啊。MSDN的文章还是不错的,不能只看Class Library Reference。