1

当我的程序运行时,以下错误不断弹出。对此很新,所以我不知道如何去捕捉这个......做了一个快速搜索alrd,但似乎没有任何帮助。

以下是我的代码的一部分:

protected DBBase(string tableName, string primary_key)
{
    string s = Application.StartupPath + "\\alliancedb.accdb";
    conn = new OleDbConnection(String.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}", s));
    if (conn.State == ConnectionState.Closed)
    {
        conn.Open();
        dataset = new DataSet();
        //query statement
        string sqlStatement = String.Format("Select * from {0} order by {1} ASC", tableName, primary_key);
        //run sql
        DBadpt = new OleDbDataAdapter(sqlStatement, conn);
        oOrderDetailsCmdBuilder = new OleDbCommandBuilder(DBadpt);
        DBadpt.Fill(dataset);
        dbTab = dataset.Tables[0];
        dbTab.TableName = this.tableName = tableName;

        rows = dbTab.Rows;
    }
    else if (conn.State == ConnectionState.Open)
    {
        conn.Close();
        conn.Open();
        dataset = new DataSet();
        //query statement
        string sqlStatement = String.Format("Select * from {0} order by {1} ASC", tableName, primary_key);
        //run sql
        DBadpt = new OleDbDataAdapter(sqlStatement, conn);
        oOrderDetailsCmdBuilder = new OleDbCommandBuilder(DBadpt);
        DBadpt.Fill(dataset);
        dbTab = dataset.Tables[0];
        dbTab.TableName = this.tableName = tableName;

        rows = dbTab.Rows;
    }
}
4

1 回答 1

1

不要让您的连接处于打开状态。始终打开它们,获取您的数据,然后立即关闭它们。using块通常是为您关闭对象的最干净的方法 。

此外,如果您的任何表格或字段是关键字或其中有空格,则必须将它们放在 [括号] 中:

using (OleDbConnection conn = new OleDbConnection(String.Format("Provider=Microsoft.ACE.OLEDB.12.0; Data Source={0}", s))) {
  conn.Open();
  string sqlStatement = String.Format("Select * from [{0}] order by [{1}] ASC", tableName, primary_key);
  // etc..
}
于 2013-09-15T12:21:06.120 回答