簡體   English   中英

填充:SelectCommand.Connection 屬性尚未初始化

[英]Fill: SelectCommand.Connection property has not been initialized

我正在使用以下代碼訪問 MS Access 數據庫。 但我收到一條錯誤消息 Fill: SelectCommand.Connection property has not been initialized.How can I解決這個問題。

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)
            {
            }
        }

我確實建議您將代碼修改為如下所示:

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         
}

您正在初始化用於構造DataAdapterCommand ,但兩者都沒有設置所需的Connection屬性:

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

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

最后一個注意事項: usingdispose / close connection ,因此DBConnectionStatus方法毫無意義。 所以不要使用它,而是首先使用using

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;
}

根據您的要求,您還可以使用 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();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM