断开式操作数据库使用DataSet和SqlDataAdapter,通过adapter的fill方法将数据库中的
数据填充到数据集中,通过update方法将数据集中的数据更新到数据库中。
1 对于Fill(DataSet ds,String dtName),将数据填充到数据集ds的表dtName中,dtName
是数据集中的表名,与数据库中表名无关,但是通常相同。
2 对于Update(DataTable dt)更新某个数据表需要三步:
(1)
添加一行:
DataRow row = ds.Tables["studentInfo"].NewRow();//(1)新建一行;(2)指定数据
ds.Tables[0].Rows.Add(row);//(3)把这一行添加到dataset
删除一行:
ds.Tables[0].Rows[2].Delete();
修改一行:
DataRow row2 = ds.Tables[0].Rows[1];
row2["sAddress"] = "日本";
(2)类似于下面创建命令对象:
SqlCommand deleteCmd = new SqlCommand("delete studentInfo where sNo = @No", conn); deleteCmd.Parameters.Add("@No", SqlDbType.Char, 6, "sNo"); ada.DeleteCommand = deleteCmd;
(3)调用更新:
ada.Update(ds.Tables["studentInfo"]);//调用Update
下面是一个完整的实例:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace 断开时访问数据库 { class Program { public static void ShowDataSet(string title, DataSet ds) { Console.WriteLine(title); foreach (DataRow row in ds.Tables[0].Rows) { foreach (DataColumn col in ds.Tables[0].Columns) Console.Write(" {0}",row[col.ColumnName]); Console.WriteLine(string.Empty); } } static void Main(string[] args) { DataSet ds = new DataSet(); string connectionString = "server = .;Initial catalog= students;Integrated Security = SSPI"; try { SqlConnection conn = new SqlConnection(connectionString); SqlDataAdapter ada = new SqlDataAdapter("select * from studentInfo", conn); ada.Fill(ds, "studentInfo"); ShowDataSet("操作前",ds); DataRow row = ds.Tables["studentInfo"].NewRow();//(1)新建一行 //(2)下面指定 这一行 各个字段的值 row["sName"] = "山本五十六"; row["sNo"] = "ST2009"; row["sAge"] = 23; row["sTime"] = "2005 - 6 - 7"; row["sAddress"] = "通过村"; ds.Tables[0].Rows.Add(row);//(3)把这一行添加到dataset //(4)下面是插入命令 SqlCommand insertCmd = new SqlCommand("insert into studentInfo values(@Name,@No,@Age,@Time,@Address)", conn); //(5)下面添加命令的参数 insertCmd.Parameters.Add("@Name", SqlDbType.VarChar, 20, "sName");//参数与数据源关联 insertCmd.Parameters.Add("@No", SqlDbType.Char, 6, "sNo"); insertCmd.Parameters.Add("@Age", SqlDbType.Int, 4, "sAge"); insertCmd.Parameters.Add("@Time", SqlDbType.SmallDateTime, 4, "sTime"); insertCmd.Parameters.Add("@Address", SqlDbType.VarChar, 50, "sAddress"); //(6)指定InsertCommand ada.InsertCommand = insertCmd;//将插入命令语句赋值给adapter //下面是删除一行对应的程序段 ds.Tables[0].Rows[2].Delete(); SqlCommand deleteCmd = new SqlCommand("delete studentInfo where sNo = @No", conn); deleteCmd.Parameters.Add("@No", SqlDbType.Char, 6, "sNo"); ada.DeleteCommand = deleteCmd; DataRow row2 = ds.Tables[0].Rows[1]; row2["sAddress"] = "日本"; SqlCommand updateCmd = new SqlCommand("update studentInfo set sAddress = @Address where sName = @Name", conn); updateCmd.Parameters.Add("@Address", SqlDbType.VarChar, 50, "sAddress"); updateCmd.Parameters.Add("@Name", SqlDbType.VarChar, 20, "sName"); ada.UpdateCommand = updateCmd; ada.Update(ds.Tables["studentInfo"]);//(7)调用Update ds.Clear(); ada.Fill(ds,"studentInfo"); ShowDataSet("操作后",ds); } catch(Exception ex) { Console.WriteLine("错:" + ex.Message); } Console.Read(); } } }