gpt4 book ai didi

c# - 为什么在尝试使用数据表 : 更新 Access 数据库文件 (.mdb) 时出现此错误

转载 作者:行者123 更新时间:2023-12-01 16:19:29 24 4
gpt4 key购买 nike

我已经花了 30 个小时来解决我的问题。每次我修复一件事时,就会出现新的错误。我想做的就是从内存中获取 DataTables 并简单地更新 Access .MDB 文件。这是我的代码:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Text;

namespace LCR_ShepherdStaffupdater_1._0
{
public class DatabaseHandling
{
static DataTable datatableB = new DataTable();
static DataTable datatableA = new DataTable();
public static DataSet datasetA = new DataSet();
public static DataSet datasetB = new DataSet();
static OleDbDataAdapter adapterA = new OleDbDataAdapter();
static OleDbDataAdapter adapterB = new OleDbDataAdapter();
static string connectionstringA = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Settings.getfilelocationA();
static string connectionstringB = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Settings.getfilelocationB();
static OleDbConnection dataconnectionB = new OleDbConnection(connectionstringB);
static OleDbConnection dataconnectionA = new OleDbConnection(connectionstringA);
static DataTable tableListA;
static DataTable tableListB;

static public void addTableA(string table, bool addtoDataSet)
{
dataconnectionA.Open();
datatableA = new DataTable(table);
try
{
OleDbCommand commandselectA = new OleDbCommand("SELECT * FROM [" + table + "]", dataconnectionA);
adapterA.SelectCommand = commandselectA;
adapterA.Fill(datatableA);
}
catch
{
Logging.updateLog("Error: Tried to get " + table + " from DataSetA. Table doesn't exist!", true, false, false);
}

if (addtoDataSet == true)
{
datasetA.Tables.Add(datatableA);
Logging.updateLog("Added DataTableA: " + datatableA.TableName.ToString() + " Successfully!", false, false, false);
}

dataconnectionA.Close();
}

static public void addTableB(string table, bool addtoDataSet)
{
dataconnectionB.Open();
datatableB = new DataTable(table);

try
{
OleDbCommand commandselectB = new OleDbCommand("SELECT * FROM [" + table + "]", dataconnectionB);
adapterB.SelectCommand = commandselectB;
adapterB.Fill(datatableB);
}
catch
{
Logging.updateLog("Error: Tried to get " + table + " from DataSetB. Table doesn't exist!", true, false, false);
}



if (addtoDataSet == true)
{
datasetB.Tables.Add(datatableB);
Logging.updateLog("Added DataTableB: " + datatableB.TableName.ToString() + " Successfully!", false, false, false);
}

dataconnectionB.Close();
}

static public string[] getTablesA(string connectionString)
{
dataconnectionA.Open();
tableListA = dataconnectionA.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" });
string[] stringTableListA = new string[tableListA.Rows.Count];

for (int i = 0; i < tableListA.Rows.Count; i++)
{
stringTableListA[i] = tableListA.Rows[i].ItemArray[2].ToString();
}
dataconnectionA.Close();
return stringTableListA;
}

static public string[] getTablesB(string connectionString)
{
dataconnectionB.Open();
tableListB = dataconnectionB.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new Object[] { null, null, null, "TABLE" });
string[] stringTableListB = new string[tableListB.Rows.Count];

for (int i = 0; i < tableListB.Rows.Count; i++)
{
stringTableListB[i] = tableListB.Rows[i].ItemArray[2].ToString();
}
dataconnectionB.Close();
return stringTableListB;
}

static public void createDataSet()
{

string[] tempA = getTablesA(connectionstringA);
string[] tempB = getTablesB(connectionstringB);
int percentage = 0;
int maximum = (tempA.Length + tempB.Length);

Logging.updateNotice("Loading Tables...");
Logging.updateLog("Started Loading File A", false, false, true);
for (int i = 0; i < tempA.Length ; i++)
{
if (!datasetA.Tables.Contains(tempA[i]))
{
addTableA(tempA[i], true);
percentage++;
Logging.loadStatus(percentage, maximum);
}
else
{
datasetA.Tables.Remove(tempA[i]);
addTableA(tempA[i], true);
percentage++;
Logging.loadStatus(percentage, maximum);
}
}
Logging.updateLog("Finished loading File A", false, false, true);
Logging.updateLog("Started loading File B", false, false, true);
for (int i = 0; i < tempB.Length ; i++)
{
if (!datasetB.Tables.Contains(tempB[i]))
{
addTableB(tempB[i], true);
percentage++;
Logging.loadStatus(percentage, maximum);
}
else
{
datasetB.Tables.Remove(tempB[i]);
addTableB(tempB[i], true);
percentage++;
Logging.loadStatus(percentage, maximum);
}
}
Logging.updateLog("Finished loading File B", false, false, true);
Logging.updateLog("Both files loaded into memory successfully", false, true, false);


}

static public DataTable getDataTableA()
{
datatableA = datasetA.Tables[Settings.textA];

return datatableA;
}
static public DataTable getDataTableB()
{
datatableB = datasetB.Tables[Settings.textB];
return datatableB;
}

static public DataSet getDataSetA()
{
return datasetA;
}

static public DataSet getDataSetB()
{
return datasetB;
}

static public void InitiateCopyProcessA()
{
DataSet tablesA;
tablesA = DatabaseHandling.getDataSetA();

foreach (DataTable table in tablesA.Tables)
{
OverwriteTable(table, table.TableName);
Logging.updateLog("Copied " + table.TableName + " successfully.", false, true, false);
}

}

static void OverwriteTable(DataTable sourceTable, string tableName)
{
using (var destConn = new OleDbConnection(connectionstringA))
using (var destCmd = new OleDbCommand(tableName, destConn) { CommandType = CommandType.TableDirect })
using (var destDA = new OleDbDataAdapter(destCmd))
{
// Since we're using a single table, we can have the CommandBuilder
// generate the appropriate INSERT and DELETE SQL statements
using (var destCmdB = new OleDbCommandBuilder(destDA))
{
destCmdB.QuotePrefix = "["; // quote reserved column names
destCmdB.QuoteSuffix = "]";
destDA.DeleteCommand = destCmdB.GetDeleteCommand();
destDA.InsertCommand = destCmdB.GetInsertCommand();

// Get rows from destination, and delete them
var destTable = new DataTable();
destDA.Fill(destTable);
foreach (DataRow dr in destTable.Rows)
{
dr.Delete();
}

destDA.Update(destTable); // !!! Run-time error: Syntax error in FROM clause. !!!

// Set rows from source as Added, so the DataAdapter will insert them
foreach (DataRow dr in sourceTable.Rows)
{
dr.SetAdded();
}
destDA.Update(sourceTable);
}
}
}



}
}

编辑:

好的,谢谢!但同样,新的问题又出现了。相同的源代码,但当我将表复制到 .MDB 文件时,已经复制了 10 个左右的表,然后出现以下运行时错误:

对于不返回任何关键列信息的 SelectCommand,不支持对 DeleteCommand 进行动态 SQL 生成。

在此命令上destDA.DeleteCommand = destCmdB.GetDeleteCommand();

这应该是我的最后一个问题,我的项目将会完成。

最佳答案

destCmdB.QuotePrefix = "["; // quote reserved column names
destCmdB.QuotePrefix = "]";

QuotePrefix 两次?也许这可能是一个问题。

关于c# - 为什么在尝试使用数据表 : 更新 Access 数据库文件 (.mdb) 时出现此错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/522591/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com