数据库DbHelperSQL.cs类

代码
 
   
1 using System;
2   using System.Collections.Generic;
3 using System.Data.SqlClient;
4 using System.Configuration;
5 using System.Data;
6
7 namespace Common
8 {
9 public abstract class DbHelperSQL
10 {
11 // 格式化字符串
12 public static string inSQL( string formatStr)
13 {
14 string Str = formatStr;
15 if (formatStr != null && formatStr != string .Empty)
16 {
17 Str = Str.Replace( " ' " , " '' " );
18 }
19 return Str;
20 }
21
22 // 获取连接字符串
23 public static string ConnectionString
24 {
25 get
26 {
27 string _connectionstring = ConfigurationManager.ConnectionStrings[ " ApplicationServices " ].ConnectionString;
28 string ConStringEncrypt = ConfigurationManager.AppSettings[ " ApplicationServices " ];
29 if (ConStringEncrypt == " true " )
30 {
31 _connectionstring = DESEncrypt.Encrypt(_connectionstring);
32 }
33 return _connectionstring;
34 }
35 }
36
37 #region 执行带参数的SQL语句
38
39 // 执行查询语句,返回SqlDataReader ( 注意:调用该方法后,一定要对SqlDataReader进行Close )
40 public static SqlDataReader ExecuteReader( string SQLString, params SqlParameter[] cmdParms)
41 {
42 SqlConnection connection = new SqlConnection(ConnectionString);
43 SqlCommand cmd = new SqlCommand();
44 try
45 {
46 PrepareCommand(cmd,connection, null ,SQLString,cmdParms);
47 SqlDataReader myReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
48 cmd.Parameters.Clear();
49 return myReader;
50 }
51 catch (System.Data.SqlClient.SqlException e)
52 {
53 throw e;
54 }
55 }
56
57 // 执行SQL语句,返回影响的记录数
58 public static int ExecuteSql( string SQLString, params SqlParameter[] cmdParms)
59 {
60 using (SqlConnection connection = new SqlConnection(ConnectionString))
61 {
62 using (SqlCommand cmd = new SqlCommand())
63 {
64 try
65 {
66 PrepareCommand(cmd,connection, null ,SQLString,cmdParms);
67 int rows = cmd.ExecuteNonQuery();
68 cmd.Parameters.Clear();
69 return rows;
70 }
71 catch (System.Data.SqlClient.SqlException e)
72 {
73 throw e;
74 }
75 }
76 }
77 }
78
79 // 执行查询语句,返回DataSet
80 private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
81 {
82 if (conn.State != ConnectionState.Open)
83 conn.Open();
84 cmd.Connection = conn;
85 cmd.CommandText = cmdText;
86 if (trans != null )
87 cmd.Transaction = trans;
88 cmd.CommandType = CommandType.Text;
89 if (cmdParms != null )
90 {
91 foreach (SqlParameter parameter in cmdParms)
92 {
93 if ((parameter.Direction == ParameterDirection.InputOutput || parameter.Direction == ParameterDirection.Input) &&
94 (parameter.Value == null ))
95 {
96 parameter.Value = DBNull.Value;
97 }
98 cmd.Parameters.Add(parameter);
99 }
100 }
101 }
102
103 #endregion
104 }
105 }
106

 

转载于:https://www.cnblogs.com/duantianya/archive/2010/11/14/1877256.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值