Oledbdatareader reader = comand.executereader();这一行出错了 [英] Oledbdatareader reader = comand.executereader(); error in this line

查看:106
本文介绍了Oledbdatareader reader = comand.executereader();这一行出错了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;

namespace practice2
{
    public partial class Form1 : Form
    {
       private OleDbConnection connection = new OleDbConnection();
       
        public Form1()
        {
            InitializeComponent();
             connection.ConnectionString = @"Provider = Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\Ahmed ch\Documents\Database1.accdb;
            Persist Security Info = False;";
           
        }

      
        private void Form1_Load(object sender, EventArgs e)
        {
           try
            {
            connection.Open();
           
                OleDbCommand comand = new OleDbCommand();
                String query =  "select* from Sheet2"
                comand.CommandText = query;
                OleDbDataReader reader = comand.ExecuteReader();
                while (reader.Read())
                {
                     comboBox1.Items.Add(reader["ProductName"].ToString());
                }


                connection.Close();
               
            }
                catch(Exception ex)
            {
                MessageBox.Show("Error" + ex);
                
            }
           
       
        }}}





我尝试过:



i还安装oledb驱动程序,MS Access数据库引擎X86,也是X64,但每次出现错误,



What I have tried:

i also install oledb driver , MS Access database Engine X86 , also X64 , but every time error comes ,

OleDbDataReader reader = comand.ExecuteReader();

在这一行

推荐答案

你得到的异常很可能是System.InvalidOperationException:ExecuteReader:Connection属性还没有初始化。这是因为你忘了设置OleDBCOmmand的Connection属性。



The exception you get is most probably "System.InvalidOperationException: ExecuteReader: Connection property has not been initialized". This because you forgot to set the Connection property of the OleDBCOmmand.

private void Form1_Load(object sender, EventArgs e)
{
  try
  {
    connection.Open();

    OleDbCommand comand = new OleDbCommand();
    String query = "select* from Sheet2"
    comand.CommandText = query;
    comand.Connection = connection;  // <<<< set connection
    OleDbDataReader reader = comand.ExecuteReader();
  // ....
}


两个可能的问题:一个是(如0x01AA所说)命令没有连接到任何数据库。另一个是你在SQL命令中应该在SELECT和*之间留一个空格 - 这不是必需的,但这是一个好主意 - 特别是因为SELECT *被认为是一个坏主意。您应该在SELECT中命名列,以便只获取所需的列。

此外,命令和连接是稀缺资源,使用using块确保它们是一个好主意根据需要关闭和处理。



Two possible problems: one is (as 0x01AA said) that the command is not connected to any database. The other is that you should have a space between "SELECT" and "*" in your SQL command - it's not required, but it's a good idea - particularly as "SELECT *" is considered a bad idea anyway. You should name your columns in the SELECT so that you only fetch the ones you need.
In addition, Commands and Connections are scarce resources, and it's a good idea to use a using block to ensure they are closed and disposed as needed.

try
    {
    using (OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0; Data Source = C:\Users\Ahmed ch\Documents\Database1.accdb;Persist Security Info = False;";))
        {
        con.Open();
        using (OleDbCommand cmd = new OleDbCommand("SELECT ProductName FROM Sheet2", con))
            {
            using (OleDbDataReader reader = cmd.ExecuteReader())
                {
                while (reader.Read())
                    {
                    comboBox1.Items.Add(reader["ProductName"].ToString());
                    }
                }
            }
        }
    }
catch(Exception ex)
    {
    MessageBox.Show("Error" + ex);
    }


这篇关于Oledbdatareader reader = comand.executereader();这一行出错了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆