源代码:(这些代码是包含在一个对数据库进行操作的类中的,这是其中的一段)如下:
/*操作脱机数据库(创建了该类的实例时直接用)*/
//根据输入的SQL语句检索数据库数据
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new SqlConnection(connectionstring);
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
this.ds.Clear();
this.da.Fill(ds,tempTableName);
//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
return ds;
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changeDataSet, string tableName)
{
this.myConnection = new SqlConnection(connectionstring);
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changeDataSet,tableName);
return changeDataSet; //返回了更新的数据表
}
问题:
1.SqlCommandBuilder的具体作用是什么,在什么时候使用?(在原代码中标记为红色的语句中)
2.对“脱机数据库”进行操作与直接对数据库进行操作在处理方法上有什么不同,都有哪些处理方法?
非常谢谢!!
/*操作脱机数据库(创建了该类的实例时直接用)*/
//根据输入的SQL语句检索数据库数据
public DataSet SelectDataBase(string tempStrSQL, string tempTableName)
{
this.strSQL = tempStrSQL;
this.myConnection = new SqlConnection(connectionstring);
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
this.ds.Clear();
this.da.Fill(ds,tempTableName);
//返回填充了数据的DataSet,其中数据表以tempTableName给出的字符串命名
return ds;
}
//数据库数据更新(传DataSet和DataTable的对象)
public DataSet UpdateDataBase(DataSet changeDataSet, string tableName)
{
this.myConnection = new SqlConnection(connectionstring);
this.da = new SqlDataAdapter(this.strSQL,this.myConnection);
this.sqlCmdBld = new SqlCommandBuilder(da);
this.da.Update(changeDataSet,tableName);
return changeDataSet; //返回了更新的数据表
}
问题:
1.SqlCommandBuilder的具体作用是什么,在什么时候使用?(在原代码中标记为红色的语句中)
2.对“脱机数据库”进行操作与直接对数据库进行操作在处理方法上有什么不同,都有哪些处理方法?
非常谢谢!!
每当设置了 DataAdapter 属性,SqlCommandBuilder 就将其本身注册为 RowUpdating 事件的侦听器。一次只能将一个 SqlDataAdapter 与一个 SqlCommandBuilder 对象(或相反)互相关联。
为了生成 INSERT、UPDATE 或 DELETE 语句,SqlCommandBuilder 会自动使用 SelectCommand 属性来检索所需的元数据集。如果在检索到元数据后(例如在第一次更新后)更改 SelectCommand,则应调用 RefreshSchema 方法来更新元数据。
SelectCommand 还必须至少返回一个主键列或唯一的列。如果什么都没有返回,就会产生 InvalidOperation 异常,不生成命令。
SqlCommandBuilder 还使用由 SelectCommand 引用的 Connection、CommandTimeout 和 Transaction 属性。如果修改了这些属性中的一个或多个,或者替换了 SelectCommand 本身,用户则应调用 RefreshSchema。否则,InsertCommand、UpdateCommand 和 DeleteCommand 属性都保留它们以前的值。
如果调用 Dispose,则会解除 SqlCommandBuilder 与 SqlDataAdapter 的关联,并且不再使用生成的命令