数据库相关代码

  1         #region -----データベースアクセス共通関数-----
  2         private static SqlConnection m_SqlConnection = null;
  3         private static SqlCommand m_SqlCommand = null;
  4         private static SqlTransaction m_SqlTransaction = null;
  5         //-------------------------------------------
  6         // DB_Init: DBアクセスの初期化
  7         // 戻り値: 1 - 成功、 -1 - 失敗
  8         //-------------------------------------------
  9         public static int DB_Init(string strConnect)
 10         {
 11 
 12             if (m_SqlConnection == null)
 13             {
 14                 m_SqlConnection = new SqlConnection(strConnect);
 15                 m_SqlConnection.Open();
 16                 m_SqlCommand = m_SqlConnection.CreateCommand();
 17                 m_SqlCommand.Connection = m_SqlConnection;
 18                 cmdTimeOut = m_SqlCommand.CommandTimeout;
 19             }
 20             return (1);
 21         }
 22 
 23         //-------------------------------------------
 24         // DB_End: DBアクセスのリソース開放
 25         // 戻り値: 1 - 成功、 -1 - 失敗
 26         //-------------------------------------------
 27         public static int DB_End()
 28         {
 29             if (m_SqlConnection == null) return (-1);
 30             m_SqlCommand.Dispose();
 31             m_SqlConnection.Close();
 32             m_SqlConnection.Dispose();
 33             m_SqlConnection = null;
 34             m_SqlCommand = null;
 35             m_SqlTransaction = null;
 36             return (1);
 37         }
 38 
 39         //-------------------------------------------
 40         // DB_BeginTran: トランザクションの開始
 41         // 戻り値: 1 - 成功、 -1 - 失敗
 42         //-------------------------------------------
 43         public static int DB_BeginTran()
 44         {
 45             if (m_SqlTransaction != null) return (1);
 46 
 47             if (m_SqlConnection == null) return (-1);
 48 
 49             m_SqlTransaction = m_SqlConnection.BeginTransaction();
 50             m_SqlCommand.Transaction = m_SqlTransaction;
 51             return (1);
 52         }
 53 
 54         //-------------------------------------------
 55         // DB_CommitTran: トランザクションのコミット
 56         // 戻り値: 1 - 成功、 -1 - 失敗
 57         //-------------------------------------------
 58         public static int DB_CommitTran()
 59         {
 60             if (m_SqlTransaction == null) return (-1);
 61 
 62             m_SqlTransaction.Commit();
 63             m_SqlTransaction = null;
 64             m_SqlCommand.Transaction = null;
 65             return (1);
 66         }
 67 
 68         //-------------------------------------------
 69         // DB_RollbackTran: トランザクションのロールバック
 70         // 戻り値: 1 - 成功、 -1 - 失敗
 71         //-------------------------------------------
 72         public static int DB_RollbackTran()
 73         {
 74             if (m_SqlTransaction == null) return (-1);
 75 
 76             m_SqlTransaction.Rollback();
 77             m_SqlTransaction = null;
 78             m_SqlCommand.Transaction = null;
 79             return (1);
 80         }
 81 
 82         //-------------------------------------------
 83         // DB_ExecuteStoredProcedure: ストアドの実行
 84         // 戻り値: DataSet - 成功、 null - 失敗
 85         //-------------------------------------------
 86         public static DataSet DB_ExecuteStoredProcedure(string strStoredProcedureName)
 87         {
 88             //return (DB_ExecuteStoredProcedure(strStoredProcedureName, null));
 89             return (DB_ExecuteStoredProcedure(strStoredProcedureName, -1, null));
 90 
 91         }
 92 
 93         public static DataSet DB_ExecuteStoredProcedure(string strStoredProcedureName, params SqlParameter[] aSqlParameter)
 94         {
 95             return (DB_ExecuteStoredProcedure(strStoredProcedureName, -1, aSqlParameter));
 96         }
 97 
 98         public static DataSet DB_ExecuteStoredProcedure(string strStoredProcedureName, int intTimeOut)
 99         {
100             return (DB_ExecuteStoredProcedure(strStoredProcedureName, intTimeOut, null));
101         }
102 
103         //public static DataSet DB_ExecuteStoredProcedure(string strStoredProcedureName, params SqlParameter[] aSqlParameter)
104         public static DataSet DB_ExecuteStoredProcedure(string strStoredProcedureName, int intTimeOut, params SqlParameter[] aSqlParameter)
105         {
106             if (m_SqlConnection == null) return (null);
107 
108             DataSet dsOut = new DataSet();
109 
110             m_SqlCommand.CommandText = strStoredProcedureName;
111             m_SqlCommand.CommandType = CommandType.StoredProcedure;
112 
113             if (intTimeOut != -1)
114             {
115                 m_SqlCommand.CommandTimeout = intTimeOut;
116             }
117             else
118             {
119                 m_SqlCommand.CommandTimeout = cmdTimeOut;
120             }
121 
122             //パラメータの設定
123             m_SqlCommand.Parameters.Clear();
124             if (aSqlParameter != null)
125             {
126                 foreach (SqlParameter p in aSqlParameter)
127                 {
128                     if (p != null)
129                     {
130                         // 割り当てられないかどうかのチェック
131                         if ((p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Input) && p.Value == null)
132                         {
133                             p.Value = DBNull.Value;
134                         }
135                         m_SqlCommand.Parameters.Add(p);
136                     }
137                 }
138             }
139             // Execute StoredProducure
140             SqlDataAdapter sdaOut = new SqlDataAdapter(m_SqlCommand);
141             sdaOut.Fill(dsOut);
142             return (dsOut);
143         }
144 
145         #endregion
View Code

转载于:https://www.cnblogs.com/-jwj/p/10710478.html

原文地址:https://www.cnblogs.com/twodog/p/12134915.html