尝试在非常简单的插入程序中应用SQL事务回滚. [英] Trying to apply SQL Transaction Rollback in very simple insert program.

查看:111
本文介绍了尝试在非常简单的插入程序中应用SQL事务回滚.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

但是当我运行此代码时,它说"ExecuteNonQuery:连接属性尚未初始化."




公共无效insert()
{

string con =数据源= .;初始目录= LPTesting;集成安全性= True";
SqlConnection sqlcon =新的SqlConnection(con);
sqlcon.Open();
SqlCommand sqlcom1 =新的SqlCommand();
SqlCommand sqlcom2 =新的SqlCommand();
SqlTransaction st = sqlcon.BeginTransaction();
试试
{


//st = sqlcon.BeginTransaction();

字符串insert =插入到Booking(ClientId,AirLine,FlightNumber,Price)" +
值(""+ ddlaAgent.SelectedValue +",""+ this.txtAirline.Text +"''," + this.txtFlightNumber.Text +'',""+ this.txtPrice .Text +'')";

字符串insert2 =插入到Bookings(ClientId,AirLine,FlightNumber,Price)" +
值(""+ ddlaAgent.SelectedValue +",""+ this.txtAirline.Text +"''," + this.txtFlightNumber.Text +'',""+ this.txtPrice .Text +'')";

sqlcom1.CommandText =插入;
sqlcom1.ExecuteNonQuery();
lblbooking.InnerText =订单已添加";

sqlcom2.CommandText = insert2;
sqlcom2.ExecuteNonQuery();
lblbooking.InnerText =已添加Order2";
sqlcon.Close();
sqlcon.Dispose();

st.Commit();

}
赶上
{
lblbooking.InnerText =错误";
st.Rollback();

}
}

but when i am running this code it is saying "ExecuteNonQuery: Connection property has not been initialized."




public void insert()
{

string con = "Data Source=.;Initial Catalog=LPTesting;Integrated Security=True";
SqlConnection sqlcon = new SqlConnection(con);
sqlcon.Open();
SqlCommand sqlcom1 = new SqlCommand();
SqlCommand sqlcom2 = new SqlCommand();
SqlTransaction st = sqlcon.BeginTransaction();
try
{


//st = sqlcon.BeginTransaction();

string insert = "insert into Booking(ClientId,AirLine,FlightNumber,Price)" +
"values (''" + ddlaAgent.SelectedValue + "'',''" + this.txtAirline.Text + "'',''" + this.txtFlightNumber.Text + "'',''" + this.txtPrice.Text + "'')";

string insert2 = "insert into Bookings(ClientId,AirLine,FlightNumber,Price)" +
"values (''" + ddlaAgent.SelectedValue + "'',''" + this.txtAirline.Text + "'',''" + this.txtFlightNumber.Text + "'',''" + this.txtPrice.Text + "'')";

sqlcom1.CommandText = insert;
sqlcom1.ExecuteNonQuery();
lblbooking.InnerText = "Order has been addded";

sqlcom2.CommandText = insert2;
sqlcom2.ExecuteNonQuery();
lblbooking.InnerText = "Order2 has been added";
sqlcon.Close();
sqlcon.Dispose();

st.Commit();

}
catch
{
lblbooking.InnerText = "Error";
st.Rollback();

}
}


推荐答案

1.在执行之前,每个SQL命令必须关联一个已打开的SQL连接,在您的情况下,它们没有,并且应使用相同的SQL连接对象.

2.事务的使用应在try-catch-finally 块中完成,并使用:BeginTransaction(),CommitTransaction()和RollbackTransaction()(在例外情况下).

您可以在MSDN中看到示例: http://msdn.microsoft.com/zh-cn/library/86773566(v = vs.110).aspx [
1.Each SQL command, before to be executed, must have associated an opened SQL connection, and in your case they don''t have, and should use the same SQL connection object.

2.The using of transaction should be done in try-catch-finally block and by using: BeginTransaction(), CommitTransaction() and RollbackTransaction() (in the case of exceptions).

You could see examples in MSDN: http://msdn.microsoft.com/en-us/library/86773566(v=vs.110).aspx[^]


这篇关于尝试在非常简单的插入程序中应用SQL事务回滚.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆