1 using System;
  2 using System.Data;
  3 using System.Collections.Generic;
  4 using System.Linq;
  5 using System.Text;
  6 using System.Data.SqlClient;
  7 using System.Text.RegularExpressions;
  8 public class SQLhelper
  9 {        //连接数据库
 10     static string strconn=System.Configuration.ConfigurationManager.Configurations["sql"].ConfigurationString;
 11     //非查询操作,返回被修改的条数,在消息窗口显示
 12     public static int ExecuteNonQurey(string commandText,params SqlParameter[] para)
 13     {
 14         using(SqlConnection conn=new SqlCollection(strconn))
 15         {
 16             using(SqlCommand cmd=new SqlCommand(commandText,conn))
 17             {
 18                 if(para!=null && para.Length!=0)
 19                 {
 20                     cmd.Parameters.AddRange(para);
 21                 }
 22                 if(conn.State==System.Data.ConnectionState.Closed())
 23                 {
 24                     conn.Open();
 25                 }
 26                 return cmd.ExecuteNonQurey;
 27             }
 28         }
 29     }
 30     //扩展,直接传参
 31     public static int ExecuteNonQurey(string commandText,params object[] obje)
 32     {
 33         MatchCollection ms=Regex.Matchs(commandText,@"@\w+");
 34         List<SqlParameter> list=new List<SqlParameter>();
 35         if(ms.Count!=obje.Length)
 36         {
 37             throw new Exception();
 38         }
 39         for(int i=0;i<ms.Count;i++)
 40         {
 41             list.Add(new SqlParameter(ms[i].Value,obje[i]));
 42         }
 43         return ExecuteNonQurey(commandText,list.ToArray());
 44     }
 45     //查询语句,返回第一行第一列,在运行窗口显示,多用来判断是否存在
 46     public static object ExecuteScalar(string commandText,params SqlParameter[] para)
 47     {
 48         using(SqlConnection conn=new SqlConnection(strconn))
 49         {
 50             using(sqlCommand cmd=new SqlCommand(commandText,conn))
 51             {
 52                 if(para!=null && para.Length!=0)
 53                 {
 54                     cmd.Parameters.AddRanger(para);
 55                 }
 56                 if(conn.State==System.Data.ConnectionState.Closed())
 57                 {
 58                     conn.Open();
 59                 }
 60             }
 61         }
 62         return cmd.ExecuteScalar();
 63     }
 64     //扩展,直接传参数
 65     public static object ExecuteScalar(string commandText,params object[] obje)
 66     {
 67         MatchConnection ms=Regex.Matchs(commandText,@"@\w+");
 68         List<Parameter> list=new List<Parameter>();
 69         if(ms.Count!=obje.Length)
 70         {
 71             throw new Exception();
 72         }
 73         for(int i=0;i<ms.Count;i++)
 74         {
 75             list.Add(new SqlParameter(ms[i].Value,obje[i]));
 76         }
 77         return ExecuteScalar(commandText,list.ToArray());
 78     }
 79     //查询数据,并按行输出
 80     public static SqlDataReader ExecuteReader(string commandText,params SqlParameter[] para)
 81     {
 82         SqlConnection conn=new SqlConnection(strconn);
 83         using(SqlCommand cmd=new SqlCommand(commandText,conn))
 84         {
 85             if(para!=null && para.Length!=0)
 86             {
 87                 cmd.Parameter.AddRange(para);
 88             }
 89             if(conn.State==System.Data.ConnectionStrate.Closed())
 90             {
 91                 conn.Open();
 92             }
 93         }
 94         return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
 95     }
 96     //扩展方法,参数化
 97     public static SqlDataReader(string commandText,params object[] obje)
 98     {
 99         MatchCollection mc=Regex.Matchs(commandText,@"@\w+");
100         List<Parameter> list=new List<Parameter>();
101         if(mc.Count!=obje.Length)
102         {
103             throw new Exception();
104         }
105         for(int i=0;i<mc.Count;i++)
106         {
107             list.Add(new SqlParameter(ms[i].Value,obje[i]);
108         }
109         return ExecuteReader(commandText,list.ToArray());
110     }
111     //输出整个数据表
112     public static DataSet DataAdapter(string commandText,params SqlParameter[] para)
113     {
114         DataSet ds=new DataSet();
115         using(SqlDataAdapter sda=new SqlAdapter(commandText,strconn))
116         {
117             if(para!=null && para.Length!=0)
118             {
119                 sda.SelectCommand.Parameters.AddRange(para);
120             }
121             sda.Fill(ds);
122             return ds;
123         }
124     }
125     //参数化
126     public static DataSet SqlAdapter(string commandText,params object[] obje)
127     {
128         MatchCollection ms=Regex.Matchs(commandText,@"@\w+");
129         List<Parameter> list=new List<Parameter>();
130         if(ms.Count!=obje.Length)
131         {
132             throw new Exception();
133         }
134         for(int i=0;i<ms.Count;i++)
135         {
136             list.Add(new SqlParameter(ms[i].Value,obje[i]));
137         }
138         return DataAdapter(commandText,list,ToArray());
139     }
140     
141 }
posted on 2012-07-23 22:08  Fan帥帥  阅读(621)  评论(0编辑  收藏  举报