制作一个没有约束的数据,但包含所有数据

发布于 2025-01-29 02:00:24 字数 3948 浏览 1 评论 0原文

我需要的

我希望创建一个具有所有相同数据的数据表,但是与任何表都没有任何约束。我将在一堆桌子上进行内在加入,以合并我需要的所有数据。这样进行:

  1. 将有几个测试条目。每个人都会收集 testStep 。每个 test 中总会有 testStep 的集合。两张表的ID都不能为null。
  2. 可能还有其他数据将连接到 test 的以及 testStep 。有时, test 的收集将包含 testparam 的集合,该将链接到每个 test ,但并非总是如此。 testParam 具有不能无效的ID字段。
  3. 有时,每个 testStep 也会收集一系列参数。有时不是。 testStepparam 的ID字段无法为null。
  4. 有时,每个 testStep 都会收集屏幕截图。再次,有时不是。 屏幕截图具有无法为空的ID字段。 如果我在SQLite浏览器中执行所有这些表格,并说没有任何连接到某个 teststep ,然后它只会说 null 在该单元格(下面键入SQL)。如果我使用下面的代码做完全相同的事情,则会引发异常。当我使用 datatable.load(sqlitedatareader)时,如何使其忽略约束?

我拥有的

是我从sqlite数据库中获取数据的方法,该数据库效果很好:

private static List<DataTable> HitDB(params string[] sqls)
    {
        List<DataTable> ldt = new List<DataTable>();

        using (SqliteConnection cnn = new SqliteConnection(LoadConnectionString()))
        {
            cnn.Open();
            foreach (string sql in sqls)
            {
                DataTable dt = new DataTable();
                SqliteCommand cmd = new SqliteCommand(sql, cnn);
                SqliteDataReader sqldr = cmd.ExecuteReader();

                //this is what I would have hoped would have fixed this.
                dt.Constraints.Clear();                    

                try
                {
                    //This is where the error is thrown.
                    dt.Load(sqldr);
                }
                catch (ConstraintException exc)
                {
                    Wr.Err($"CONSTRAINT EXCEPTION CAUGHT: \n{exc}\n\n");
                }
                catch (Exception exc)
                {
                    Wr.Err($"EXCEPTION CAUGHT: {exc}\n\n");
                }
                ldt.Add(dt);
            }

            cnn.Close();
        }

        return ldt;
    }

对于SQL,我有:

string sql = $"SELECT * FROM Test t " +
$"INNER JOIN TestStep ts " +
$"ON t.ID = ts.Test_ID " +
$"LEFT OUTER JOIN Test_Param tp " +
$"ON t.ID = tp.Test_ID " +
$"LEFT OUTER JOIN TestStep_Param tsp " +
$"ON ts.ID = tsp.TestStep_ID " +
$"LEFT OUTER JOIN ScreenShot ss " +
$"ON ts.ID = ss.TestStep_ID "

这是我在sqlite浏览器中键入它的结果,以及我想在C#中重新创建的内容: table

我've尝试

我已经尝试执行dt.constraints.clear();在加载 sqlitedatareader 之前,但这一切都没有改变。

例外

CONSTRAINT EXCEPTION CAUGHT:
System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
   at System.Data.DataTable.EnableConstraints()
   at System.Data.DataTable.set_EnforceConstraints(Boolean value)
   at System.Data.DataTable.EndLoadData()
   at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
   at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption)
   at Automonom.AutoDB.HitDB(String sql) in C:\VS Projects\Automonom\AutoDB.cs:line 555

WHAT I NEED

I'm looking to create a DataTable that has all of the same Data, but none of the constraints associated with any table. I will do INNER JOIN's on a bunch of table to coalesce all of the data that I need. It goes as such:

  1. There will several Test entries. Each one will have a collection of TestStep. There will always be a collection of TestStep's within each Test. Both tables have ID's that can't be NULL.
  2. There may be other pieces of data that will be connected to both Test's as well as TestStep's. Sometimes, Test's will have a collection of TestParam's that will be linked to each Test, but not always. TestParam has an ID field that can't be NULL.
  3. Sometimes there will be a collection of parameters for each TestStep as well. Sometimes not. TestStepParam has an ID field that can't be NULL.
  4. Sometimes there will be a collection of ScreenShot's for each TestStep. Again, sometimes not. ScreenShot has an ID field that can't be NULL.
    If I execute the SQL where I join all of these tables within the SQLite browser, and say there aren't any ScreenShot's that are connected to a certain TestStep, then it will just say NULL in that cell (SQL is typed below). If I do the exact same thing using the code below, it throws an exception. How do I make it ignore constraints when I'm using DataTable.Load(SqLiteDataReader)?

WHAT I HAVE

Below is the method that I have for getting the data from a SQLite database which works perfectly fine:

private static List<DataTable> HitDB(params string[] sqls)
    {
        List<DataTable> ldt = new List<DataTable>();

        using (SqliteConnection cnn = new SqliteConnection(LoadConnectionString()))
        {
            cnn.Open();
            foreach (string sql in sqls)
            {
                DataTable dt = new DataTable();
                SqliteCommand cmd = new SqliteCommand(sql, cnn);
                SqliteDataReader sqldr = cmd.ExecuteReader();

                //this is what I would have hoped would have fixed this.
                dt.Constraints.Clear();                    

                try
                {
                    //This is where the error is thrown.
                    dt.Load(sqldr);
                }
                catch (ConstraintException exc)
                {
                    Wr.Err(
quot;CONSTRAINT EXCEPTION CAUGHT: \n{exc}\n\n");
                }
                catch (Exception exc)
                {
                    Wr.Err(
quot;EXCEPTION CAUGHT: {exc}\n\n");
                }
                ldt.Add(dt);
            }

            cnn.Close();
        }

        return ldt;
    }

for the sql, I have:

string sql = 
quot;SELECT * FROM Test t " +
quot;INNER JOIN TestStep ts " +
quot;ON t.ID = ts.Test_ID " +
quot;LEFT OUTER JOIN Test_Param tp " +
quot;ON t.ID = tp.Test_ID " +
quot;LEFT OUTER JOIN TestStep_Param tsp " +
quot;ON ts.ID = tsp.TestStep_ID " +
quot;LEFT OUTER JOIN ScreenShot ss " +
quot;ON ts.ID = ss.TestStep_ID "

This is the result when I type it in the SQLite Browser, as well as exactly what I'd like to recreate in C#: TABLE

WHAT I'VE TRIED

I've tried executing dt.Constraints.Clear(); before loading in the SqLiteDataReader but that changes nothing.

THE EXCEPTION

CONSTRAINT EXCEPTION CAUGHT:
System.Data.ConstraintException: Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.
   at System.Data.DataTable.EnableConstraints()
   at System.Data.DataTable.set_EnforceConstraints(Boolean value)
   at System.Data.DataTable.EndLoadData()
   at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)
   at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.Common.LoadAdapter.FillFromReader(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)
   at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption, FillErrorEventHandler errorHandler)
   at System.Data.DataTable.Load(IDataReader reader, LoadOption loadOption)
   at Automonom.AutoDB.HitDB(String sql) in C:\VS Projects\Automonom\AutoDB.cs:line 555

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请远离我 2025-02-05 02:00:24

如果您真的想将测试存储在数据库中,我建议您避免这种情况,因为:

  1. 不支持版本控件

,请想象您的经理要求您测试新的FEAUTURE。这将涉及:重构现有测试,添加新的测试,删除过期的测试,记录测试的行为,并将其与某些任务跟踪器链接链接。您准备支持所有情况吗?

  1. 很难迁移

SQL更难更改:它会影响现有数据表架构,查询。您确定您的架构每年更改一次吗?

  1. 复制问题问题

测试是非常动态的结构,在这里和那里进行略有更改,而不是扩展数据库架构,复制paste测试更便宜。

  1. 团队开发/测试

如果您有多个功能,则多个CI/CD管道如何运行新测试不影响生产测试?您准备好到开发人员可能会破坏所有人和生产的管道吗?

只需将您的测试存储在文件中,作为文档中的git。

If you really want to store tests in database, I suggest you to avoid this because:

  1. No support of version control

Imagine your managers ask you to test new feauture. This will involve: refactoring of existing tests, adding new tests, removing expired ones, documenting behavior of tests, linking them with some task tracker link. Do you ready to support all of this cases?

  1. Hard to migrate

SQL is way harder to change: it affects existing data tables schema, queries. Are you sure your schema change once a year?

  1. Copy-paste problem

Tests is very dynamic structure, it is cheapier to copy-paste test with slight changes here and there, rather than extending database schema.

  1. Team development/testing

If you have multiple features, multiple CI/CD pipelines how you can run your new tests not affecting production tests? You ready to situation where developer can potentially break pipeline for everyone and for production?

Just store your tests in files, in GIT as documents.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文