1

出于某种原因,我不能使用我的 MySQL-DB,所以我切换到了 Access DB。

我想要一些小函数来执行简单的查询,如 SELECT、UPDATE 和 INSERT。

我遵循了这个小代码:

using System.Data.OleDb;
using System.Windows.Forms;
using System.Data;

class Csharp_Access
{
    public void Csharp_Access_Datenbank()
    {
        OleDbConnection con = new OleDbConnection(
        @"Provider=Microsoft.Jet.OLEDB.4.0;
        Data Source=C:\data.mdb");
        con.Open();

        string strSQL = "SELECT * FROM Tabelle1";

        OleDbCommand cmd = new OleDbCommand(strSQL, con);
        OleDbDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            MessageBox.Show(dr[0].ToString());
        }
        dr.Close();
        con.Close();
        }
    }

当我只是复制并粘贴它时,此代码可以完美运行。所以我决定把代码改成“dbFunctions.cs”:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;
using System.Xml.Serialization;

namespace AquadoServerTool
{
    class dbFunctions
    {
        /// <summary>
        /// Funktion, um eine Abfrage auszuführen
        /// </summary>
        /// <param name="QueryStr">SQL-String</param>
        /// <returns></returns>
        public static OleDbDataReader QueryString(string QueryStr)
        {
            // string strAccessSelect = "SELECT * FROM seriennummer";

            // Verbindung zur Datenbank aufbauen
            OleDbConnection con = null;
            try
            {
                con = new OleDbConnection(GlobalVar.strAccessConn);
                con.Open();
            }
            catch (Exception)
            {
                return null;
            }

            OleDbCommand cmd = new OleDbCommand(QueryStr, con);
            OleDbDataReader dr = cmd.ExecuteReader();
            // while (dr.Read())
            // {
            //     MessageBox.Show(dr[0].ToString());
            // }

            con.Close();

            return dr;
        }
    }

}

为了测试,如果可行,我做了一个小按钮:

    private void button1_Click(object sender, EventArgs e)
    {
        OleDbDataReader dr = dbFunctions.QueryString("SELECT * FROM seriennummer;");

        while (dr.Read())
        {
            MessageBox.Show(dr[1].ToString());
        }
        dr.Close();
    }

是的,现在点击那个按钮来遗忘!但我收到错误,Read() 不起作用,因为它已经关闭。

我的代码是完全错误的还是我忘记了什么?

问候,巨魔

4

2 回答 2

2

您的问题是您首先关闭连接,然后尝试从DataReader. 您必须保持连接打开才能阅读。


DataReader您可以围绕为您实现IDisposable和关闭连接创建一个包装器。

简单的例子:

class Wrapper : IDisposable
{
    public OleDbDataReader Reader { get { return reader; } }
    private OleDbConnection connection;

    public Wrapper(OleDbConnection connection, string QueryStr)
    { 
        this.connection = connection; 
        OleDbCommand cmd = new OleDbCommand(QueryStr, connection);
        OleDbDataReader dr = cmd.ExecuteReader();
    }

    public void Dispose()
    {
        reader.Dispose();
        connection.Dispose();
    }
}

class dbFunctions
{
    public static OleDbDataReader QueryString(string QueryStr)
    {
        OleDbConnection con = new OleDbConnection(GlobalVar.strAccessConn);
        con.Open();
        return new Wrapper(con, QueryStr);
    }
}

用法:

private void button1_Click(object sender, EventArgs e)
{
    using (var dr = dbFunctions.QueryString("SELECT * FROM seriennummer;"))
        while (dr.Reader.Read())
            MessageBox.Show(dr[1].ToString());
}

另一种方法是通过DataTable.

class dbFunctions
{
    public static DataTable QueryString(string QueryStr)
    {
        var result = new DataTable();
        using(OleDbConnection con = new OleDbConnection(GlobalVar.strAccessConn))
        {
            con.Open();
            using(OleDbCommand cmd = new OleDbCommand(QueryStr, con))
                result.Load(cmd.ExecuteReader());
        }
        return result;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    var data = dbFunctions.QueryString("SELECT * FROM seriennummer;");
    foreach(var row in data.Rows)
        MessageBox.Show(row[1].ToString());
}
于 2012-07-31T11:52:35.627 回答
0

使用DataSet而不是DataReader
DataReader需要打开连接来读取它。

于 2012-07-31T11:55:24.770 回答