淘宝评论给什么:断开数据库的连接后,SqlDataAdapter还能与数据库通信?

来源:百度文库 编辑:神马品牌网 时间:2024/05/07 08:43:59
string strConn="Initial Catalog=Northwind;Data Source=(local);user ID=sa;password=";
conn.Open();

SqlCommand selcmd = new SqlCommand();
selcmd.Connection = conn;
selcmd.CommandText =
"SELECT * FROM Region";

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = selcmd;

DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();

// 利用SqlCommandBuilder自动建立
// Insert, Update 和 Delete Command
SqlCommandBuilder cb = new SqlCommandBuilder(da);

// 添加数据
Console.WriteLine("输入一个新的 RegionDescription: ");
string newRegion = Console.ReadLine();

// 把新数据添加到 DataSet
DataRow r = ds.Tables[0].NewRow();
r["RegionID"] = 5;
r["RegionDescription"] = newRegion;
ds.Tables[0].Rows.Add(r);
// 更新数据库
da.Update(ds);

数据适配器会自动完成将ds更新到db的过程

真的更新数据库了么?你更新的只是ds啊?

这个ds里放的是刚才数据库的特定部分的拷贝

你再次打开数据库有你刚才写的么?