C#WinForms SQL Server CE - 无法插入外键值,因为不存在相应的主键值

时间:2015-07-20 14:42:39

标签: c# database winforms foreign-keys sql-server-ce

我收到此错误消息:

  

无法插入外键值,因为不存在相应的主键值。 [外键约束名称= FK_t_TypeSystem_t_TypeConfig]

情况如下:
1)我通过Sql Server CE在本地创建表格。 (使用外键,顺序正确)
2)然后我执行SELECT * FROM [table]语句(来自服务器DB)
3)我通过SqlCeBulkCopy将语句结果插入到本地表中。

这里发生错误。我不明白为什么,我检查了创建表顺序,添加约束顺序。我还试图通过ALTER TABLE而不是在Create语句中添加约束。插入之前和之后。但它总是一样的:它给我一个错误。
我在网上搜索了一个解决方案,但它处理了插入顺序。在我的情况下,订单是正确的。所以它没有解决我的问题。

这是我的代码:

if (File.Exists(LOCAL_SDF_FILE))
{
    File.Delete(LOCAL_SDF_FILE);
}

SqlCeEngine engine = new SqlCeEngine(LOCAL_CONN_STRING);
engine.CreateDatabase();
engine.Dispose();

SqlCeConnection localCnx = null;

try
{
    localCnx = new SqlCeConnection(LOCAL_CONN_STRING);
    localCnx.Open();

    SqlCeCommand localCmd = localCnx.CreateCommand();

    // Table t_TypeConfig
    localCmd.CommandText = @"CREATE TABLE t_TypeConfig(
                                TypeConfig_ID int IDENTITY(1,1) NOT NULL,
                                TypeConfig_Name nvarchar(50) NOT NULL,
                                TypeConfig_IsVisible bit NOT NULL,
                                CONSTRAINT pk_TypeConfigID PRIMARY KEY (TypeConfig_ID)
                                )";
    localCmd.ExecuteNonQuery();

    // Table t_TypeSystem
    localCmd.CommandText = @"CREATE TABLE t_TypeSystem(
                                TypeSystem_ID int IDENTITY(1,1) NOT NULL,
                                TypeSystem_Name nvarchar(50) NOT NULL,
                                TypeSystem_ConfigID int NOT NULL,
                                TypeSystem_IsVisible bit NOT NULL,
                                CONSTRAINT PK_t_TypeSystem PRIMARY KEY (TypeSystem_ID),
                                CONSTRAINT FK_t_TypeSystem_t_TypeConfig FOREIGN KEY (TypeSystem_ConfigID)
                                REFERENCES t_TypeConfig(TypeConfig_ID)
                                )";
    localCmd.ExecuteNonQuery();

    List<string> TablesName = new List<string>();
    TablesName.Add("t_TypeConfig");
    TablesName.Add("t_TypeSystem");

    using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))
    {
        try
        {
            sourceCnx.Open();

            SqlCommand SourceCmd = sourceCnx.CreateCommand();

            // Insert data from server Wibe DB to local Wibe DB.
            for (int i = 0; i < TablesName.Count; i++)
            {
                string table = TablesName[i];

                SourceCmd.CommandText = String.Format("Select * FROM {0}", table);

                SqlDataReader SqlReader = SourceCmd.ExecuteReader();

                DataTable SqlReaderDataTable = new DataTable();
                SqlReaderDataTable.Load(SqlReader);

                using (SqlCeBulkCopy bulkCopy = new SqlCeBulkCopy(LOCAL_CONN_STRING))
                {
                    bulkCopy.DestinationTableName = table;

                    try
                    {
                        // Write from the source (DB server) to the destination (local wibe)
                        bulkCopy.WriteToServer(SqlReaderDataTable);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "An error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        SqlReader.Close();
                    }
                }
            }
            MessageBox.Show("The data has been successfully loaded into local storage.", "Process complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString(), "An error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        finally
        {
            sourceCnx.Close();
        }
    }

}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString(), "An error occurred", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
    localCnx.Close();
}

这是抛出上述错误的行: bulkCopy.WriteToServer(SqlReaderDataTable);

发生错误时,目标表为t_TypeSystem

我希望我能提供所有需要帮助的信息。

谢谢,

地狱猫

0 个答案:

没有答案