日期:2014-05-19  浏览次数:20899 次

关于ADO.NET的

private   System.Data.SqlClient.SqlConnection   sqlConnection1;
private   System.Data.SqlClient.SqlDataAdapter   sqlDataAdapterReader;
private   System.Data.SqlClient.SqlDataAdapter   sqlDataAdapterBook;
private   System.Data.SqlClient.SqlDataAdapter   sqlDataAdapterPublish;

现每个SqlDataAdapter类分别对应同一数据库的表:Reader,Book,Publish
我是用VS.NET工具生成SqlDataAdapter的,设置各个SqlDataAdapter类对象的查询时,
都是SELECT   *   FROM   对应的表名,这样工具就生成了四个:
private   System.Data.SqlClient.SqlCommand   sqlSelectCommand3;
private   System.Data.SqlClient.SqlCommand   sqlInsertCommand3;
private   System.Data.SqlClient.SqlCommand   sqlUpdateCommand3;
private   System.Data.SqlClient.SqlCommand   sqlDeleteCommand3;
private   System.Data.SqlClient.SqlCommand   sqlSelectCommand1;
private   System.Data.SqlClient.SqlCommand   sqlInsertCommand1;
private   System.Data.SqlClient.SqlCommand   sqlUpdateCommand1;
private   System.Data.SqlClient.SqlCommand   sqlDeleteCommand1;
private   System.Data.SqlClient.SqlCommand   sqlSelectCommand2;
private   System.Data.SqlClient.SqlCommand   sqlInsertCommand2;
private   System.Data.SqlClient.SqlCommand   sqlUpdateCommand2;
private   System.Data.SqlClient.SqlCommand   sqlDeleteCommand2;
private   System.Data.SqlClient.SqlCommand   sqlSelectCommand4;
private   System.Data.SqlClient.SqlCommand   sqlInsertCommand4;


private   void   btnQuery_Click(object   sender,   System.EventArgs   e)//一个查询按钮
{
SqlString= "select   *   from "+this.comboDataTable.Text+ "where ";
SqlString=SqlString+this.comboDataItem.Text+this.comboCondition.Text+this.textValue.Text;     //生成查询语句
switch(this.comboDataTable.SelectedText)//请大家看清楚这里啦,根据条件,只能有一种情况出现的
{
case   0:
this.sqlSelectCommand1.CommandText=SqlString;
break;
case   1:
this.sqlSelectCommand2.CommandText=SqlString;
break;
case   2:
this.sqlSelectCommand3.CommandText=SqlString;
break;
case   3:
this.sqlSelectCommand4.CommandText=SqlString;
break;
}
try
{
this.LoadDataSet();
this.dataGrid1.DataSource=this.objDataSetQuery;
this.dataGrid1.DataMember=this.comboDataTable.Text.Trim();
}

catch(System.Exception   e)
{
this.ErrorHandle(e);
}
}


private   void   LoadDataSet()
{
DataSetBookQuery   objDataSetTemp=new   DataSetQuery();//这里生成一个DataSet数据集
try
{
this.FillDataSet(objDataSetTemp);

}
catch   (System.Exception   E)  
{
//   在此处添加错误处理代码。
this.ErrorHandle(E);
}
try
{
this.objDataSetQuery.Clear();
this.objDataSetQuery.Merge(objDataSetTemp);
}
catch(System.Exception   E)  
{
//   在此处添加错误处理代码。
this.ErrorHandle(E);
}

}

private   void   FillDataSet(DataSetBookQuery   dataset)//参数为DataSet数据集
{
dataset.EnforceConstraints=false;
try                                 {
this.sqlConnection1.Open();
this.sqlDataAdapterReader.Fill(dataset);
this.sqlDataAdapterBook.Fill(dataset);
this.sqlDataAdapterPublish.Fill(dataset);