对象不是ADODB记录集=C#填充数据表

对象不是ADODB记录集=C#填充数据表,c#,sql,string,ms-access,int,C#,Sql,String,Ms Access,Int,我正试图通过匹配comboBox2中选择的数字从access DB中恢复记录,并恢复与该数字相关的所有行-这在其他项目中运行良好,但出于某种原因,我一直收到上面的相关错误-任何帮助都会很好。。错误在dap.Fill(dt,typeof(Int32))上;-当我取出“typeof int32 out”时,我得到了这个错误:“语法错误,表达式'OCR='中缺少运算符;” 编辑:如果有区别,我将在此类事件中使用此代码: 使用所有注释的帮助编辑修复-修复了现在在不起作用的代码之后添加的代码 组合框2\u

我正试图通过匹配comboBox2中选择的数字从access DB中恢复记录,并恢复与该数字相关的所有行-这在其他项目中运行良好,但出于某种原因,我一直收到上面的相关错误-任何帮助都会很好。。错误在dap.Fill(dt,typeof(Int32))上;-当我取出“typeof int32 out”时,我得到了这个错误:“语法错误,表达式'OCR='中缺少运算符;”

编辑:如果有区别,我将在此类事件中使用此代码:

使用所有注释的帮助编辑修复-修复了现在在不起作用的代码之后添加的代码

组合框2\u选择的值已更改

   string strprovider = @"provider=microsoft.jet.oledb.4.0;data source=C:\Users\farrejos\Documents\inboxV2.mdb;persist security info=false";
            OleDbConnection newConn = new OleDbConnection(strprovider);
            System.Data.DataTable dt = new System.Data.DataTable();
            //DataSet ds = new DataSet();
            //ds.Tables.Add(dt);
            OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);

            if (dap != null)
            {
                //dap.Fill(ds);

                dap.Fill(dt, typeof(Int32));
            }

            //dap.Fill(ds, "ocr");

            foreach (DataRow myRow in dt.Rows)
            {
                textBox3.Text = myRow[4].ToString();
                textBox4.Text = myRow[6].ToString();
            }
        }
固定代码:

  System.Data.DataTable dt = new System.Data.DataTable();
        DataSet ds = new DataSet();

        OleDbDataAdapter da = new OleDbDataAdapter("Select *  from ocr where [OCR] = " + comboBox2.SelectedText.ToString() + "", newConn);

        da.Fill(dt);
        foreach (DataRow myRow in dt.Rows)
        {
            textBox3.Text = myRow[4].ToString();

            textBox4.Text = myRow[6].ToString();
        }
你喜欢这样吗

 string strprovider = @"provider=microsoft.jet.oledb.4.0;data source=C:\Users\farrejos\Documents\inboxV2.mdb;persist security info=false";
        OleDbConnection newConn = new OleDbConnection(strprovider);
        System.Data.DataTable dt = new System.Data.DataTable();
        DataSet ds = new DataSet();
        //ds.Tables.Add(dt);
        OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);

        if (dap != null)
        {                
            dap.Fill(ds);
        }

        foreach (DataRow myRow in ds.Tables[0].Rows)
        {
            textBox3.Text = myRow[4].ToString();
            textBox4.Text = myRow[6].ToString();
        }
    }
你喜欢这样吗

 string strprovider = @"provider=microsoft.jet.oledb.4.0;data source=C:\Users\farrejos\Documents\inboxV2.mdb;persist security info=false";
        OleDbConnection newConn = new OleDbConnection(strprovider);
        System.Data.DataTable dt = new System.Data.DataTable();
        DataSet ds = new DataSet();
        //ds.Tables.Add(dt);
        OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);

        if (dap != null)
        {                
            dap.Fill(ds);
        }

        foreach (DataRow myRow in ds.Tables[0].Rows)
        {
            textBox3.Text = myRow[4].ToString();
            textBox4.Text = myRow[6].ToString();
        }
    }
你喜欢这样吗

 string strprovider = @"provider=microsoft.jet.oledb.4.0;data source=C:\Users\farrejos\Documents\inboxV2.mdb;persist security info=false";
        OleDbConnection newConn = new OleDbConnection(strprovider);
        System.Data.DataTable dt = new System.Data.DataTable();
        DataSet ds = new DataSet();
        //ds.Tables.Add(dt);
        OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);

        if (dap != null)
        {                
            dap.Fill(ds);
        }

        foreach (DataRow myRow in ds.Tables[0].Rows)
        {
            textBox3.Text = myRow[4].ToString();
            textBox4.Text = myRow[6].ToString();
        }
    }
你喜欢这样吗

 string strprovider = @"provider=microsoft.jet.oledb.4.0;data source=C:\Users\farrejos\Documents\inboxV2.mdb;persist security info=false";
        OleDbConnection newConn = new OleDbConnection(strprovider);
        System.Data.DataTable dt = new System.Data.DataTable();
        DataSet ds = new DataSet();
        //ds.Tables.Add(dt);
        OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);

        if (dap != null)
        {                
            dap.Fill(ds);
        }

        foreach (DataRow myRow in ds.Tables[0].Rows)
        {
            textBox3.Text = myRow[4].ToString();
            textBox4.Text = myRow[6].ToString();
        }
    }

如果OCR字段是字符串字段,则可能需要使用撇号将组合框值括起来:

OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = '" + comboBox2.SelectedText.ToString() + "'", newConn);
编辑

我已经有几年没有这样做了,但我相信我还记得OleDbDataAdapter在构造函数中与
where
子句一起使用时偶尔会给我带来一些麻烦

是否改为使用
OleDbCommand
进行尝试

            OleDbDataAdapter dap = new OleDbDataAdapter();
            dap.SelectCommand = new OleDbCommand("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);
            dap.Fill(dt, typeof(Int32));
编辑2

SQL语句使用“ocr”作为表名和字段名。这可能是提供商有问题的模棱两可之处

将其更改为:

Select * from [ocr] where [ocr].[OCR] = ...
还有一些想法,请按以下顺序尝试:

  • 列出要手动选择的字段名,而不是
    *
  • 用括号将表名括起来
  • 用括号将字段名括起来

  • 重写代码以使用
    OleDbParameter
    进行查询


无论如何,我强烈建议使用。这将帮助您直截了当地了解语法问题,并帮助您防范SQL注入攻击。

如果您的OCR字段是字符串字段,您可能需要用撇号括住组合框值:

OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = '" + comboBox2.SelectedText.ToString() + "'", newConn);
编辑

我已经有几年没有这样做了,但我相信我还记得OleDbDataAdapter在构造函数中与
where
子句一起使用时偶尔会给我带来一些麻烦

是否改为使用
OleDbCommand
进行尝试

            OleDbDataAdapter dap = new OleDbDataAdapter();
            dap.SelectCommand = new OleDbCommand("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);
            dap.Fill(dt, typeof(Int32));
编辑2

SQL语句使用“ocr”作为表名和字段名。这可能是提供商有问题的模棱两可之处

将其更改为:

Select * from [ocr] where [ocr].[OCR] = ...
还有一些想法,请按以下顺序尝试:

  • 列出要手动选择的字段名,而不是
    *
  • 用括号将表名括起来
  • 用括号将字段名括起来

  • 重写代码以使用
    OleDbParameter
    进行查询


无论如何,我强烈建议使用。这将帮助您直截了当地了解语法问题,并帮助您防范SQL注入攻击。

如果您的OCR字段是字符串字段,您可能需要用撇号括住组合框值:

OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = '" + comboBox2.SelectedText.ToString() + "'", newConn);
编辑

我已经有几年没有这样做了,但我相信我还记得OleDbDataAdapter在构造函数中与
where
子句一起使用时偶尔会给我带来一些麻烦

是否改为使用
OleDbCommand
进行尝试

            OleDbDataAdapter dap = new OleDbDataAdapter();
            dap.SelectCommand = new OleDbCommand("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);
            dap.Fill(dt, typeof(Int32));
编辑2

SQL语句使用“ocr”作为表名和字段名。这可能是提供商有问题的模棱两可之处

将其更改为:

Select * from [ocr] where [ocr].[OCR] = ...
还有一些想法,请按以下顺序尝试:

  • 列出要手动选择的字段名,而不是
    *
  • 用括号将表名括起来
  • 用括号将字段名括起来

  • 重写代码以使用
    OleDbParameter
    进行查询


无论如何,我强烈建议使用。这将帮助您直截了当地了解语法问题,并帮助您防范SQL注入攻击。

如果您的OCR字段是字符串字段,您可能需要用撇号括住组合框值:

OleDbDataAdapter dap = new OleDbDataAdapter("Select * from ocr where OCR = '" + comboBox2.SelectedText.ToString() + "'", newConn);
编辑

我已经有几年没有这样做了,但我相信我还记得OleDbDataAdapter在构造函数中与
where
子句一起使用时偶尔会给我带来一些麻烦

是否改为使用
OleDbCommand
进行尝试

            OleDbDataAdapter dap = new OleDbDataAdapter();
            dap.SelectCommand = new OleDbCommand("Select * from ocr where OCR = " + comboBox2.SelectedText.ToString() + "", newConn);
            dap.Fill(dt, typeof(Int32));
编辑2

SQL语句使用“ocr”作为表名和字段名。这可能是提供商有问题的模棱两可之处

将其更改为:

Select * from [ocr] where [ocr].[OCR] = ...
还有一些想法,请按以下顺序尝试:

  • 列出要手动选择的字段名,而不是
    *
  • 用括号将表名括起来
  • 用括号将字段名括起来

  • 重写代码以使用
    OleDbParameter
    进行查询


无论如何,我强烈建议使用。这将帮助您直截了当地了解语法问题,并帮助您防范SQL注入攻击。

您需要开始分解代码,首先,由于它抱怨SQL表达式,我会将其移动到它自己的变量

    DataSet ds = new DataSet();
    //ds.Tables.Add(dt);
    string sql = String.Format("select * from [ocr] where [ocr].[OCR] = {0}", comboBox2.SelectedText);
    System.Diagnostics.Debug.WriteLine(s);

    OleDbDataAdapter dap = new OleDbDataAdapter(sql, newConn);

然后在输出窗口中,您将看到它试图执行的SQL,这将帮助您缩小根本原因。

您需要开始分解代码,首先,因为它抱怨SQL表达式,我会将它移动到它自己的变量

    DataSet ds = new DataSet();
    //ds.Tables.Add(dt);
    string sql = String.Format("select * from [ocr] where [ocr].[OCR] = {0}", comboBox2.SelectedText);
    System.Diagnostics.Debug.WriteLine(s);

    OleDbDataAdapter dap = new OleDbDataAdapter(sql, newConn);

然后在输出窗口中,您将看到它试图执行的SQL,这将帮助您缩小根本原因。

您需要开始分解代码,首先是因为它抱怨SQL表达式I