ADO.NET中的多数据表操作之读取

开发者在线 Builder.com.cn 更新时间:2007-11-13作者:郑佐 来源:csdn

叉开话题提一下,Data Access Application Block 2.0中的SqlHelper.FillDataset这个方法超过两个表的填充时会出现错误,其实里面的逻辑是错的,只不过两个表的时候刚好凑巧,下面是从里面截的代码:

private static void FillDataset(SqlConnection connection, SqlTransaction transaction, CommandType commandType, string commandText, DataSet dataSet, string[] tableNames,
params SqlParameter[] commandParameters)

{
 if( connection == null ) throw new ArgumentNullException( "connection" );
 if( dataSet == null ) throw new ArgumentNullException( "dataSet" );
 SqlCommand command = new SqlCommand();
 bool mustCloseConnection = false;
 PrepareCommand(command, connection, transaction, commandType, commandText, commandParameters, out mustCloseConnection );

 using( SqlDataAdapter dataAdapter = new SqlDataAdapter(command) )
 {
  if (tableNames != null && tableNames.Length > 0)
  {
   string tableName = "Table";
   for (int index=0; index < tableNames.Length; index++)
   {
    if( tableNames[index] == null || tableNames[index].Length == 0 )
     throw new ArgumentException( "The tableNames parameter must contain a list of tables, a value was  provided as null or empty string.", "tableNames" );

     tableName += (index + 1).ToString();//这里出现错误

   }
  }
  dataAdapter.Fill(dataSet);
  command.Parameters.Clear();
 }
 if( mustCloseConnection )
  connection.Close();
}

  这里把tableName += (index + 1).ToString();修改成

dataAdapter.TableMappings.Add((index>0) (tableName+index.ToString()):tableName, tableNames[index]);就能解决问题。

  接下来看看窗体程序的代码:

public class Form1 : System.Windows.Forms.Form
{
 private DataAccess _dataAccess;
 private DatasetOrders _ds;
 //……
 //构造函数
 public Form1()
 {
  InitializeComponent();
  _dataAccess = new DataAccess();
  _ds = new DatasetOrders();
  _ds.EnforceConstraints = false; //关闭约束检查,提高数据填充效率
  this.dataGridCustomers.DataSource = _ds;
  this.dataGridCustomers.DataMember = _ds.Customers.TableName;
  this.dataGridOrders.DataSource = _ds;
  this.dataGridOrders.DataMember = _ds.Customers.TableName+"."+_ds.Customers.ChildRelations[0].RelationName;
  this.dataGridOrderDetails.DataSource = _ds;
  this.dataGridOrderDetails.DataMember =_ds.Customers.TableName+"."+
    _ds.Customers.ChildRelations[0].RelationName+"."+
    _ds.Orders.ChildRelations[0].RelationName;
 }

  对于上面的三个表的动态关联,你也可以使用SetDataBinding方法来完成数据的动态绑定,而不是分别指定DataGride的DataSource和DataMemger属性。

this.dataGridCustomers.SetDataBinding(_ds,_ds.Customers.TableName);

this.dataGridOrders.SetDataBinding(_ds,_ds.Customers.TableName+"."+_ds.Customers.ChildRelations[0].RelationName);

this.dataGridOrderDetails.SetDataBinding(_ds,_ds.Customers.TableName+"."+
  _ds.Customers.ChildRelations[0].RelationName+"."+_ds.Orders.ChildRelations[0].RelationName);
}

  数据填充事件处理如下:

private void buttonFillData_Click(object sender, System.EventArgs e)

  执行上面的事件处理函数我们会看到数据显示到对应的DataGrid上,如(图2-1)所示。

  如果使用数据读取器获取多表纪录下面是实现的一种方式(参考):

SqlCommand comm = new SqlCommand("GetCustomerOrdersInfo",_conn);
comm.CommandType = CommandType.StoredProcedure;
_conn.Open();
SqlDataReader reader = comm.ExecuteReader();
do

}while(reader.NextResult());
Console.ReadLine();
_conn.Close();

查看本文来源

用户评论

  • 用户名
  • 评论内容