none
Fill属性尚未初始化 RRS feed

  • 问题

  • ************** 异常文本 **************
    System.InvalidOperationException: Fill: SelectCommand.Connection 属性尚未初始化。

    using(var adap=new OleDbDataAdapter($"select [day],[price],[numer],[type],[real e] from [戈胶单] where ([code]='{nORc[0]}' or [name]='{nORc[0]}') and [type] in('卖出','买入','送股','红利','中签扣款','托管转入') order by [day],[type]",@"provider= microsoft.ace.oledb.12.0;data source= d:\时时乱码\AssetsOf醚芪\交易记录.ACCDB")){
    						adap.Fill(dt);
    						adap.SelectCommand=new OleDbCommand($"select [day],[price],[numer],[type],[real e]from [戈胶单] where ([code]='{nORc[0]}' or [name]='{nORc[0]}') and [type] in('卖出','买入','送股','红利','中签扣款','托管转入') and ([day]<{valday[0]})order by [day],[type]");
    							adap.Fill(dt1);

    我原来是用两个adap而且都用using裹着,我突发奇想adap有selectcommand,改一下命令,再次填充就成了。

    不行,求解。

    2021年3月24日 8:10

答案

  • 你好,

    根据你提供的异常提示,我判断是没有给Adapter赋值对应的connection。

    我写了一个简单的sample,可以得到对应的dataset而无需重复写代码。

    static void Main(string[] args)
            {
                OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\School.mdb");
                DataSet set1 = GetDataSet("select * from Student where Age=22", connection);
                DataSet set2 = GetDataSet("select * from Student where Age=23", connection);
    
            }
            static DataSet GetDataSet(string sqltext,OleDbConnection connection)
            {
                connection.Open();
                DataSet dataSet = new DataSet();
                using (var adap = new OleDbDataAdapter(sqltext, connection))
                {
                    adap.Fill(dataSet);
                    connection.Close();
                    return dataSet;
                }
                
            }

    希望可以帮助你。

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 czhch 2021年3月26日 6:53
    2021年3月25日 2:41
    版主

全部回复

  • 改回原来两个adap,不用改命令
    2021年3月24日 8:21
  • 你好,

    根据你提供的异常提示,我判断是没有给Adapter赋值对应的connection。

    我写了一个简单的sample,可以得到对应的dataset而无需重复写代码。

    static void Main(string[] args)
            {
                OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\School.mdb");
                DataSet set1 = GetDataSet("select * from Student where Age=22", connection);
                DataSet set2 = GetDataSet("select * from Student where Age=23", connection);
    
            }
            static DataSet GetDataSet(string sqltext,OleDbConnection connection)
            {
                connection.Open();
                DataSet dataSet = new DataSet();
                using (var adap = new OleDbDataAdapter(sqltext, connection))
                {
                    adap.Fill(dataSet);
                    connection.Close();
                    return dataSet;
                }
                
            }

    希望可以帮助你。

    Best Regards,

    Jack



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • 已标记为答案 czhch 2021年3月26日 6:53
    2021年3月25日 2:41
    版主