当前位置: 首页 > 面试题库 >

如何解决:对于不返回任何键列信息的SelectCommand,不支持为DeleteCommand动态生成SQL。

云育
2023-03-14
问题内容

我的代码有效。复制大约10张表后,出现错误。Dynamic SQL generation for the DeleteCommand is not supported against a SelectCommand that does not return any key column information.好的,我知道我需要生成一个主键。但是为什么我可以复制10个左右的表,然后得到错误消息。每行都必须返回一个主键吗?如果某行没有主键,我该如何生成一个主键?

这是我的代码:

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();
            int percentage = 0;
            int maximum = (tablesA.Tables.Count);

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

        }

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

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



        }          
    }

Google在这方面对我不是很有帮助。请提供一个编码示例。


问题答案:

没有编码示例真的可以工作。您必须从表中确定表的唯一标识符是什么。如果没有主键(或者至少没有唯一索引),那么您就不走运了。

解决此问题的唯一方法是自己创建一个动态命令,该命令将表中的每个值与要删除的记录进行比较。如果它们相同,则将其删除。

但是,由于表中可能存在具有相同值的多行,因此存在从表中删除多行的风险。



 类似资料:
  • 当我运行HelloWorld时,它会返回: “java.lang.UnsupportedClassVersionError:HelloWorld已由java运行时的最新版本(类文件版本52.65535)编译,此版本的java运行时仅识别52.0以下的类文件版本。” 我该如何解决这个问题?

  • 问题内容: 我无法连接到SQL Server,项目的连接字符串为: 我收到此错误: 不支持的关键字:“元数据” 如何解决此错误? 问题答案: 该连接字符串仅受实体框架支持。(公平地说,关键字“ entities”在键名中!)如果要在ADO原始连接中使用连接字符串,请除去字符串部分之外的所有内容,包括s: 更改为:

  • 我使用xampp 32位PHP7.1。1. 我在我的Windows上安装了mongodb 3.4.2(Windows 8 Pro 64位) 我还安装了驱动程序(php_mongodb.dll) 我从这里得到:https://pecl.php.net/package/mongodb/1.2.5/windows (7.1线程安全(TS)x86) 但是,当我的系统执行时,仍然存在错误: 哎呀,好像出了什

  • 我正在使用mySQL数据库,该表有一个由数据库引擎生成的UUID格式的主键。我使用Spring framework JdbcTemplate(https://docs.Spring.io/Spring/docs/current/javadoc-api/index.html?org/springframework/jdbc/core/JdbcTemplate.html)来执行所有操作。但是在插入时,

  • 问题内容: 搜索时,Elasticsearch返回包含各种元信息的数据结构。 实际结果集包含在从数据库返回的JSON结果内的“ hits”字段中。 Elasticsearch是否有可能仅返回所需的数据(然后是“ hits”字段的内容)而没有嵌入所有其他元数据中? 我知道我可以将结果解析为JSON并提取出来,但是我不希望复杂性,麻烦和性能下降。 谢谢! 这是Elasticsearch返回的数据结构的