扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:郑佐 来源:csdn 2007年11月13日
关键字:
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(); |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者