数据库帮助类 SqlServerHelp

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Data;
  6 using System.Data.SqlClient;
  7 
  8 namespace WindowsFormsApplication1.DAL
  9 {
 10     /**
 11      * 实现对SqlServer的基本操作
 12      * 1.SqlConnection对象连接数据库;
 13      * 2.SqlCommand对象,负责SQL语句的执行和存储过程的调用,并获取三种返回模式
 14      * 3.执行存储过程,和带参数的存储过程
 15      * 4.执行带参数的sql语句
 16      */
 17     public class SqlServerHelp
 18     {
 19         //写在配置中:string ConnectionString = ConfigurationManager.ConnectionStrings["MyConnection"].ToString();
 20         //配置中的写法
 21         //<connectionStrings> 
 22         //  <add name="SqlConnection"  providerName="System.Data.SqlClient" connectionString="server=.;uid=sa;pwd=accp;database=pubs"/>
 23         //</connectionStrings>
 24         // 25         //<appSettings>
 26         //  <add name="SqlConnection"  providerName="System.Data.SqlClient" connectionString="server=.;uid=sa;pwd=accp;database=pubs"/>
 27         //</appSettings>
 28 
 29         //Sql 验证:string ConnectionString = "user id=sa;password=;initial catalog=northwind;datasource=localhost;connect Timeout=20";
 30         //Windows 身份验证:连接字符串:Data Source =数据库程序实例名,Initial Catalog=数据库名, Integrated Security=True 表示系统模式登录,不需要用户名密码,有最高权限
 31         static string ConnectionString = @"Data Source=DESKTOP-IF0RC4MSQL2012;Initial Catalog=mySqlServer;Integrated Security=True";
 32         //执行sql语句,返回一个值
 33         public static string GetStr() 
 34         {
 35             string val = "";
 36             //传入连接字符串连接
 37             SqlConnection con = new SqlConnection(ConnectionString);
 38             try 
 39             {
 40                 con.Open();
 41                 //传入sql语句和连接类
 42                 SqlCommand com = new SqlCommand("SELECT * FROM dbo.Table_1", con);
 43                 //返回一个值(第一行第一列)
 44                 val = com.ExecuteScalar().ToString();
 45                 con.Close(); //必须关闭,可以用USING方式写
 46             }
 47             catch 
 48             {
 49                 con.Close(); 
 50             }
 51             return val;
 52         }
 53         //执行sql语句,返回多行值
 54         public static DataTable GetRows()
 55         {
 56             DataTable dt = new DataTable();
 57             //传入连接字符串连接
 58             SqlConnection con = new SqlConnection(ConnectionString);
 59             try
 60             {
 61                 con.Open();
 62                 //传入sql语句和连接类
 63                 SqlCommand com = new SqlCommand("SELECT * FROM dbo.Table_1", con);
 64                 //返回一个值(第一行第一列)
 65                 SqlDataReader dr = com.ExecuteReader();
 66                 dt.Load(dr);//直接把结果放进TB表中
 67                 //while(dr.Read()) //返回ture表示能读取一行,表示读取全部行
 68                 //{
 69                 //    val = dr.GetString(0);//GetString(0)获取第一个字段的值
 70                 //}
 71 
 72                 //dr.Read();//执行读取一行,返回值是bool
 73                 //val = dr.GetString(0);
 74                 con.Close(); 
 75             }
 76             catch
 77             {
 78                 con.Close();
 79             }
 80             return dt;
 81         }
 82         //执行sql语句,返回返回一张表
 83         public static DataTable GetTable()
 84         {
 85             DataTable dt = new DataTable();
 86             //传入连接字符串连接
 87             SqlConnection con = new SqlConnection(ConnectionString);
 88             try
 89             {
 90                 con.Open();
 91                 //传入sql语句和连接类
 92                 //SqlCommand com = new SqlCommand("SELECT * FROM dbo.Table_1", con);
 93                 //SqlDataAdapter sda = new SqlDataAdapter(com);
 94                 //另一种写法
 95                 SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM dbo.Table_1", con);
 96                 sda.Fill(dt);
 97                 con.Close();
 98             }
 99             catch
100             {
101                 con.Close();
102             }
103             return dt;
104         }
105         //带参数的sql语句
106         public static string SqlStr_parameters() 
107         {
108             string val = "";
109             //传入连接字符串连接
110             SqlConnection con = new SqlConnection(ConnectionString);
111             try
112             {
113                 con.Open();
114                 //传入sql语句和连接类
115                 SqlCommand com = new SqlCommand("SELECT * FROM dbo.Table_1 WHERE a=@a", con); //特别注意的是,如果参数是字符串,在这里不能加'',
116                 //写法一
117                 //SqlParameter sp = new SqlParameter("@a",SqlDbType.Char,10); //创建参数实例
118                 //sp.Value = "1";
119                 //com.Parameters.Add(sp);
120 
121                 //写法一
122                 //SqlParameter sp = new SqlParameter("@a",SqlDbType.Char,10){Value = "1"}; //创建参数实例
123                 //com.Parameters.Add(sp);
124 
125                 //写法三(推荐)
126                 SqlParameter sp = new SqlParameter("@a", "1");
127                 sp.Value = "1";
128                 com.Parameters.Add(sp);
129 
130                 //如果参数多的时候可以写成数组的形式
131                 //SqlParameter[] sp1 =new SqlParameter[]{ new SqlParameter("@a", "1") };
132                 //com.Parameters.AddRange(sp1);
133 
134                 //返回一个值(第一行第一列)
135                 val = com.ExecuteScalar().ToString();
136                 con.Close(); //必须关闭,可以用USING方式写
137             }
138             catch(Exception ea)
139             {
140                 con.Close();
141             }
142             return val;
143         }
144         //带参数的存储过程
145         public static void Sql_procedure() 
146         {
147             string val = "";
148             //传入连接字符串连接
149             SqlConnection con = new SqlConnection(ConnectionString);
150             try
151             {
152                 /*存储过程
153                  * ALTER PROC usp_execSqlStr
154                    @a CHAR(10)
155                    AS
156                    BEGIN
157                      SELECT * FROM dbo.Table_1 WHERE a=@a
158                    END
159                  */
160                 con.Open();
161                 //传入sql语句和连接类
162                 SqlCommand com = new SqlCommand("usp_execSqlStr", con); //特别注意的是,如果参数是字符串,在这里不能加'',
163                 //com.CommandType = CommandType.Text; //表示sql语句
164                 com.CommandType = CommandType.StoredProcedure ; //表示存储过程
165                 SqlParameter sp = new SqlParameter("@a", "1");
166                 com.Parameters.Add(sp);
167                 val = com.ExecuteScalar().ToString(); //存储过程的执行和一般的sql语句的执行是一样的
168                 con.Close(); //必须关闭,可以用USING方式写
169             }
170             catch (Exception ea)
171             {
172                 con.Close();
173             }
174         }
175     }
176 }
原文地址:https://www.cnblogs.com/it-xcn/p/6098557.html