1

我正在尝试在 Windows 8 中使用 c# 和 oledb 写入 Paradox 数据库文件。我能够删除表、创建表并在收到错误“操作必须使用可更新查询”之前写入第一行。

我已经通过以下步骤尝试解决:1)以管理员身份运行 2)更新应用程序中的权限以确保我没有文件上的只读或存档属性 3)添加正在运行的用户权限以读取/ 写入 / 修改 db 文件所在的目录 4) 多次更改查询,以防我对查询做一些奇怪的事情

如果我根本无法写入或插入,那么上述步骤将是有意义的,但初始插入有效并且任何其他插入都会失败。

下面的代码显示了我尝试执行这些操作的当前方式,我已将其留在注释掉的部分中,以便您可以看到我还尝试过的其他内容。

public void OverwriteData(string fileName, DataTable dataToWrite)
{
    //split up the filename
    string path = Path.GetDirectoryName(fileName);
    string file = Path.GetFileName(fileName);

    //create the string for creating the table
    string strTempCreate = "";
    //string strTempInsert = "";
    foreach (DataColumn column in dataToWrite.Columns)
    {
        if (strTempCreate != "")
        {
            strTempCreate = strTempCreate + ", ";
        }
        strTempCreate = strTempCreate + "[" + column.ColumnName + "]" + " char(30)";

        /*if (strTempInsert != "")
        {
            strTempInsert = strTempInsert + ", ";
        }
        strTempInsert = strTempInsert + column.ColumnName;*/
    }
    string createTableStr = "CREATE TABLE " + file + " (" + strTempCreate + ")";
    string dropTableStr = "DROP TABLE " + file;
    //build the sql insert command
    //string insertSql = "insert into " + file + " values ";
    /*foreach (DataRow row in dataToWrite.Rows)
    {
        insertSql = insertSql + row.Field<string>;
    }*/


    string connectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + @";Extended Properties=Paradox 5.x;";
    //DataTable results = new DataTable();
    using (OleDbConnection conn = new OleDbConnection(connectionString))
    {
        conn.Open();
        OleDbCommand dbCommand = new OleDbCommand();
        dbCommand.Connection = conn;
        dbCommand.CommandText = dropTableStr;
        try
        {
            dbCommand.ExecuteNonQuery();
        }
        catch { }

        dbCommand.CommandText = createTableStr;
        dbCommand.ExecuteNonQuery();

        //try to do the insert
        StringBuilder sb = new StringBuilder();

        //make sure that the database is not readonly
        FileAttributes attributes = File.GetAttributes(fileName);
        if ((attributes & FileAttributes.Archive) == FileAttributes.Archive)
        {
            attributes = attributes & ~FileAttributes.Archive;
            File.SetAttributes(fileName, attributes);
        }

        //then we want to try and connect to this database to put data into it
        string selectSQL = "Select * from " + file;
        using (var adapter = new OleDbDataAdapter(selectSQL, conn))
        {
            using (var builder = new OleDbCommandBuilder(adapter))
            {
                var destinationTable = new DataTable();
                adapter.Fill(destinationTable);
                destinationTable.Merge(dataToWrite,true,MissingSchemaAction.Ignore);
                destinationTable.AcceptChanges();
                foreach (DataRow row in destinationTable.Rows)
                {
                    row.SetAdded();
                }
                builder.QuotePrefix = "[";
                builder.QuoteSuffix = "]";
                builder.GetInsertCommand();
                adapter.Update(destinationTable);
            }
        }

       /*foreach (DataRow row in dataToWrite.Rows)
        {
            sb.Clear();
            sb.Append("insert into " + file + " values ('");
            IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
            sb.Append(string.Join("','", fields));
            sb.Append("')");
            dbCommand.CommandText = sb.ToString();
            dbCommand.ExecuteNonQuery();
        }*/

        /*sb.Clear();
        sb.Append("insert into " + file);
        foreach (DataRow row in dataToWrite.Rows)
        {
            sb.Append(" values ('");
            IEnumerable<string> fields = row.ItemArray.Select(field => field.ToString());
            sb.Append(string.Join("','", fields));
            sb.Append("'),");
        }
        sb.Remove(sb.Length - 1, 1);
        sb.Append(";");
        dbCommand.CommandText = sb.ToString();
        dbCommand.ExecuteNonQuery();
        */

    }
}
4

1 回答 1

0

这篇文章解决了我的问题:https ://msdn.microsoft.com/en-us/library/ms715421(v=vs.85).aspx

根据 Microsoft 的说法:在两种情况下,Paradox 驱动程序无法更新表:

  1. 当表上没有定义唯一索引时。这对于空表来说是不正确的,即使没有在表上定义唯一索引,也可以用单行更新。如果在没有唯一索引的空表中插入单行,则应用程序无法在插入单行后创建唯一索引或插入其他数据。
  2. 如果没有实现 Borland 数据库引擎。
于 2015-12-03T04:01:10.207 回答