简体   繁体   English

C#查询MS-Access表并将读取的值放在文本框中

[英]C# Query MS-Access Table and place read values from column in a text box

Below is a snapshot of my code. 下面是我的代码的快照。 I am trying to access the only column in the customer table and place the values into a textbox on the form. 我正在尝试访问客户表中的唯一列,并将值放入表单上的文本框。 I keep getting the error with my code "InvalidOperationException was unhandled" at the line declaring dr as a OleDbDataReader object. 在将dr声明为OleDbDataReader对象的行中,我的代码"InvalidOperationException was unhandled"不断出现错误。

  1. What do I have wrong with the below code that would be giving me this error? 以下代码会给我这个错误,我怎么了?

  2. Should I do a list to pick out the text I want from the database? 我应该做一个列表来从数据库中挑选我想要的文本吗?

  3. How can I return the column values from access into a list in C# so that I can search the list for a particular value? 如何将访问中的列值返回到C#中的列表中,以便可以在列表中搜索特定值?

     string strsql = "Select * from Customer"; OleDbCommand cmd = new OleDbCommand(); cmd.CommandText = strsql; conn.Open(); OleDbDataReader dr = cmd.ExecuteReader(); while(dr.Read()) { textBox1.Text += dr["Customer"].ToString(); } conn.Close(); 

A command carries the info to be executed, a connection carries the info to reach the database server. 命令携带要执行的信息,连接携带信息到达数据库服务器。 The two objects should be linked together to produce any result. 这两个对象应链接在一起以产生任何结果。 You miss that line 你想念那条线

OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = strsql;
cmd.Connection = conn; // <= here
conn.Open();

Remember also that disposable objects like a command, a reader and a connection should be disposed immediately after usage. 还请注意, 一次性对象像的命令,读取器和连接应在使用后立即被设置 For this pattern exists the using statement 对于这种模式,存在using语句

So you should write 所以你应该写

string cmdText = "Select * from Customer";
using(OleDbConnection conn = new OleDbConnection(.....constring...))
using(OleDbCommand cmd = new OleDbCommand(cmdText, conn))
{
    conn.Open();
    using(OleDbDataReader reader = cmd.ExecuteReader())
    {
        while(reader.Read())
           .....
    }
}

Here is some sample code. 这是一些示例代码。

try
            {
                using (OleDbConnection myConnection = new OleDbConnection())//make use of the using statement 
                {
                    myConnection.ConnectionString = myConnectionString;
                    myConnection.Open();//Open your connection
                    OleDbCommand cmdNotReturned = myConnection.CreateCommand();//Create a command 
                    cmdNotReturned.CommandText = "someQuery";
                    OleDbDataReader readerNotReturned = cmdNotReturned.ExecuteReader(CommandBehavior.CloseConnection);
                        // close conn after complete
                    // Load the result into a DataTable
                    if (readerNotReturned != null) someDataTable.Load(readerNotReturned);
               }
            }

After that you have a Datatable containing your data. 之后,您将有一个包含数据的数据表。 Ofcourse you can afterwards search for records in the Datatable any way you like. 当然,您以后可以使用任何方式在数据表中搜索记录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM