0

我正在使用以下代码访问 MS Access 数据库。但是我收到一条错误消息填充:SelectCommand.Connection 属性尚未初始化。我该如何解决这个问题。

common.cs
=========
public static bool DBConnectionStatus()
        {
            try
            {
                string conString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|db_admin.mdb; Jet OLEDB:Database Password=admin";
                using (OleDbConnection conn = new OleDbConnection(conString))
                {
                    conn.Open();
                    return (conn.State == ConnectionState.Open);
                }
            }
            catch (OleDbException)
            {
                return false;
            }


protected void btn_general_Click(object sender, EventArgs e)
        {
            try
            {
                bool state = common.DBConnectionStatus();
                if (state == true)
                {
                    cmd = new OleDbCommand("select * from tbl_admin");
                    da = new OleDbDataAdapter(cmd);
                    DataSet ds = new DataSet();

                    da.Fill(ds); // Error Here 
                    if (ds.Tables[0].Rows.Count > 0)
                    {

                    }

                }
            }
            catch (Exception e1)
            {
            }
        }
4

3 回答 3

0

我确实建议您将代码修改为以下内容:

private DataTable YourData()  
{  
    string conString = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=|DataDirectory|db_admin.mdb; Jet OLEDB:Database Password=admin";  
    DataSet ds = new DataSet();  
    using (SqlConnection conn = new SqlConnection(conString))  
    {  
        try  
        {  
            conn.Open();  
            SqlCommand command = new SqlCommand("select * from tbl_admin", conn);  
            command.CommandType = CommandType.Text;  

            SqlDataAdapter adapter = new SqlDataAdapter(command);  
            adapter.Fill(ds);  

            if (ds.Tables[0].Rows.Count > 0)  
            {  
                // do somethign here  
            }  
        }
        catch (Exception)  
        {
            /*Handle error*/  
        }  
    }  
    return ds.Tables[0];  
}  

接着:

protected void btn_general_Click(object sender, EventArgs e)
{          
     this.YourData(); // you will get the Data here. you can then use it the way you want it         
}
于 2013-03-10T12:20:49.253 回答
0

您正在初始化Command用于构造 a 的 a DataAdapter,但两者均未设置所需的Connection属性:

cmd = new OleDbCommand("select * from tbl_admin"); // <-- no connectuion assigned
da = new OleDbDataAdapter(cmd);  // <-- no connectuion assigned

所以你的例外是不言自明的。

最后一点:usingwill dispose/ closethe connection,因此该方法DBConnectionStatus毫无意义。所以不要使用它,而是using首先使用 in :

try
{
    using(var con = new OleDbConnection(connectionString))
    using(var da = new OleDbDataAdapter("elect * from tbl_admin", con))
    {
        var table = new DataTable();
        da.Fill(table); // note that you don't need to open the connection with DataAdapter.Fill
        if (table.Rows.Count > 0)
        {
            // ...
        }
    }
}
catch (Exception e1)
{
    // don't catch an exception if you don't handle it in a useful way(at least loggging)
    throw;
}
于 2013-03-10T13:33:14.380 回答
0

根据您的要求,您还可以使用 SqlDataAdapter 代替 ExecuteReader。

public void ReadMyData(string connectionString)
{
    string queryString = "SELECT OrderID, CustomerID FROM Orders";
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        OleDbDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            Console.WriteLine(reader.GetInt32(0) + ", " + reader.GetString(1));
        }
        // always call Close when done reading.
        reader.Close();
    }
}
于 2018-11-16T05:19:53.527 回答