如何用Asp.net C#在sqlserver中激发两个insert命令

本文关键字:两个 命令 insert net Asp 何用 sqlserver | 更新日期: 2023-09-27 17:57:38

if (dpsessional.SelectedItem.Text.ToString().Equals("1"))
{
if (lengthofbatch == 1)
              {
                 com = new SqlCommand("insert into Attendence_Stu_Main(st_id,st_rollno,st_name,branch_name,sem_no,batch_year,batch_name,scode,sess_no,attendence,total) values (@Stid,@Strollno,@Stname,@Stbranch,@Ssemno,@Syear,@Sbatch,@Scode,@Ssessno,@Satt,@Stotal)", con);
              }
              else
              {
                  if (lbsubjecttype.Text.Equals("L"))
                  {
                      com = new SqlCommand("insert into Attendence_Stu_Main(st_id,st_rollno,st_name,branch_name,sem_no,batch_year,batch_name,scode,sess_no,attendence,total) values (@Stid,@Strollno,@Stname,@Stbranch,@Ssemno,@Syear,firstchar,@Scode,@Ssessno,00,00)", con);

                      com = new SqlCommand("insert into 

Attendance_Stu_MainL1(st_id、st_rollno、st_name、branch_name、sem_no、batch_year、batch_name、scode、 sess_no、出席人数、总数(值

(@Stid,@Strollno,@Stname,@Stbranch,@Ssemno,@Syear,@Sbatch,@Scode,@Sessno,@Satt,@Stotal(",

con(;}其他

                  {

com=新的SqlCommand("插入

Attendance_Stu_MainL1(st_id、st_rollno、st_name、branch_name、sem_no、batch_year、batch_name、scod

e、 sess_no、出席人数、总数(值

(@Stid,@Strollno,@Stname,@Stbranch,@Ssemno,@Syear,@Sbatch,@Scode,@Sessno,@Satt,@Stotal(",

con(;

                  }
                }
          }
               com.Parameters.AddWithValue("@Stid", g1.Cells[0].Text);

com.Parameters.AddWithValue("@Strollno",g1.Cells[1].Text(;com.Parameters.AddWithValue("@Stname",g1.Cells[2].Text(;

com.Parameters.AddWithValue("@Stbranch",g1.Cells[3].Text(;

            com.Parameters.AddWithValue("@Ssemno", g1.Cells[4].Text);
            com.Parameters.AddWithValue("@Syear", g1.Cells[5].Text);
            com.Parameters.AddWithValue("@Sbatch", label1);
            com.Parameters.AddWithValue("@Scode", label2);
            com.Parameters.AddWithValue("@Ssessno", label3);
            com.Parameters.AddWithValue("@Satt", textbox);
            com.Parameters.AddWithValue("@Stotal", textbox1);


            com.ExecuteNonQuery();

            con.Close();
        }
        catch (SqlException ex)
        {
            Label5.Text = ex.Message;
            Label5.Visible = true;
        }


    }
}

com=新的SqlCommand("插入Attendance_Stu_Main(st_id,st_rollno,st_name,branch_name,sem_no,batch_year,batch_name,scode,sess_no、出席人数、总数(值(@Stid,@Strollno,@Stname,@Stbranch,@Ssemno,@Syear,firstchar,@Scode,@sessno,00,00(",con(;

com=新的SqlCommand("插入

Attendance_Stu_MainL1(st_id、st_rollno、st_name、branch_name、sem_no、batch_year、batch_name、scod

e、 sess_no、出席人数、总数(值

(@Stid,@Strollno,@Stname,@Stbranch,@Ssemno,@Syear,@Sbatch,@Scode,@Sessno,@Satt,@Stotal(",

con(;

从插入命令的2,只有一个被激发,另一个没有被激发。。我试过使用

调试,它显示了清晰的遍历,但不能存储在表

如何用Asp.net C#在sqlserver中激发两个insert命令

尽管这是人类中最难理解的问题,但我会尝试回答这个问题。如果你能让你的问题变得可读和简洁,并让我们知道你的期望,那么就去做吧。

您不断地改写您的命令。

com = new SqlCommand("insert into Attendence_Stu_Main(st_id,st_rollno,st_name,branch_name,sem_no,batch_year,batch_name,scode,sess_no,attendence,total) values (@Stid,@Strollno,@Stname,@Stbranch,@Ssemno,@Syear,firstchar,@Scode,@Ssessno,00,00)", con);

然后,在没有向数据库提交此更改的情况下,您正在覆盖它:

com = new SqlCommand("insert into Attendence_Stu_MainL1(st_id,st_rollno,st_name,branch_name,sem_no,batch_year,batch_name,scode,sess_no,attendence,total) values (@Stid,@Strollno,@Stname,@Stbranch,@Ssemno,@Syear,@Sbatch,@Scode,@Ssessno,@Satt,@Stotal)", 

我会让它们成为两个不同的SqlCommand实例。

只是附带说明一下,数据库表&您正在进行的列命名方案,不太美观。