图01:DataSet对象的结构模型图 |
SqlConnection sqlConnection1 = new SqlConnection ( "Data Source=localhost ;Integrated Security=SSPI ;Initial Catalog=Northwind" ) ; //创建数据连接 SqlCommand selectCMD = new SqlCommand ( "SELECT CustomerID , CompanyName FROM Customers" , sqlConnection1 ) ; //创建并初始化SqlCommand对象 SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter ( ) ; custDA.SelectCommand = selectCMD ; sqlConnection.Open ( ) ; //创建SqlDataAdapter对象,并根据SelectCommand属性检索数据 DataSet dsDataSet1 = new DataSet ( ) ; sqlDataAdapter1.Fill ( dsDataSet1 , "Customers" ) ; //使用SqlDataAdapter的Fill方法填充DataSet sqlConnection.Close ( ) ; //关闭数据连接 |
SqlCommandBuilder sqlCommandBuilder1 = new SqlCommandBuilder ( sqlDataAdapter1 ) ; //以sqlDataAdapter1为参数来初始化SqlCommandBuilder实例 dsDataSet1.Tables["Customers"].Rows[0].Delete ( ) ; //删除DataSet中删除数据表Customers中第一行数据 sqlDataAdapter1.Update ( dsDataSet1 ,"Customers" ) ; //调用Update方法,以DataSet中的数据更新从数据库 dsDataSet1.Tables["Customers"].AcceptChanges ( ) ; |
欢迎光临 JBTALKS.CC (https://jbtalks.my/) | Powered by Discuz! X2.5 |