简体   繁体   English

如何使用存储过程将主键值传递给C#中的外键

[英]How to pass primary key value to foreign key in c# using stored procedure

I'm trying to insert values into two tables using stored procedure. 我正在尝试使用存储过程将值插入两个表中。

This is stored procedure I've used 这是我使用过的存储过程

ALTER PROCEDURE [dbo].[insert_emp_pics]
@EmpName nvarchar(100), 
@Nationality nvarchar(30), 
@PassportPic nvarchar(100), 
@Pic nvarchar(100)
AS
Begin
set nocount on;
DECLARE @ID int, 
@Emp_ID int
insert into Employee (EmpName,Nationality)
values (@EmpName,@Nationality)
set @ID = SCOPE_IDENTITY();
insert into DatePics 
(Emp_ID,PassportPic,Pic)
values
(@Emp_ID,@PassportPic ,@Pic)
set @ID_Pics  = SCOPE_IDENTITY();
 end

And this is my C# statement 这是我的C#语句

        private void Savebtn_Click_1(object sender, EventArgs e)
    {


        if (Nametxt.Text == "" || Nationalitydrp.SelectedItem == null )
        {
            nameerr.Text = ("*");
            nationalityerr.Text = ("*");

        }
        else
        {               

            SqlCommand com = new SqlCommand();                
            com.Connection = db.con;
            com.CommandType = CommandType.StoredProcedure;
            com.CommandText = "insert_emp_pics ";
            com.Parameters.AddWithValue("@EmpName", Nametxt.Text);
            com.Parameters.AddWithValue("@Nationality", Nationalitydrp.SelectedItem);                
            com.Parameters.AddWithValue("@Pic", picpath.Text);
            com.Parameters.AddWithValue("@PassportPic", pppath.Text);
            //These are the relation between two tables
            com.Parameters.AddWithValue("@Emp_ID", "@ID");
            db.con.Open();
            int x = com.ExecuteNonQuery();
            db.con.Close();
            if (x > 0)
            {
                MessageBox.Show("Welcome " + Nametxt.Text + " in our organization" );

                nameerr.Visible = false;              
                nationalityerr.Visible = false;                   
                Nametxt.Text = null;
                Nationalitydrp.SelectedItem = null;                   
                picpath.Text = null;
                pppath.Text = null;


            }
    }

    }

The error pop up "error converting data type nvarchar to int" and it write data to Employee table only. 该错误弹出“将数据类型nvarchar转换为int时出错”,并且仅将数据写入Employee表。

The DatePics.Emp_ID is the Foreign Key of the second table DatePics.Emp_ID是第二个表的外键

Employee.ID is the Primary Key. Employee.ID是主键。 It is suppose that there is one to one relationship between these tables. 假设这些表之间存在一对一的关系。 How to pass the auto generated value from Employee.ID to DatePics.Emp_ID in C# ? 如何将自动生成的值从Employee.ID传递到C#中的DatePics.Emp_ID?

Thanks 谢谢

You have a couple of issues here. 您在这里有几个问题。

First, you don't need this: 首先,您不需要:

com.Parameters.AddWithValue("@Emp_ID", "@ID");

@Emp_ID is not a parameter in your stored procedure, and you don't need it to be, since employee id's are generated via identity (I'm assuming your id column in the Employee table is a PK identity column). @Emp_ID不是您的存储过程中的参数,并且您不必使用它,因为员工ID是通过身份生成的(我假设Employee表中的id列是PK身份列)。

Second, in your stored procedure, you are not inserting the parent table id into the child table as the foreign key like you are intending; 其次,在存储过程中,您不会像预期的那样将父表ID作为子项插入子表中; you're grabbing the id of the just-inserted employee and setting it to the @ID variable, but then you are inserting the (unpopulated) @Emp_ID variable as the foreign key in your child table. 您正在获取刚插入的员工的ID并将其设置为@ID变量,但是随后您将(未填充的)@Emp_ID变量作为外键插入子表中。 Do this instead: 改为这样做:

ALTER PROCEDURE [dbo].[insert_emp_pics]
@EmpName nvarchar(100), 
@Nationality nvarchar(30), 
@PassportPic nvarchar(100), 
@Pic nvarchar(100)
AS
Begin
set nocount on;
DECLARE @ID int
insert into Employee (EmpName,Nationality)
values (@EmpName,@Nationality)
set @ID = SCOPE_IDENTITY(); -- This is the employee id that was just inserted
insert into DatePics 
(Emp_ID,PassportPic,Pic)
values
(@ID,@PassportPic ,@Pic) -- Use the just-inserted employee id to insert as a foreign key into the child table
set @ID_Pics  = SCOPE_IDENTITY();
 end

I don't think you have to pass @Emp_ID from your code, 我认为您不必从代码中传递@Emp_ID,

ie try commenting out com.Parameters.AddWithValue("@Emp_ID", "@ID"); 即尝试注释掉com.Parameters.AddWithValue("@Emp_ID", "@ID"); from your code. 从您的代码。

Because you are inserting Emp_ID in DatePics table from @ID (declared in your stored proc.) 因为你将Emp_IDDatePics从@ID表(在你的存储过程声明。)

Also try to write the whole stored proc inside Transaction scope. 也尝试将整个存储的proc写入Transaction范围内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM