图1-1 |
图2-1 |
CREATE PROCEDURE GetCustomerOrdersInfo AS SELECT CustomerID,CompanyName,ContactName FROM Customers WHERE CustomerID LIKE 'A%' SELECT OrderID,OrderDate,CustomerID FROM Orders WHERE CustomerID IN (SELECT CustomerID FROM Customers WHERE CustomerID LIKE 'A%') SELECT OrderID,ProductID,UnitPrice,Quantity,Discount FROM [Order Details] WHERE OrderID IN (SELECT OrderID FROM Orders WHERE CustomerID IN (SELECT CustomerID FROM Customers WHERE CustomerID LIKE 'A%')) GO |
using System; using System.Data; using System.Data.SqlClient; using Microsoft.ApplicationBlocks.Data; namespace WinformTest { public class DataAccess { private string _connstring = "data source=(local);initial catalog=Northwind;uid=csharp;pwd=c#.net2004;"; private SqlConnection _conn; ///构造函数 public DataAccess() { _conn = new SqlConnection(_connstring); } |
public void FillCustomerOrdersInfo(DatasetOrders ds) { SqlCommand comm = new SqlCommand("GetCustomerOrdersInfo",_conn); comm.CommandType = CommandType.StoredProcedure; SqlDataAdapter dataAdapter = new SqlDataAdapter(comm); dataAdapter.TableMappings.Add("Table","Customers"); dataAdapter.TableMappings.Add("Table1","Orders"); dataAdapter.TableMappings.Add("Table2","Order Details"); dataAdapter.Fill(ds); } |
public void FillCustomerOrdersInfoWithSqlHelper(DatasetOrders ds) { SqlHelper.FillDataset(_connstring,CommandType.StoredProcedure,"GetCustomerOrdersInfo",ds,new string[]{"Customers","Orders","Order Details"}); } |
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(); } |
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; } |
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) { _ds.Clear();//重新填充数据集 _dataAccess.FillCustomerOrdersInfo(_ds); //_dataAccess.FillCustomerOrdersInfoWithSqlHelper(_ds); } |
SqlCommand comm = new SqlCommand("GetCustomerOrdersInfo",_conn); comm.CommandType = CommandType.StoredProcedure; _conn.Open(); SqlDataReader reader = comm.ExecuteReader(); do { while(reader.Read()) { Console.WriteLine(reader[0].ToString());//获取数据代码 } }while(reader.NextResult()); Console.ReadLine(); _conn.Close(); |