View Code
  1 using System;
  2 using System.Data;
  3 using System.Data.SqlClient;
  4 public class Helper
  5 {
  6     public static void Main()
  7     {
  8         //连接字符串
  9         string strcon = "server = .;database = blog;uid = sa;pwd = 1";
 10         SqlHelper helper = new SqlHelper(strcon);
 11         //表employee中有neme(varchar(20)),age(int),sex(bit)三个字段;
 12         string sql = "select * from employee";
 13         SqlDataReader reader = helper.Reader(sql,null);
 14         using (reader)
 15         {
 16             while (reader.Read())
 17             {
 18                 Console.WriteLine(reader["name"].ToString());
 19             }
 20         }
 21         /*string sql = "insert into employee values(@name,@age,@sex)";
 22         SqlParameter[] ps = new SqlParameter[]
 23         {
 24             new SqlParameter("@name",SqlDbType.VarChar,20),
 25             new SqlParameter("@age",SqlDbType.Int),
 26             new SqlParameter("@sex",SqlDbType.Int)
 27         };
 28             
 29             ps[0].Value = "张宇";
 30             ps[1].Value = 40;
 31             ps[2].Value = 1;
 32             helper.ExecuteNonQuery(sql,ps);*/
 33     }
 34 }
 35 public class SqlHelper
 36 {
 37     private SqlConnection con = null;
 38     private SqlCommand cmd = null;
 39     public SqlHelper (string strcon)
 40     {
 41         con = new SqlConnection(strcon);
 42         cmd = new SqlCommand();
 43         cmd.Connection = con;
 44     }
 45     //执行初始化Command对象
 46     private void PreparedCommand(string sql,params SqlParameter[]param)
 47     {
 48         cmd.CommandText = sql;
 49         //清空Parameteras中的对象
 50         cmd.Parameters.Clear();
 51         if (param!=null)
 52         {
 53             foreach (SqlParameter p in param)
 54             {
 55                 cmd.Parameters.Add(p);
 56             }
 57         }
 58         con.Open();
 59     }
 60     /*执行非查询语句*/
 61     //有参数
 62     public int ExecuteNonQuery(string sql,params SqlParameter[] param)
 63     {
 64         PreparedCommand(sql,param);
 65         int i = cmd.ExecuteNonQuery();
 66         Close();
 67         return i;
 68     }
 69     //无参数
 70     public int ExecuteNonQuery(string sql)
 71     {
 72         PreparedCommand(sql,null);
 73         int i = cmd.ExecuteNonQuery();
 74         Close();
 75         return i;
 76     }
 77     /*执行查询语句*/
 78     //有参数
 79     public SqlDataReader Reader(string sql,params SqlParameter[] param)
 80     {
 81         PreparedCommand(sql,param);
 82         return cmd.ExecuteReader();
 83     }
 84     //无参数
 85     public SqlDataReader Reader(string sql)
 86     {
 87         PreparedCommand(sql,null);
 88         return cmd.ExecuteReader();
 89         
 90     }
 91 
 92     public void Open()
 93     {
 94         con.Open();
 95     }
 96     public void Close()
 97     {
 98         cmd.Dispose();
 99         con.Close();
100     }
101 
102 }

这个程序是把在外部操作数据库的方法进行封装,是外部操作数据库的通用版哦!可以随时随地的引用……简单方便的实现数据库的增、删、改、查!

相关文章:

  • 2021-09-07
  • 2021-05-07
  • 2021-11-18
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2021-07-25
  • 2021-07-05
猜你喜欢
  • 2021-07-05
  • 2021-09-14
  • 2022-02-07
  • 2021-06-03
  • 2021-06-06
  • 2021-06-05
  • 2022-12-23
相关资源
相似解决方案