zoukankan      html  css  js  c++  java
  • 使用SqlTransaction创立数据库连接

    使用SqlTransaction创立数据库连接

    一、连接字符串 

    public static readonly string connString = System.Configuration.ConfigurationSettings.AppSettings["connString"];
     

    二、一个封装了ExecuteNonQuery()操作的方法,使用SqlTransaction建立数据库连接.   
      public static int ExecuteNonQuery(SqlTransaction transaction, string cmdText, SqlParameter[] parms,CommandType cmdtype)
      {
       int result= 0;

       SqlCommand cmd = new SqlCommand(cmdText);
       cmd.Connection = tran.Connection;
       cmd.Transaction = transaction;
       cmd.CommandType = cmdtype;
       cmd.CommandTimeout = 100;

       if ( parms != null)
       {
        foreach (SqlParameter parm in parms)
        {
         cmd.Parameters.Add(parm);
        }
       }
       
       result = cmd.ExecuteNonQuery(); 
       cmd.Parameters .Clear ();

       return result;
      }

    三、另外一个封装了ExecuteNonQuery()操作的数据库操作方法,使用SqlTransaction连接数据库

    public static int ExecuteNonQuery(SqlTransaction transaction, string cmdText)
      {
       int result = 0;

       SqlCommand cmd = new SqlCommand(cmdText);
       cmd.Connection = tran.Connection;
       cmd.Transaction = transaction;
       cmd.CommandTimeout = 100;
       
       result= cmd.ExecuteNonQuery(); 

       return result;
      }

  • 相关阅读:
    java匿名对象
    Java面向对象详解
    Java语言基本语法
    Win7下JDK环境变量的设置
    LeetCode-Shortest Word Distance
    LeetCode-Count Complete Tree Nodes
    LeetCode-Palindrome Pairs
    LeetCode- Implement Trie (Prefix Tree)
    LeetCode-Lowest Common Ancestor of a Binary Tre
    LeetCode- Binary Tree Longest Consecutive Sequence
  • 原文地址:https://www.cnblogs.com/winner/p/1032620.html
Copyright © 2011-2022 走看看