SqlHelper

SqlHelper实现如下:

 

  1 /// <summary>

  2 /// Sql扩展方法的委托

  3 /// </summary>

  4 /// <typeparam name="T">参数类型</typeparam>

  5 /// <param name="target">参数</param>

  6 public delegate void SqlMethodExtend(SqlDataReader target);

  7 /// <summary>

  8 /// 数据库操作类

  9 /// </summary>

 10 public class SqlHelper

 11 {

 12     private string connectionString = null;

 13 

 14     public string ConnectionString

 15     {

 16         get { return connectionString; }

 17         set { connectionString = value; }

 18     }

 19 

 20     private string commandText = null;

 21 

 22     public string CommandText

 23     {

 24         get { return commandText; }

 25         set { commandText = value; }

 26     }

 27 

 28 

 29     private SqlHelper() { }

 30 

 31     /// <summary>

 32     /// 执行一系列数据库的操作

 33     /// </summary>

 34     /// <param name="conStr">连接字符串</param>

 35     /// <param name="cmdText">SQL语句</param>

 36     /// <param name="parameter">可选参数</param>

 37     public void SqlExecuteNone(string conStr = "", string cmdText = "", params SqlParameter[] parameter)

 38     {

 39         if (conStr != null)

 40         {

 41             this.connectionString = conStr;

 42         }

 43         if (cmdText != null)

 44         {

 45             commandText = cmdText;

 46         }

 47         using (SqlConnection con = new SqlConnection(connectionString))

 48         {

 49             using (SqlCommand cmd = new SqlCommand(commandText, con))

 50             {

 51                 foreach (SqlParameter p in parameter)

 52                 {

 53                     cmd.Parameters.Add(p);

 54                 }

 55                 con.Open();

 56                 cmd.ExecuteNonQuery();

 57             }

 58         }

 59     }

 60     /// <summary>

 61     /// 执行一系列查询数据库的操作,返回数据库表的第一行第一列

 62     /// </summary>

 63     /// <param name="conStr">连接字符串</param>

 64     /// <param name="cmdText">SQL语句</param>

 65     /// <param name="parameter">可选参数</param>

 66     /// <returns>返回值</returns>

 67     public object SqlExecuteScalar(string conStr = "", string cmdText = "", params SqlParameter[] parameter)

 68     {

 69         object result = null;

 70         if (conStr != null)

 71         {

 72             connectionString = conStr;

 73         }

 74         if (cmdText != null)

 75         {

 76             commandText = cmdText;

 77         }

 78         using (SqlConnection con = new SqlConnection(connectionString))

 79         {

 80             using (SqlCommand cmd = new SqlCommand(commandText, con))

 81             {

 82                 foreach (SqlParameter p in parameter)

 83                 {

 84                     cmd.Parameters.Add(p);

 85                 }

 86                 con.Open();

 87                 result = cmd.ExecuteScalar();

 88             }

 89         }

 90         return result;

 91     }

 92 

 93     /// <summary>

 94     /// 灵活操作数据库的方法,必须有SqlMethodExtend委托类型的方法Method,

 95     /// Method继承SqlExecuteReader的SqlDataReader,可对数据进行灵活读取

 96     /// </summary>

 97     /// <param name="conStr">连接字符串</param>

 98     /// <param name="Method">操作SqlDataReader的方法,满足委托SqlMethodExtend</param>

 99     /// <param name="cmdText">SQL语句</param>

100     /// <param name="parameter">可选参数</param>

101     public void SqlExecuteReader(string conStr, string cmdText,

102         SqlMethodExtend Method, params SqlParameter[] parameter)

103     {

104         if (conStr != null)

105         {

106             connectionString = conStr;

107         }

108         if (cmdText != null)

109         {

110             commandText = cmdText;

111         }

112         using (SqlConnection con = new SqlConnection(connectionString))

113         {

114             using (SqlCommand cmd = new SqlCommand(commandText, con))

115             {

116                 foreach (SqlParameter p in parameter)

117                 {

118                     cmd.Parameters.Add(p);

119                 }

120                 con.Open();

121                 SqlDataReader reader = cmd.ExecuteReader();

122                 Method(reader);

123             }

124         }

125     }

126 

127     /// <summary>

128     /// 灵活操作数据库的方法,必须有SqlMethodExtend委托类型的方法Method,

129     /// Method继承SqlExecuteReader的SqlDataReader,可对数据进行灵活读取

130     /// </summary>

131     /// <param name="Method">操作SqlDataReader的方法,满足委托SqlMethodExtend</param>

132     /// <param name="cmdText">SQL语句</param>

133     /// <param name="parameter">可选参数</param>

134     public void SqlExecuteReader(string conStr, SqlMethodExtend Method, params SqlParameter[] parameter)

135     {

136         if (conStr != null)

137         {

138             connectionString = conStr;

139         }

140         using (SqlConnection con = new SqlConnection(connectionString))

141         {

142             using (SqlCommand cmd = new SqlCommand(commandText, con))

143             {

144                 foreach (SqlParameter p in parameter)

145                 {

146                     cmd.Parameters.Add(p);

147                 }

148                 con.Open();

149                 SqlDataReader reader = cmd.ExecuteReader();

150                 Method(reader);

151             }

152         }

153     }

154 

155     /// <summary>

156     /// 灵活操作数据库的方法,必须有SqlMethodExtend委托类型的方法Method,

157     /// Method继承SqlExecuteReader的SqlDataReader,可对数据进行灵活读取

158     /// </summary>

159     /// <param name="Method">操作SqlDataReader的方法,满足委托SqlMethodExtend</param>

160     /// <param name="cmdText">SQL语句</param>

161     /// <param name="parameter">可选参数</param>

162     public void SqlExecuteReader(string cmdText,

163         SqlMethodExtend Method, params SqlParameter[] parameter)

164     {

165         if (cmdText != null)

166         {

167             commandText = cmdText;

168         }

169         using (SqlConnection con = new SqlConnection(connectionString))

170         {

171             using (SqlCommand cmd = new SqlCommand(commandText, con))

172             {

173                 foreach (SqlParameter p in parameter)

174                 {

175                     cmd.Parameters.Add(p);

176                 }

177                 con.Open();

178                 SqlDataReader reader = cmd.ExecuteReader();

179                 Method(reader);

180             }

181         }

182     }

183 

184     /// <summary>

185     /// 灵活操作数据库的方法,必须有SqlMethodExtend委托类型的方法Method,

186     /// Method继承SqlExecuteReader的SqlDataReader,可对数据进行灵活读取

187     /// </summary>

188     /// <param name="Method">操作SqlDataReader的方法,满足委托SqlMethodExtend</param>

189     /// <param name="parameter">可选参数</param>

190     public void SqlExecuteReader(SqlMethodExtend Method, params SqlParameter[] parameter)

191     {

192         using (SqlConnection con = new SqlConnection(connectionString))

193         {

194             using (SqlCommand cmd = new SqlCommand(commandText, con))

195             {

196                 foreach (SqlParameter p in parameter)

197                 {

198                     cmd.Parameters.Add(p);

199                 }

200                 con.Open();

201                 SqlDataReader reader = cmd.ExecuteReader();

202                 Method(reader);

203             }

204         }

205     }

206 

207     /// <summary>

208     /// 执行一系列查询数据库的操作,返回数据库表的DataSet

209     /// </summary>

210     /// <param name="conStr">连接字符串</param>

211     /// <param name="cmdText">SQL语句</param>

212     /// <param name="parameter">可选参数</param>

213     /// <returns>Datarow</returns>

214     public DataSet SqlExecuteAdapter(string conStr = "", string cmdText = "")

215     {

216         DataSet ds = new DataSet();

217         if (conStr != null)

218         {

219             connectionString = conStr;

220         }

221         if (cmdText != null)

222         {

223             commandText = cmdText;

224         }

225         using (SqlConnection con = new SqlConnection(connectionString))

226         {

227             SqlDataAdapter da = new SqlDataAdapter(commandText, con);

228             da.Fill(ds);

229             return ds;

230         }

231 

232     }

233 

234     /// <summary>

235     /// 执行一系列查询数据库的操作,返回数据库表的DataTable

236     /// </summary>

237     /// <param name="conStr">连接字符串</param>

238     /// <param name="cmdText">SQL语句</param>

239     /// <param name="parameter">可选参数</param>

240     /// <returns>DataTable</returns>

241     public DataTable SqlExecuteAdapter(string conStr = "", string cmdText = "")

242     {

243         DataTable db = new DataTable();

244         if (conStr != null)

245         {

246             connectionString = conStr;

247         }

248         if (cmdText != null)

249         {

250             commandText = cmdText;

251         }

252         using (SqlConnection con = new SqlConnection(connectionString))

253         {

254             SqlDataAdapter da = new SqlDataAdapter(commandText, con);

255             da.Fill(db);

256             return db;

257         }

258 

259     }

260     /// <summary>

261     /// 执行一系列查询数据库的操作,返回数据库表的DataRow

262     /// </summary>

263     /// <param name="conStr">连接字符串</param>

264     /// <param name="cmdText">SQL语句</param>

265     /// <param name="parameter">可选参数</param>

266     /// <returns>DataRow</returns>

267     public DataRow SqlExecuteAdapter(string conStr = "", string cmdText = "")

268     {

269         DataRow dr = new DataRow();

270         DataTable db = new DataTable();

271         if (conStr != null)

272         {

273             connectionString = conStr;

274         }

275         if (cmdText != null)

276         {

277             commandText = cmdText;

278         }

279         using (SqlConnection con = new SqlConnection(connectionString))

280         {

281             SqlDataAdapter da = new SqlDataAdapter(commandText, con);

282             da.Fill(db);

283             dr = db.Rows[1];

284             return dr;

285         }

286 

287     }

288 }

 2012-10-26

你可能感兴趣的:(help)