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

时间:2014-02-28 00:01:22

标签: c# sql-server stored-procedures

我正在尝试使用存储过程将值插入到两个表中。

这是我用过的存储过程

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

这是我的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;


            }
    }

    }

错误弹出“将数据类型nvarchar转换为int时出错”并且仅将数据写入Employee表。

DatePics.Emp_ID是第二个表的外键

Employee.ID是主键。假设这些表之间存在一对一的关系。如何将自动生成的值从Employee.ID传递到C#中的DatePics.Emp_ID?

由于

2 个答案:

答案 0 :(得分:1)

这里有几个问题。

首先,你不需要这个:

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

@Emp_ID不是存储过程中的参数,并且您不需要它,因为员工ID是通过身份生成的(我假设您的Employee表中的id列是PK标识列)。

其次,在您的存储过程中,您没有将父表id作为外键插入到子表中;您正在抓取刚刚插入的员工的ID并将其设置为@ID变量,但是您将(未填充的)@Emp_ID变量作为外键插入子表中。这样做:

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

答案 1 :(得分:0)

我认为您不必从代码中传递@Emp_ID,

即尝试在代码中注释com.Parameters.AddWithValue("@Emp_ID", "@ID");

因为您正在Emp_ID表中插入DatePics来自@ID(在您的存储过程中声明)

还尝试在Transaction范围内编写整个存储过程。