C#中的db上下文事务

时间:2015-07-27 11:06:52

标签: c# sql linq

我正在尝试从c#context提交事务:

private TestDbEntities context =new TestDbEntities(ConnectionString); 

this.context.Connection.Open();
System.Data.Common.DbTransaction transaction = this.context.Connection.BeginTransaction();
DbCommand command = this.context.Connection.CreateCommand();
command.CommandText = "Some Insert/Update Query";
command.ExecuteNonQuery();
transaction.Commit();

但我收到错误消息:

  

"查询语法无效。接近标识符Table_Name,第1行,   第8栏。"

当我直接在sql management studio上直接执行时,查询似乎很好。 有什么解决方案吗?

1 个答案:

答案 0 :(得分:-2)

 SqlConnection conn = new SqlConnection(); 
 SqlCommand command1 = new SqlCommand(); 
 SqlCommand command2 = new SqlCommand(); 

            SqlTransaction trans = conn.BeginTransaction();
            command1.Transaction = trans;
            command2.Transaction = trans;

            try
            {
                command1.ExecuteNonQuery();
                command2.ExecuteNonQuery();
                trans.Commit();
            }
            catch (SqlException ex)
            {
                trans.Rollback();
            }
            finally
            {

            }