SqlTransaction

SqlTransactionpublicvoidRunSqlTransaction(stringmyConnString){   SqlConnectionmyConnection=newSqlConnection(myConnString);   myConnection.Open();   SqlCommandmyCommand=myConnection.CreateCommand();  

大家好,又见面了,我是你们的朋友全栈君。

public void RunSqlTransaction(string myConnString)
{

    SqlConnection myConnection = new SqlConnection(myConnString);
    myConnection.Open();

    SqlCommand myCommand = myConnection.CreateCommand();
    SqlTransaction myTrans;

    // Start a local transaction
    myTrans = myConnection.BeginTransaction();
    // 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)
    {

      try
      {

        myTrans.Rollback();
      }
      catch (SqlException ex)
      {

        if (myTrans.Connection != null)
        {

          Console.WriteLine(“An exception of type ” + ex.GetType() +
                            ” was encountered while attempting to roll back the transaction.”);
        }
      }
   
      Console.WriteLine(“An exception of type ” + e.GetType() +
                        ” was encountered while inserting the data.”);
      Console.WriteLine(“Neither record was written to database.”);
    }
    finally
    {

      myConnection.Close();
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132632.html原文链接:https://javaforall.net

(0)
上一篇 2022年6月10日 下午2:46
下一篇 2022年6月10日 下午2:46


相关推荐

  • Python-pandas的dropna()方法-丢弃含空值的行、列

    Python-pandas的dropna()方法-丢弃含空值的行、列0 摘要 dropna 方法 能够找到 DataFrame 类型数据的空值 缺失值 将空值所在的行 列删除后 将新的 DataFrame 作为返回值返回 1 函数详解函数形式 dropna axis 0 how any thresh None subset None inplace False 参数 axis 轴 0 或 index 表示按行删除 1 或 column

    2026年3月19日
    1
  • X3协同胚布库存管理系统操作手册「建议收藏」

    X3协同胚布库存管理系统操作手册「建议收藏」胚布库存管理系统操作手册 转载于:https://blog.51cto.com/itlingm/399124

    2022年6月12日
    36
  • QList 用法

    QList 用法QList 是一种表示链表的模板类 QList lt T gt 是 Qt 的一种泛型容器类 它以链表方式存储一组值 并能对这组数据进行快速索引 还提供了快速插入和删除等操作 QList QLinkedList 和 QVector 提供的操作极其相似 对大多数操作来说 我们用 QList 就可以了 其 API 是基于索引 index 的 因此用起来比 QLinkedList 更方便 QLinkedList 的 AP

    2026年3月17日
    2
  • 教你如何暴力破解wifii密码

    使用kalilinux系统进行wifi暴力破解获取密码注意:私自破解他人WiFi属于违法行为,本教程仅供学习与参考。破解工具破解工具:kalilinux系统 ,本教程使用的装在物理机的linux系统(虚拟机使用方法一样)。支持监听模式的无线网卡,本教材使用的是某宝购买的3070L网卡。字典文件(如果你没有字典也没有问题后面会教你如何使用cruncl创建密码文件)。…

    2022年4月8日
    81
  • 美团面试中被问到的问题汇总

    美团面试中被问到的问题汇总

    2022年1月22日
    80
  • SqlCommand.ExecuteReader 方法

    SqlCommand.ExecuteReader 方法SqlCommand.ExecuteReader方法将CommandText发送到Connection并生成一个SqlDataReader。重载列表名称说明SqlCommand.ExecuteReader()…

    2022年6月20日
    30

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号