INSERT Satatement与FOREIGN KEY Contraint融为一体。请帮忙!!!! [英] The INSERT Satatement Confliceted with the FOREIGN KEY Contraint. Please help!!!!

查看:61
本文介绍了INSERT Satatement与FOREIGN KEY Contraint融为一体。请帮忙!!!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个关于快递服务的项目。当我尝试在运行时输入值时出现此错误。我有2个插入语句,一个用于发送方,另一个用于Receiver。但是在Sender和LoginID的insert语句中存在问题。实际上,我想在发件人表中添加已登录的用户,这样我就可以看到,登录的用户做了多少条目。首先,我使用以下语句从登录表中获取LoginID

I am creating a project on courier service. I got this error when I tried to enter values on run time. I have 2 insert statements, one for sender and the other for Receiver. But the problem exists in insert statement for Sender and in LoginID. Actually, I want to add the logged user in "Sender" Table, so that I can see, how much entries, the logged user did. First, I am getting LoginID from the "Login" table using this statement

log_id = Convert.ToInt32(dt.Rows[0][0]);



其中log_id是整数变量。和dt是数据表名称



它完美无缺。我已经通过使用messagebox看到了它。



然后我创建了一个公共变量以在其他表单上使用它。


Where log_id is an integer variable. and dt is data table name

It works perfectly. I have seen it by using messagebox.

And then I created a Public Variable to use it on other forms.

public int logId
       {
           get { return log_id; }
       }





然后我在我的数据输入表单上调用了这个变量。



Then I called this variable on my data entry form like this.

public StdExpIntl(int logId)
        {
            InitializeComponent();
            logID = logId;
        }



这是我的插入语句插入发件人(CourierNo,LoginID,SenderName)值('+ cNo.Text +',' + logID +','+ Name1.Text +'),con);



这不是完整的声明,但我想它可以现在。



登录表


And this is my Insert Statement Insert into Sender(CourierNo,LoginID,SenderName) values ('" + cNo.Text + "','"+logID+"', '" + Name1.Text + "')", con);

This is not the full statement, but I guess it is ok for now.

Login Table

<pre>LoginID	numeric(18, 0)	NOT NULL
StaffID	varchar(15)	NOT NULL
username	varchar(10)	NOT NULL
password	varchar(10)	NOT NULL





发件人表





Sender Table

CourierNo	varchar(15)	NOT NULL
LoginID	numeric(18, 0)	NOT NULL
SenderName	text	NOT NULL
SenderNIC	numeric(18, 0)	NOT NULL
SenderCountry	text	NOT NULL
SenderState	text	NOT NULL
SenderCity	text	NOT NULL







e rror在LoginID中,请有人帮帮我。




The error is in LoginID, please someone help me.

推荐答案

我认为Sender表中的LoginID是一个外键,链接到Login Table中的主键LoginID。发生错误是因为您尝试将LoginID值插入到Sender表中,但该值在登录表中不存在。这是工作中的外键约束。

除了控制可以存储在外键表中的数据外,FOREIGN KEY约束还通过强制引用完整性来控制对主键表中数据的更改,从而无法对主键中的数据进行更改表,如果这些更改使外键表中数据的链接无效。

了解更多:主键和外键约束 [ ^ ]
I think the LoginID in Sender table is a foreign key linking to the primary key LoginID in Login Table. The error occurred because you were trying to insert a LoginID value into Sender table but that value had not existed in the Login Table. That was foreign key constraint at work.
Besides controlling the data that can be stored in the foreign key table, FOREIGN KEY constraint also controls changes to data in the primary key table by enforcing referential integrity whereby changes cannot be made to data in the primary key table if those changes invalidate the link to data in the foreign key table.
Read more: Primary and Foreign Key Constraints[^]


在您有一个插入语句尝试将数据插入到不存在外键的表中时,此错误是IMO适当的。

首先将数据插入主表,然后插入子表。
In you have an insert statement that is trying to insert data into a table for which a foreign key does not exists, this error is IMO appropriate.
First insert data into the main table and then into the child table.


谢谢,但我找到了解决方案,我在构造函数中调用了logid但是我没有将它发送到stdExpIntl表主表。但是这样做之后就有用了。

Thanks, but I found the solution, that I was calling logid in the constructor but I didn't send that to the stdExpIntl table from the main table. But it worked, after doing this.
StdExpIntl std = new StdExpIntl(logid);
std.show();


这篇关于INSERT Satatement与FOREIGN KEY Contraint融为一体。请帮忙!!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆