我把数据库操作类整理了一下,它包含了常用的数据库操作,由三种方式:简单的SQL拼接字符串的形式,SQL语句使用参数的形式和存储过程的形式,每种形式均有五个方法,并且都有事务.,可以直接调用.代码如下:
1//======================================================================
2//
3// Copyright (C) 2007-2008 三月软件工作室
4// All rights reserved
5//
6// filename :SQLDataBase
7// description :
8//
9// created by 侯垒 at 04/14/2008 18:33:32
10// http://houleixx.cnblogs.com
11//
12//======================================================================
13
14using System;
15using System.Collections;
16using System.Collections.Specialized;
17using System.Data;
18using System.Data.SqlClient;
19using System.Configuration;
20using System.Data.Common;
21
22namespace SQLDataBase
23{
24 /**//// <summary>
25 /// 数据访问基础类(基于SQLServer)
26 /// </summary>
27 class SQLDataBase
28 {
29 protected static string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
30 public SQLDataBase()
31 {
32
33 }
34
35 执行简单SQL语句#region 执行简单SQL语句
36
37 /**//// <summary>
38 /// 执行SQL语句,返回影响的记录数
39 /// </summary>
40 /// <param name="SQLString">SQL语句</param>
41 /// <returns>影响的记录数</returns>
42 public int ExecuteSql(string SQLString)
43 {
44 using (SqlConnection connection = new SqlConnection(connectionString))
45 {
46 using (SqlCommand cmd = new SqlCommand(SQLString, connection))
47 {
48 try
49 {
50 connection.Open();
51 int rows = cmd.ExecuteNonQuery();
52 return rows;
53 }
54 catch (System.Data.SqlClient.SqlException E)
55 {
56 connection.Close();
57 throw new Exception(E.Message);
58 }
59 }
60 }
61 }
62