public int Release(SqlConnection cn,
                             String MyApplID,
                             String MyKinmuDate,
                             String MyGoukiKubun,
                             String MyLuName)
        {
            int rc = 1;
            SqlTransaction transaction;
            transaction = cn.BeginTransaction("XSSAppUseTransaction");

            // SQL生成
            String mySQL = "SELECT * FROM XSSORAP " +
                                 "where  APPLID = '" + MyApplID + "'" +
                             //  "  and  KinmuDate = '" + MyKinmuDate + "'" +
                             //    "  and  GoukiKubun = '" + MyGoukiKubun + "'" +
                                 "  and  LUNAME= '" + MyLuName + "'";
            SqlDataAdapter daORAP
                = new SqlDataAdapter(mySQL, cn);

            SqlCommandBuilder cbORAP = new SqlCommandBuilder(daORAP);


            daORAP.SelectCommand.Transaction = transaction;
            cbORAP.GetInsertCommand().Transaction = transaction;
            cbORAP.GetUpdateCommand().Transaction = transaction;
            cbORAP.GetDeleteCommand().Transaction = transaction;


            daORAP.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            DataSet dsORAP = new DataSet();

            daORAP.Fill(dsORAP, "XSSORAP");
         
            DataTable dtORAP = dsORAP.Tables["XSSORAP"];

       
            if (dtORAP.Rows.Count != 0)
            {

                foreach (DataRow drORAP in dtORAP.Rows)
                {
                    drORAP.Delete();
                }
                daORAP.Update(dsORAP, "XSSORAP");
                rc = 0;
            }
            else
            {
                rc = -1;
            }
            transaction.Commit();
            transaction.Dispose();
            dtORAP.Dispose();
            dsORAP.Dispose();
            cbORAP.Dispose();
            daORAP.Dispose();

            // 結果
            return (rc);
        }

重载列表

开始数据库事务。

[C#] public SqlTransaction BeginTransaction();

 

以指定的隔离级别开始数据库事务。

[C#] public SqlTransaction BeginTransaction(IsolationLevel);

 

以指定的事务名称开始数据库事务。

[C#] public SqlTransaction BeginTransaction(string);

 

以指定的隔离级别和事务名称开始数据库事务。

[C#] public SqlTransaction BeginTransaction(IsolationLevel, string);

 

示例

[C#] 下面的示例创建一个 SqlConnection 和一个 SqlTransaction。它还演示如何使用 BeginTransaction、Commit 和 Rollback 方法。

[C#] 注意   此示例显示如何使用 BeginTransaction 的一个重载版本。有关其他可用示例,请参阅单独的重载主题。

[C#]

public void RunSqlTransaction(string myConnString)

{ SqlConnection myConnection = new SqlConnection(myConnString);

myConnection.Open();

SqlCommand myCommand = new SqlCommand();

SqlTransaction myTrans; // Start a local transaction

myTrans = myConnection.BeginTransaction(IsolationLevel.ReadCommitted,"SampleTransaction");

// Must assign both transaction object and connection

// to Command object for a pending local transaction

myCommand.Connection = myConnection;

myCommand.Transaction = myTrans;

try {

myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";

myCommand.ExecuteNonQuery();

myCommand.CommandText = "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')"; myCommand.ExecuteNonQuery();

myTrans.Commit();

Console.WriteLine("Both records are written to database.");

}

catch(Exception e)

 { myTrans.Rollback("SampleTransaction");

Console.WriteLine(e.ToString());

Console.WriteLine("Neither record was written to database.");

}

finally { myConnection.Close(); }}

 

posted on 2009-02-20 14:40  wn323225  阅读(420)  评论(0)    收藏  举报