数据未插入一个表(father_new)中,但显示在另一个表中(lab5_students)

时间:2017-10-19 11:34:14

标签: c# sql-server database winforms stored-procedures

我正在尝试将数据插入两个不同的表格中 一个是(father_new),另一个是(lab5_students),但数据插入lab5_student表中,不显示在father_new中。
我使用过存储过程,我不确定我在哪里做错了。

这是我保存父亲记录的代码

private void btn_submit_Click(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(tbox_Fname1.Text) == true || String.IsNullOrEmpty(tbox_fdob.Text) == true || String.IsNullOrEmpty(tbox_fcnic.Text) == true)
    {
        MessageBox.Show("Name,DOB,Cnic fields are compulsary to fill");
    }

    {
        SqlCommand cmd = new SqlCommand("Lab5task2", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Action", SqlDbType.NVarChar).Value = btn_Psubmit.Text;
        cmd.Parameters.Add("@FName", SqlDbType.NVarChar).Value = tbox_Fname1.Text;
        cmd.Parameters.Add("@FNum", SqlDbType.NChar).Value = tbox_fnum.Text;
        cmd.Parameters.Add("@FOccup", SqlDbType.NVarChar).Value = tbox_occupation.Text;
        cmd.Parameters.Add("@FEmail", SqlDbType.NVarChar).Value = tbox_femail.Text;
        cmd.Parameters.Add("@FDOB", SqlDbType.DateTime).Value = DateTime.ParseExact(tbox_fdob.Text, "d-M-yyyy", null);
        cmd.Parameters.Add("@Foffic_Num", SqlDbType.NChar).Value = tbox_offnum.Text;
        cmd.Parameters.Add("@FAge", SqlDbType.NChar).Value = tbox_fage.Text;
        cmd.Parameters.Add("@FState", SqlDbType.NVarChar).Value = tbox_Fstate.Text;
        cmd.Parameters.Add("@FIncome", SqlDbType.NChar).Value = tbox_fincome.Text;
        cmd.Parameters.Add("@FCnic", SqlDbType.NChar).Value = tbox_fcnic.Text;
                                conn.Open();
            cmd.ExecuteNonQuery();
            conn.Close();

    }
}

这是为了保存学生的记录

private void btn_Ssubmit_Click(object sender, EventArgs e)
{
    if (String.IsNullOrEmpty(tbox_sname.Text) == true || String.IsNullOrEmpty(tbox_sfname.Text) == true || String.IsNullOrEmpty(tbox_sid.Text) == true)
    {
        MessageBox.Show("Name,Father's Name and Id fields are compulsary to fill");
    }
    else
    {
        SqlCommand cmd = new SqlCommand("Lab5task2", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@Action", SqlDbType.NVarChar).Value = btn_Ssubmit.Text;
        cmd.Parameters.Add("@Sname", SqlDbType.NVarChar).Value = tbox_sname.Text;
        cmd.Parameters.Add("@Sid", SqlDbType.NChar).Value = tbox_sid.Text;
        cmd.Parameters.Add("@Sfather_name", SqlDbType.NVarChar).Value = tbox_sfname.Text;
        cmd.Parameters.Add("@Sphone", SqlDbType.NVarChar).Value = tbox_sphone.Text;
        cmd.Parameters.Add("@Semail", SqlDbType.NVarChar).Value = tbox_semail.Text;
        cmd.Parameters.Add("@Saddress", SqlDbType.NVarChar).Value = tbox_saddress.Text;
        cmd.Parameters.Add("@Sage", SqlDbType.NVarChar).Value = tbox_sage.Text;

        if (rbtn_female.Text == "Female")
        {
            cmd.Parameters.Add("@Sgender", SqlDbType.NVarChar).Value = rbtn_female.Text;
        }
        else if (rbtn_male.Text == "Male")
        {
            cmd.Parameters.Add("@Sgender", SqlDbType.NVarChar).Value = rbtn_male.Text;
        }

        cmd.Parameters.Add("@Ssemester", SqlDbType.NVarChar).Value =      tbox_ssemester.Text;
        cmd.Parameters.Add("@Suniversity", SqlDbType.NVarChar).Value = tbox_suni.Text;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close(); 

这是我的存储过程

create procedure [dbo].[lab5task2]
(
      -- For Parent data
      @FName nvarchar(50)=null,
      @FNum nchar(50) =null,
      @FOccup nvarchar(50)=null,
      @FEmail nvarchar(50)=null,
      @FDOB datetime=null,
      @Foffic_Num nchar(50)=null,
      @FAge nchar(50)=null,
      @FState nvarchar(50)=null,
      @FIncome nchar(50)=null,
      @FCnic nchar(50)=null,
      -- For Student data
      @Sname nvarchar(50)=null,
      @Sid nchar(50)=null,
      @Sfather_name nvarchar(50)=null,
      @Sphone nvarchar(50)=null,
      @Semail nvarchar(50)=null,
      @Saddress nvarchar(50)=null,
      @Sage nvarchar(50)=null,
      @Sgender nvarchar(50)=null,
      @Ssemester nchar(50)=null,
      @Suniversity nvarchar(50)=null,
      @Action nvarchar(50)=null
)

continued procedure

1 个答案:

答案 0 :(得分:0)

问题在于存储过程的这一部分。

if @Action = 'Delete All'
delete from lab5_students;
delete from father_new;

它没有做你认为它做的事情。我怀疑您认为if适用于两者删除行。但它只适用于第一个。你实际上在做:

if @Action = 'Delete All'
    delete from lab5_students;

delete from father_new; // i.e. always run this

因此,您需要使用BEGIN ... END

if @Action = 'Delete All'
BEGIN
    delete from lab5_students;
    delete from father_new;
END

如果DELETE是“全部删除”,这将确保第二个@Action仅执行