zoukankan      html  css  js  c++  java
  • FillDataSet 的解析

    使用ADO.net填充数强类型据集中多张表时,需要注意了:
    1.select查询语句顺序会影响到DataAdapter.Tablemappings.add方法.

    如果不使用映射,那么ADO将自动使用Table、Table1、Table2这样的表名去填充DataSet.
    第一条Select自然对应的Table.
      2.要想让ADO自动将表对应的填充到强类型DatsSet中的表,需使用TableMappings.Add()
      参考代码:
         
        public virtual void FillDataset(IDbCommand command, DataSet dataSet, string[] tableNames)
        {
            bool flag = false;
            this.CleanParameterSyntax(command);
            if (command.Connection.State != ConnectionState.Open)
            {
                command.Connection.Open();
                flag = true;
            }
            using (IDbDataAdapter dataAdapter = null)
            {
                dataAdapter = this.GetDataAdapter();
                dataAdapter.SelectCommand = command;
                if ((tableNames != null) && (tableNames.Length > 0))
                {
                    string str = "Table";
                    for (int i = 0; i < tableNames.Length; i++)
                    {
                        if ((tableNames[i] == null) || (tableNames[i].Length == 0))
                        {
                            throw new ArgumentException("The tableNames parameter must contain a list of tables, a value was provided as null or empty string.", "tableNames");
                        }
                        dataAdapter.TableMappings.Add(str + ((i == 0) ? "" : i.ToString()), tableNames[i]);
                    }
                }
                dataAdapter.Fill(dataSet);
                if (flag)
                {
                    command.Connection.Close();
                }
            }
        }
        
         
         private void Form1_Load(object sender, EventArgs e)
                {
                    StringBuilder strSql = new StringBuilder();
                    strSql.Append("select * from Employees ");
                    strSql.Append("select * from Products ");
                    strSql.Append("select * from Customers ");
                    
                    string connstr = ConfigurationManager.ConnectionStrings["test2.Properties.Settings.NorthwindConnectionString"].ConnectionString;
                    SqlConnection conn = new SqlConnection(connstr);
                    conn.Open();
                    SqlCommand cmd = new SqlCommand(strSql.ToString(),conn);
                    SqlDataAdapter ad = new SqlDataAdapter(cmd);
                    ad.TableMappings.Add("Table", "Employees");
                    ad.TableMappings.Add("Table1", "Products");
                    ad.TableMappings.Add("Table2", "Customers");
                    ad.Fill(dataSet11);
                }
      1. 相关阅读:
        Array对象---添加或删除数组中的元素->splice()
        微信小程序-文本作用域
        微信小程序-模块化
        属性动画的核心方法:ValueAnimator.ofInt(int... values)
        关于原生+WebView js交互、数据传输问题
        Beam Search快速理解及代码解析
        vue项目启动报错 spawn cmd ENOENT errno
        TortoiseGit提交每次都需要输入账号密码的解决办法
        Grpc.Core.RpcException: Status(StatusCode=DeadlineExceeded, Detail="Deadline Exceeded")
        公司限制网络,不能访问b站..etc,怎么办?搞起来
      2. 原文地址:https://www.cnblogs.com/perock/p/2778212.html
      Copyright © 2011-2022 走看看