本文介绍了怎样构建一个OleDbCommand的查询,这样我可以从一个.MDB拿表,并在另一个.MDB取代它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图采取从一个Access数据库文件的表,它们具有完全相同的结构,但不同的信息添加到另一个Access数据库文件。我需要覆盖任何现有的表。我几乎与我的项目,这是我最后一次砖墙完成。

I am trying to take tables from one Access Database File, add them to another Access Database file with the exact same structure but with different information. I need to overwrite any existing tables. I am almost done with my project this is last my brick wall.

我使用一个名为DatabaseHandling.cs与Access数据库文件的工作一个单独的类文件。

I am using a separate class file named DatabaseHandling.cs to work with the Access Database files.

下面是我的整个当前DatabaseHandling.cs代码。

Here is my ENTIRE current DatabaseHandling.cs code. This is kept up to date for now on.

Code:

代码>使用系统;
使用System.Collections.Generic;
使用System.Data这;
使用System.Data.OleDb;
:使用System.IO;
使用System.Linq的;
使用System.Text;

命名空间LCR_ShepherdStaffupdater_1._0
{
公共类DatabaseHandling
{
静态数据表datatableB =新的DataTable();
静态数据表datatableA =新的DataTable();
公共静态数据集datasetA =新的DataSet();
公共静态数据集datasetB =新的DataSet();
静态OleDbDataAdapter的adapterA =新OleDbDataAdapter的();
静态OleDbDataAdapter的适配器B =新OleDbDataAdapter的();
静态字符串connectionstringA =供应商= Microsoft.Jet.OLEDB.4.0; +数据源=+ Settings.getfilelocationA();
静态字符串connectionstringB =供应商= Microsoft.Jet.OLEDB.4.0; +数据源=+ Settings.getfilelocationB();
静态的OleDbConnection dataconnectionB =新的OleDbConnection(connectionstringB);
静态的OleDbConnection dataconnectionA =新的OleDbConnection(connectionstringA);
静态数据表tableListA;
静态数据表tableListB;

静态公共无效addTableA(字符串表,布尔addtoDataSet)
{
dataconnectionA.Open();
datatableA =新的DataTable(表);

{
OleDbCommand的commandselectA =新的OleDbCommand(SELECT * FROM [+表+],dataconnectionA);
adapterA.SelectCommand = commandselectA;
adapterA.Fill(datatableA);
}

{
Logging.updateLog(错误:试图得到+表+从DataSetA表不存在!,真的,假的,假的);
}

如果(addtoDataSet ==真)
{
datasetA.Tables.Add(datatableA);
Logging.updateLog(新增DataTableA:+ datatableA.TableName.ToString()+,假的,假的,假的成功!);
}

dataconnectionA.Close();
}

静态公共无效addTableB(字符串表,布尔addtoDataSet)
{
dataconnectionB.Open();
datatableB =新的DataTable(表);


{
OleDbCommand的commandselectB =新的OleDbCommand(SELECT * FROM [+表+],dataconnectionB);
adapterB.SelectCommand = commandselectB;
adapterB.Fill(datatableB);
}

{
Logging.updateLog(错误:试图得到+表+从DataSetB表不存在!,真的,假的,假的);
}



如果(addtoDataSet ==真)
{
datasetB.Tables.Add(datatableB);
Logging.updateLog(新增DataTableB:+ datatableB.TableName.ToString()+,假的,假的,假的成功!);
}

dataconnectionB.Close();
}

静态公共字符串[] getTablesA(字符串的connectionString)
{
dataconnectionA.Open();
tableListA = dataconnectionA.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,新的对象[] {NULL,NULL,NULL,TABLE});
的String [] = stringTableListA新的字符串[tableListA.Rows.Count]

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

静态公共字符串[] getTablesB(字符串的connectionString)
{
dataconnectionB.Open();
tableListB = dataconnectionB.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,新的对象[] {NULL,NULL,NULL,TABLE});
的String [] = stringTableListB新的字符串[tableListB.Rows.Count]

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

静态公共无效createDataSet()
{

的String [] = TEMPA getTablesA(connectionstringA);
的String [] = tempB getTablesB(connectionstringB);
INT百分比= 0;
INT最大=(tempA.Length + tempB.Length);

Logging.updateNotice(加载表...);
Logging.updateLog(开始加载文件,假的,真的,假的);
的for(int i = 0; I< tempA.Length;我++)
{
如果
{$(datasetA.Tables.Contains(TEMPA [I])!) b $ b addTableA(TEMPA [I]中,真);
++的百分比;
Logging.loadStatus(百分比,最大);
}
,否则
{
datasetA.Tables.Remove(TEMPA [I]);
addTableA(TEMPA [I]中,真);
++的百分比;
Logging.loadStatus(百分比,最大);
}
}
Logging.updateLog(说完加载文件,假的,真的,假的);
Logging.updateLog(开始加载文件B,假的,真的,假的);
的for(int i = 0; I< tempB.Length;我++)
{
如果
{$(datasetB.Tables.Contains(tempB [I])!) b $ b addTableB(tempB [I]中,真);
++的百分比;
Logging.loadStatus(百分比,最大);
}
,否则
{
datasetB.Tables.Remove(tempB [I]);
addTableB(tempB [I]中,真);
++的百分比;
Logging.loadStatus(百分比,最大);
}
}
Logging.updateLog(说完加载文件B,假的,真的,假的);


}

静态公共数据表getDataTableA()
{
datatableA = datasetA.Tables [Settings.textA]

返回datatableA;
}
静态公共数据表getDataTableB()
{
datatableB = datasetB.Tables [Settings.textB]
返回datatableB;
}

静态公共数据集getDataSetA()
{
返回datasetA;
}

静态公共数据集getDataSetB()
{
返回datasetB;
}

静态公共无效InitiateCopyProcessA()
{
的DataSet tablesA;
tablesA = DatabaseHandling.getDataSetA();

的foreach(在tablesA.Tables DataTable的表)
{
OverwriteTable(表,table.TableName);
Logging.updateLog(复制+ table.TableName +成功,假的,真的,假的);
}

}

静态无效OverwriteTable(数据表sourceTable会,串tableName值)
{
使用(VAR destConn =新的OleDbConnection(connectionstringA ))
使用(VAR destCmd =新的OleDbCommand(tableName值,destConn){=的CommandType} CommandType.TableDirect),使用(VAR destDA =新OleDbDataAdapter的(destCmd))

{
//因为我们使用了一个表,我们可以有CommandBuilder的
//生成适当的INSERT和DELETE使用SQL语句
(VAR destCmdB =新的OleDbCommandBuilder(destDA))
{
destCmdB.QuotePrefix =[; //报价保留列名
destCmdB.QuotePrefix =];
destDA.DeleteCommand = destCmdB.GetDeleteCommand();
destDA.InsertCommand = destCmdB.GetInsertCommand();

//从目的地的行,并删除
变种destTable =新的DataTable();
destDA.Fill(destTable);
的foreach(在destTable.Rows的DataRow DR)
{
dr.Delete();
}
destDA.Update(destTable);从源作为新增

//设置行,因此DataAdapter的将它们插入
的foreach(在sourceTable.Rows的DataRow DR)
{
dr.SetAdded() ;
}
destDA.Update(sourceTable会);
}
}
}



}
}

我只是想采取一个DataTable是在内存中,并写入到一个.mdb文件。我一直在试图为30小时以上这样做

I simply want to take a Datatable that is in memory and write it to a .MDB file. I have been attempting to do this for over 30 hours.

最新编辑:

好吧,增加了新的代码。我得到一个新的运行时错误:语法错误在FROM子句

Okay, added new code. I get a new run-time error: Syntax error in FROM clause.

代码:

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.QuotePrefix = "]";
            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);

            // Set rows from source as Added, so the DataAdapter will insert them
            foreach (DataRow dr in sourceTable.Rows)
            {
                dr.SetAdded();
            }
            destDA.Update(sourceTable); // !!! Run-time error: Syntax error in FROM clause. !!!
        }
    }
}



再次,它不工作。让我知道如果你需要更多的信息。

Once again, it does not work. Let me know if you need additional information.

推荐答案

尝试更换

using (var destCmdB = new OleDbCommandBuilder(destDA))
{
    destDA.DeleteCommand = destCmdB.GetDeleteCommand();
    destDA.InsertCommand = destCmdB.GetInsertCommand();
}



with

destDA.InsertCommand = new OleDbCommand("INSERT INTO `AdminUsers` (`UserName`, `Password`) VALUES (?, ?)");
destDA.DeleteCommand = new OleDbCommand("DELETE FROM `AdminUsers` WHERE (`ID` = ?)");
destDA.UpdateCommand = new OldDbCommand("UPDATE `AdminUsers` SET `UserName` = ?, `Password` = ? WHERE (`ID` = ?)");



当查询是有效的表结构。

Where the queries are valid to your table structure.

这篇关于怎样构建一个OleDbCommand的查询,这样我可以从一个.MDB拿表,并在另一个.MDB取代它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:17