Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用户代码未处理OLEDBEException-未指定错误_C#_Ms Access_Ado.net - Fatal编程技术网

C# 用户代码未处理OLEDBEException-未指定错误

C# 用户代码未处理OLEDBEException-未指定错误,c#,ms-access,ado.net,C#,Ms Access,Ado.net,当我的程序运行时,以下错误会随机出现。 这是很新的,所以我不知道如何去捕捉这个。。。 做了一个快速搜索,但似乎没有任何帮助 以下是我的代码的一部分: protected DBBase(string tableName, string primary_key) { string s = Application.StartupPath + "\\alliancedb.accdb"; conn = new OleDbConnection(String.Format("Provider=

当我的程序运行时,以下错误会随机出现。 这是很新的,所以我不知道如何去捕捉这个。。。 做了一个快速搜索,但似乎没有任何帮助

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

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

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

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

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

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

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

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