首页 > 代码库 > ASP.NET:分享一个操作SQL Server数据库的工具类

ASP.NET:分享一个操作SQL Server数据库的工具类

  1 using System;  2 using System.Collections.Generic;  3 using System.Linq;  4 using System.Web;  5 using System.Collections;  6 using System.Data.SqlClient;  7   8 public class DatabaseHelper  9 { 10     private string connectionString = Constants.ConnectionURL; 11      12     //隐藏构造器; 13     private DatabaseHelper() 14     { 15     } 16  17     private DatabaseHelper(string connectionString) 18     { 19         this.connectionString = connectionString; 20     } 21  22     //默认参数连接数据库; 23     public SqlConnection GetConnection() 24     { 25         SqlConnection connection = new SqlConnection(this.connectionString); 26         return connection; 27     } 28  29     //指定连接字符串连接数据库; 30     public SqlConnection GetConnection(string connectionString) 31     { 32         SqlConnection connection = new SqlConnection(connectionString); 33         return connection; 34     } 35  36     //执行SQL SELECT 查询语句,返回JSON格式的查询结果; 37     public string Query(SqlConnection connection, string SQLstatement){ 38         string str = "["; 39  40         connection.Open(); 41         SqlCommand command = connection.CreateCommand(); 42         command.CommandText = SQLstatement; 43         SqlDataReader dataReader = command.ExecuteReader(); 44         int i = 0; 45         ArrayList arrayList = new ArrayList(); 46         for (i = 0; i < dataReader.FieldCount; i++) 47         { 48             arrayList.Add(dataReader.GetName(i)); 49         } 50         while (dataReader.Read()) 51         {  52             str += "{"; 53             for (i = 0; i < dataReader.FieldCount; i++) 54             { 55                 str += arrayList[i].ToString() + ":" + dataReader[i].ToString(); 56                 if (!(i == dataReader.FieldCount - 1)) 57                 { 58                     str += ","; 59                 } 60             } 61             str += "},"; 62         } 63         dataReader.Close(); 64         connection.Close(); 65         int index = str.LastIndexOf(","); 66         str = str.Remove(index); 67         str += "]"; 68         return str; 69     } 70  71     //执行SQL INSERT语句; 72     public void Insert(SqlConnection connection, string SQLstatement) 73     { 74         connection.Open(); 75         SqlCommand command = connection.CreateCommand(); 76         command.CommandText = SQLstatement; 77         command.ExecuteNonQuery(); 78         connection.Close(); 79     } 80  81     //执行SQL UPDATE语句; 82     public void Update(SqlConnection connection, string SQLstatement) 83     { 84         connection.Open(); 85         SqlCommand command = connection.CreateCommand(); 86         command.CommandText = SQLstatement; 87         command.ExecuteNonQuery(); 88         connection.Close(); 89     } 90  91     //执行SQL DELETE语句; 92     public void Delete(SqlConnection connection, string SQLstatement) 93     { 94         connection.Open(); 95         SqlCommand command = connection.CreateCommand(); 96         command.CommandText = SQLstatement; 97         command.ExecuteNonQuery(); 98         connection.Close(); 99     }100 101     //返回默认参数的实例对象;102     public static DatabaseHelper GetInstance()103     {104         return new DatabaseHelper();105     }106 107     //返回指定参数的实例对象;108     public static DatabaseHelper GetInstance(string connectionString)109     {110         return new DatabaseHelper(connectionString);111     }112 }

 

ASP.NET:分享一个操作SQL Server数据库的工具类