C# 正在执行但未在db c中反映的事务命令#

C# 正在执行但未在db c中反映的事务命令#,c#,rollback,C#,Rollback,下面的代码用于插入学员详细信息并运行3个存储过程(全部在cmd1上)。我用过 事务和回滚。所有4个ExecuteOnQuery()都已执行,但数据库中未反映任何内容。 有人能解释什么是错误的,或者为什么它不影响数据库吗 con.Open(); SqlCommand cmd1 = con.CreateCommand(); SqlTransaction transaction1; transaction1 = con.BeginTransaction("Save Update Student");

下面的代码用于插入学员详细信息并运行3个存储过程(全部在cmd1上)。我用过 事务和回滚。所有4个ExecuteOnQuery()都已执行,但数据库中未反映任何内容。 有人能解释什么是错误的,或者为什么它不影响数据库吗

con.Open();
SqlCommand cmd1 = con.CreateCommand();
SqlTransaction transaction1;
transaction1 = con.BeginTransaction("Save Update Student");
cmd1.Connection = con;
cmd1.Transaction = transaction1;
try
{
    //sp to autogenerate student code in system..
    cmd1.CommandText = "sp_AutoGenerateStudentCode";
    cmd1.CommandType = CommandType.StoredProcedure;

    cmd1.Parameters.Add("@std", SqlDbType.VarChar).Value = cb_std.SelectedItem.ToString();
    cmd1.Parameters.Add("@div", SqlDbType.VarChar).Value = cb_div.SelectedItem;
    cmd1.Parameters.Add("@Rollno", SqlDbType.Int).Value = txt_roll.Text;
    cmd1.Parameters.Add("@ReturnValue", SqlDbType.VarChar).Value = txt_name.Text;
    cmd1.ExecuteNonQuery();

    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText = "insert into StudentMaster(GrNo,Name,DOB,Std,Div,RollNo,MobileNo,Address,TelNo,FathersName,FathersProfession,MothersName,MothersProfession,Age,Year,status,DOE,BookNo,FeesStatus,FthrsQlfction,FthrsOfcAdd,FthrsPhone,MthrsPhone,MthrsOfcAdd,MthrsQlfction,Bloodgrp,caste,Nationality,MotherTongue,PreviousSchool,Religion,height,weight,sex,SCode,EmailId)values ('" + txt_Grno.Text + "','" + txt_name.Text + "',@DOB,'" + cb_std.SelectedItem + "','" + cb_div.SelectedItem + "','" + txt_roll.Text + "','" + txt_mobile.Text + "','" + Rtxt_ResiAdd.Text + "','" + txt_Phone.Text + "','" + txt_fname.Text + "','" + txt_fOccu.Text + "','" + txt_mName.Text + "','" + txt_mOccu.Text + "','" + txt_Age.Text + "',getDate(),'" + cb_status.SelectedItem + "',@DOE,'" + txt_bookno.Text + "','" + cb_feestat.SelectedItem + "','" + txt_fQualificatn.Text + "','" + Rtxt_fOfcAdd.Text + "','" + txt_fPhone.Text + "','" + txt_mPhone.Text + "','" + Rtxt_mOfcAdd.Text + "','" + txt_mQualificatn.Text + "','" + cb_BldGrp.SelectedItem + "','" + txt_caste.Text + "','" + txt_Nationality.Text + "','" + txt_MthrTng.Text + "','" + txt_PrevSchool.Text + "','" + txt_Relgn.Text + "','" + masktb_hgt.Text + "','" + masktb_wgt.Text + "','" + cb_Gender.SelectedItem + "','scode','" + txt_email.Text + "')";

    cmd1.Parameters.Add("@DOE", SqlDbType.DateTime).Value = dateTimePicker1.Value;
    cmd1.Parameters.Add("@DOB", SqlDbType.DateTime).Value = dateTimePicker2.Value;
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "PrimaryFeesMainUpdate";
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "FEE";
    cmd1.ExecuteNonQuery();

    con.Close();
    MessageBox.Show("Record Added Successfully", "Success");
    button2_Click(null, null);
}
catch (Exception ex)
{
    Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
    Console.WriteLine("  Message: {0}", ex.Message);

    // Attempt to roll back the transaction. 
    try
    {
        transaction1.Rollback();
    }
    catch (Exception ex2)
    {
        Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
        Console.WriteLine("  Message: {0}", ex2.Message);
    }
}
con.Close();

执行最后一个命令后调用
Commit

cmd1.ExecuteNonQuery();
transaction1.Commit();

执行最后一个命令后调用
Commit

cmd1.ExecuteNonQuery();
transaction1.Commit();

执行最后一个命令后调用
Commit

cmd1.ExecuteNonQuery();
transaction1.Commit();

执行最后一个命令后调用
Commit

cmd1.ExecuteNonQuery();
transaction1.Commit();

您必须提交事务,如下所示:

con.Open();
SqlCommand cmd1 = con.CreateCommand();
SqlTransaction transaction1;
transaction1 = con.BeginTransaction("Save Update Student");
cmd1.Connection = con;
cmd1.Transaction = transaction1;
try
{
    //sp to autogenerate student code in system..
    cmd1.CommandText = "sp_AutoGenerateStudentCode";
    cmd1.CommandType = CommandType.StoredProcedure;

    cmd1.Parameters.Add("@std", SqlDbType.VarChar).Value = cb_std.SelectedItem.ToString();
    cmd1.Parameters.Add("@div", SqlDbType.VarChar).Value = cb_div.SelectedItem;
    cmd1.Parameters.Add("@Rollno", SqlDbType.Int).Value = txt_roll.Text;
    cmd1.Parameters.Add("@ReturnValue", SqlDbType.VarChar).Value = txt_name.Text;
    cmd1.ExecuteNonQuery();

    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText = "insert into StudentMaster(GrNo,Name,DOB,Std,Div,RollNo,MobileNo,Address,TelNo,FathersName,FathersProfession,MothersName,MothersProfession,Age,Year,status,DOE,BookNo,FeesStatus,FthrsQlfction,FthrsOfcAdd,FthrsPhone,MthrsPhone,MthrsOfcAdd,MthrsQlfction,Bloodgrp,caste,Nationality,MotherTongue,PreviousSchool,Religion,height,weight,sex,SCode,EmailId)values ('" + txt_Grno.Text + "','" + txt_name.Text + "',@DOB,'" + cb_std.SelectedItem + "','" + cb_div.SelectedItem + "','" + txt_roll.Text + "','" + txt_mobile.Text + "','" + Rtxt_ResiAdd.Text + "','" + txt_Phone.Text + "','" + txt_fname.Text + "','" + txt_fOccu.Text + "','" + txt_mName.Text + "','" + txt_mOccu.Text + "','" + txt_Age.Text + "',getDate(),'" + cb_status.SelectedItem + "',@DOE,'" + txt_bookno.Text + "','" + cb_feestat.SelectedItem + "','" + txt_fQualificatn.Text + "','" + Rtxt_fOfcAdd.Text + "','" + txt_fPhone.Text + "','" + txt_mPhone.Text + "','" + Rtxt_mOfcAdd.Text + "','" + txt_mQualificatn.Text + "','" + cb_BldGrp.SelectedItem + "','" + txt_caste.Text + "','" + txt_Nationality.Text + "','" + txt_MthrTng.Text + "','" + txt_PrevSchool.Text + "','" + txt_Relgn.Text + "','" + masktb_hgt.Text + "','" + masktb_wgt.Text + "','" + cb_Gender.SelectedItem + "','scode','" + txt_email.Text + "')";

    cmd1.Parameters.Add("@DOE", SqlDbType.DateTime).Value = dateTimePicker1.Value;
    cmd1.Parameters.Add("@DOB", SqlDbType.DateTime).Value = dateTimePicker2.Value;
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "PrimaryFeesMainUpdate";
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "FEE";
    cmd1.ExecuteNonQuery();

    // COMMIT THE TRANSACTION!        
    transaction1.Commit();

    con.Close();
    MessageBox.Show("Record Added Successfully", "Success");
    button2_Click(null, null);
}
catch (Exception ex)
{
    Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
    Console.WriteLine("  Message: {0}", ex.Message);

    // Attempt to roll back the transaction. 
    try
    {
        transaction1.Rollback();
    }
    catch (Exception ex2)
    {
        Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
        Console.WriteLine("  Message: {0}", ex2.Message);
    }
}
con.Close();

您必须提交事务,如下所示:

con.Open();
SqlCommand cmd1 = con.CreateCommand();
SqlTransaction transaction1;
transaction1 = con.BeginTransaction("Save Update Student");
cmd1.Connection = con;
cmd1.Transaction = transaction1;
try
{
    //sp to autogenerate student code in system..
    cmd1.CommandText = "sp_AutoGenerateStudentCode";
    cmd1.CommandType = CommandType.StoredProcedure;

    cmd1.Parameters.Add("@std", SqlDbType.VarChar).Value = cb_std.SelectedItem.ToString();
    cmd1.Parameters.Add("@div", SqlDbType.VarChar).Value = cb_div.SelectedItem;
    cmd1.Parameters.Add("@Rollno", SqlDbType.Int).Value = txt_roll.Text;
    cmd1.Parameters.Add("@ReturnValue", SqlDbType.VarChar).Value = txt_name.Text;
    cmd1.ExecuteNonQuery();

    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText = "insert into StudentMaster(GrNo,Name,DOB,Std,Div,RollNo,MobileNo,Address,TelNo,FathersName,FathersProfession,MothersName,MothersProfession,Age,Year,status,DOE,BookNo,FeesStatus,FthrsQlfction,FthrsOfcAdd,FthrsPhone,MthrsPhone,MthrsOfcAdd,MthrsQlfction,Bloodgrp,caste,Nationality,MotherTongue,PreviousSchool,Religion,height,weight,sex,SCode,EmailId)values ('" + txt_Grno.Text + "','" + txt_name.Text + "',@DOB,'" + cb_std.SelectedItem + "','" + cb_div.SelectedItem + "','" + txt_roll.Text + "','" + txt_mobile.Text + "','" + Rtxt_ResiAdd.Text + "','" + txt_Phone.Text + "','" + txt_fname.Text + "','" + txt_fOccu.Text + "','" + txt_mName.Text + "','" + txt_mOccu.Text + "','" + txt_Age.Text + "',getDate(),'" + cb_status.SelectedItem + "',@DOE,'" + txt_bookno.Text + "','" + cb_feestat.SelectedItem + "','" + txt_fQualificatn.Text + "','" + Rtxt_fOfcAdd.Text + "','" + txt_fPhone.Text + "','" + txt_mPhone.Text + "','" + Rtxt_mOfcAdd.Text + "','" + txt_mQualificatn.Text + "','" + cb_BldGrp.SelectedItem + "','" + txt_caste.Text + "','" + txt_Nationality.Text + "','" + txt_MthrTng.Text + "','" + txt_PrevSchool.Text + "','" + txt_Relgn.Text + "','" + masktb_hgt.Text + "','" + masktb_wgt.Text + "','" + cb_Gender.SelectedItem + "','scode','" + txt_email.Text + "')";

    cmd1.Parameters.Add("@DOE", SqlDbType.DateTime).Value = dateTimePicker1.Value;
    cmd1.Parameters.Add("@DOB", SqlDbType.DateTime).Value = dateTimePicker2.Value;
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "PrimaryFeesMainUpdate";
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "FEE";
    cmd1.ExecuteNonQuery();

    // COMMIT THE TRANSACTION!        
    transaction1.Commit();

    con.Close();
    MessageBox.Show("Record Added Successfully", "Success");
    button2_Click(null, null);
}
catch (Exception ex)
{
    Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
    Console.WriteLine("  Message: {0}", ex.Message);

    // Attempt to roll back the transaction. 
    try
    {
        transaction1.Rollback();
    }
    catch (Exception ex2)
    {
        Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
        Console.WriteLine("  Message: {0}", ex2.Message);
    }
}
con.Close();

您必须提交事务,如下所示:

con.Open();
SqlCommand cmd1 = con.CreateCommand();
SqlTransaction transaction1;
transaction1 = con.BeginTransaction("Save Update Student");
cmd1.Connection = con;
cmd1.Transaction = transaction1;
try
{
    //sp to autogenerate student code in system..
    cmd1.CommandText = "sp_AutoGenerateStudentCode";
    cmd1.CommandType = CommandType.StoredProcedure;

    cmd1.Parameters.Add("@std", SqlDbType.VarChar).Value = cb_std.SelectedItem.ToString();
    cmd1.Parameters.Add("@div", SqlDbType.VarChar).Value = cb_div.SelectedItem;
    cmd1.Parameters.Add("@Rollno", SqlDbType.Int).Value = txt_roll.Text;
    cmd1.Parameters.Add("@ReturnValue", SqlDbType.VarChar).Value = txt_name.Text;
    cmd1.ExecuteNonQuery();

    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText = "insert into StudentMaster(GrNo,Name,DOB,Std,Div,RollNo,MobileNo,Address,TelNo,FathersName,FathersProfession,MothersName,MothersProfession,Age,Year,status,DOE,BookNo,FeesStatus,FthrsQlfction,FthrsOfcAdd,FthrsPhone,MthrsPhone,MthrsOfcAdd,MthrsQlfction,Bloodgrp,caste,Nationality,MotherTongue,PreviousSchool,Religion,height,weight,sex,SCode,EmailId)values ('" + txt_Grno.Text + "','" + txt_name.Text + "',@DOB,'" + cb_std.SelectedItem + "','" + cb_div.SelectedItem + "','" + txt_roll.Text + "','" + txt_mobile.Text + "','" + Rtxt_ResiAdd.Text + "','" + txt_Phone.Text + "','" + txt_fname.Text + "','" + txt_fOccu.Text + "','" + txt_mName.Text + "','" + txt_mOccu.Text + "','" + txt_Age.Text + "',getDate(),'" + cb_status.SelectedItem + "',@DOE,'" + txt_bookno.Text + "','" + cb_feestat.SelectedItem + "','" + txt_fQualificatn.Text + "','" + Rtxt_fOfcAdd.Text + "','" + txt_fPhone.Text + "','" + txt_mPhone.Text + "','" + Rtxt_mOfcAdd.Text + "','" + txt_mQualificatn.Text + "','" + cb_BldGrp.SelectedItem + "','" + txt_caste.Text + "','" + txt_Nationality.Text + "','" + txt_MthrTng.Text + "','" + txt_PrevSchool.Text + "','" + txt_Relgn.Text + "','" + masktb_hgt.Text + "','" + masktb_wgt.Text + "','" + cb_Gender.SelectedItem + "','scode','" + txt_email.Text + "')";

    cmd1.Parameters.Add("@DOE", SqlDbType.DateTime).Value = dateTimePicker1.Value;
    cmd1.Parameters.Add("@DOB", SqlDbType.DateTime).Value = dateTimePicker2.Value;
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "PrimaryFeesMainUpdate";
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "FEE";
    cmd1.ExecuteNonQuery();

    // COMMIT THE TRANSACTION!        
    transaction1.Commit();

    con.Close();
    MessageBox.Show("Record Added Successfully", "Success");
    button2_Click(null, null);
}
catch (Exception ex)
{
    Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
    Console.WriteLine("  Message: {0}", ex.Message);

    // Attempt to roll back the transaction. 
    try
    {
        transaction1.Rollback();
    }
    catch (Exception ex2)
    {
        Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
        Console.WriteLine("  Message: {0}", ex2.Message);
    }
}
con.Close();

您必须提交事务,如下所示:

con.Open();
SqlCommand cmd1 = con.CreateCommand();
SqlTransaction transaction1;
transaction1 = con.BeginTransaction("Save Update Student");
cmd1.Connection = con;
cmd1.Transaction = transaction1;
try
{
    //sp to autogenerate student code in system..
    cmd1.CommandText = "sp_AutoGenerateStudentCode";
    cmd1.CommandType = CommandType.StoredProcedure;

    cmd1.Parameters.Add("@std", SqlDbType.VarChar).Value = cb_std.SelectedItem.ToString();
    cmd1.Parameters.Add("@div", SqlDbType.VarChar).Value = cb_div.SelectedItem;
    cmd1.Parameters.Add("@Rollno", SqlDbType.Int).Value = txt_roll.Text;
    cmd1.Parameters.Add("@ReturnValue", SqlDbType.VarChar).Value = txt_name.Text;
    cmd1.ExecuteNonQuery();

    cmd1.CommandType = CommandType.Text;
    cmd1.CommandText = "insert into StudentMaster(GrNo,Name,DOB,Std,Div,RollNo,MobileNo,Address,TelNo,FathersName,FathersProfession,MothersName,MothersProfession,Age,Year,status,DOE,BookNo,FeesStatus,FthrsQlfction,FthrsOfcAdd,FthrsPhone,MthrsPhone,MthrsOfcAdd,MthrsQlfction,Bloodgrp,caste,Nationality,MotherTongue,PreviousSchool,Religion,height,weight,sex,SCode,EmailId)values ('" + txt_Grno.Text + "','" + txt_name.Text + "',@DOB,'" + cb_std.SelectedItem + "','" + cb_div.SelectedItem + "','" + txt_roll.Text + "','" + txt_mobile.Text + "','" + Rtxt_ResiAdd.Text + "','" + txt_Phone.Text + "','" + txt_fname.Text + "','" + txt_fOccu.Text + "','" + txt_mName.Text + "','" + txt_mOccu.Text + "','" + txt_Age.Text + "',getDate(),'" + cb_status.SelectedItem + "',@DOE,'" + txt_bookno.Text + "','" + cb_feestat.SelectedItem + "','" + txt_fQualificatn.Text + "','" + Rtxt_fOfcAdd.Text + "','" + txt_fPhone.Text + "','" + txt_mPhone.Text + "','" + Rtxt_mOfcAdd.Text + "','" + txt_mQualificatn.Text + "','" + cb_BldGrp.SelectedItem + "','" + txt_caste.Text + "','" + txt_Nationality.Text + "','" + txt_MthrTng.Text + "','" + txt_PrevSchool.Text + "','" + txt_Relgn.Text + "','" + masktb_hgt.Text + "','" + masktb_wgt.Text + "','" + cb_Gender.SelectedItem + "','scode','" + txt_email.Text + "')";

    cmd1.Parameters.Add("@DOE", SqlDbType.DateTime).Value = dateTimePicker1.Value;
    cmd1.Parameters.Add("@DOB", SqlDbType.DateTime).Value = dateTimePicker2.Value;
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "PrimaryFeesMainUpdate";
    cmd1.ExecuteNonQuery();

    cmd1.CommandText = "FEE";
    cmd1.ExecuteNonQuery();

    // COMMIT THE TRANSACTION!        
    transaction1.Commit();

    con.Close();
    MessageBox.Show("Record Added Successfully", "Success");
    button2_Click(null, null);
}
catch (Exception ex)
{
    Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
    Console.WriteLine("  Message: {0}", ex.Message);

    // Attempt to roll back the transaction. 
    try
    {
        transaction1.Rollback();
    }
    catch (Exception ex2)
    {
        Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
        Console.WriteLine("  Message: {0}", ex2.Message);
    }
}
con.Close();

您的
提交在哪里
?使用而不是字符串连接。注意攻击。@SonerGönül也是正确的。为什么你只把DOE和DOB作为参数化并连接其余部分?谢谢大家的回复和建议,@soner:我已经通过sql注入,我也将在参数化查询中编写代码..你的
提交到哪里
?使用而不是字符串连接。注意攻击。@SonerGönül也是正确的。为什么你只把DOE和DOB作为参数化并连接其余部分?谢谢大家的回复和建议,@soner:我已经通过sql注入,我也将在参数化查询中编写代码..你的
提交到哪里
?使用而不是字符串连接。注意攻击。@SonerGönül也是正确的。为什么你只把DOE和DOB作为参数化并连接其余部分?谢谢大家的回复和建议,@soner:我已经通过sql注入,我也将在参数化查询中编写代码..你的
提交到哪里
?使用而不是字符串连接。注意攻击。@SonerGönül也是正确的。为什么你只把DOE和DOB作为参数化的,并把其余的连接起来?谢谢大家的回复和建议,@soner:我已经经历了sql注入,我也将在参数化查询中编写代码。。