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;
}
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;
}
解决方案 »
- 如何通过Message-ID打开相应的email
- progressBar1显示datagridview读取数据的进度
- 100分 求答 编译时间
- 程序出错,BindingSource与DataTable
- .NetRemote和其它几种技术的比较
- 如何知道某固定ip机器是否联网?
- 难题!用 .net怎么做像金山词霸一样的从页面抓信息(鼠标指处)
- 哪位大侠做过:如何实现COM中void*类型指针内容到C#中byte[ ]类型的转换?急。。。
- c# 文件夹监控 多线程发送流向 出错 帮忙看看
- 请问怎么才能使窗口只开一个
- 我该选择哪个东东?
- bigrongshu(沧海破浪),你给的ebook已收到,请拿分
噢!!
我只是想连接数据库并简洁写,没想到用dataAdapter
在程序里怎么正确应用dataAdapter?!
初学者,望指教!
我觉得用string strInsert = " INSERT INTO person ( id , xm , xb , nl , zip ) VALUES ( " ;好麻烦而且还会错!!
你出错是在哪一行?还有你的连接字符串怎么这么怪,八成有问题?
开发中用数据库使用SQL语句比较多,因为可以在数据里调试它,充分利用数据库的功能
DataRow anyRow = xxqqhh.studentmessage.NewRow();!!!!!!!xxqqhh是不是要放数据库名??
不知道要在那里放什么才对。
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 更新数据库