SQLHelper--四种方法完整版

主要分为三部分:

(1) 配置文件

(2)Program.cs

(3)SQLHelper.cs

//--------------------------------------1  配置文件-----------------------------------------------------------------------------------



   
       
   

 
   
 


//--------------------------------------2 Program.cs---------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _11SQLHelper
{
    class Program
    {
        static void Main(string[] args)
        {
            // 对数据库的增删改查

            // 创建数据库
            //string createtable = "create table Test0806(id int not null, name nvarchar(10), pwd varchar(10))";

            //int res = JKLibrary.SQLHelper.ExecuteNonQuery(createtable);

            //Console.WriteLine(res);


            // 插入数据
            #region 无参数,增加数据
            //string sql = "insert into Test0806(id, name, pwd) values (1, N'赵晓虎','TigerZhao');";
            //int res = JKLibrary.SQLHelper.ExecuteNonQuery(sql);
            //Console.WriteLine(res);
            #endregion


            #region 参数化增加
            //string sql = "insert into Test0806(id, name, pwd) values (@id, @name, @pwd);";
            //SqlParameter[] ps = {
            //                       new SqlParameter("@id", (object)2),
            //                       new SqlParameter("@name", "赵剑宇"),
            //                       new SqlParameter("@pwd", "happyZhao")
            //                    };
            //int res = JKLibrary.SQLHelper.ExecuteNonQuery(sql, ps);
            //Console.WriteLine(res);
            #endregion

            // 修改
            #region 参数化修改
            //string sql = "update Test0806 set name=@name where id=@id;";
            //SqlParameter[] ps = {
            //                        new SqlParameter("@name", "赵晓"),
            //                        new SqlParameter("@id", (object)2)
            //                    };
            //int res = JKLibrary.SQLHelper.ExecuteNonQuery(sql, ps);
            #endregion

            // 删除
            //string sql = "delete from Test0806 where name=@name;";
            //SqlParameter pName = new SqlParameter("@name", "赵晓");
            //int count = JKLibrary.SQLHelper.ExecuteNonQuery(sql, pName);
            //Console.WriteLine(count);

            // 查询姓赵的有多少人?
            #region Scalar
            //string sql = "select count(*) from Test0806 where name like @name;";
            //SqlParameter pName = new SqlParameter("@name", "赵%");
            //int count = (int)JKLibrary.SQLHelper.ExecuteScalar(sql, pName);
            //Console.WriteLine(count);
            #endregion

            // 读取数据
            #region 读取数据
            //using (SqlDataReader reader = JKLibrary.SQLHelper.ExecuteReader("select * from Test0806;"))
            //{
            //    while (reader.Read())
            //    {
            //        List list = new List();
            //        for(int i=0;i             //        {
            //            list.Add(reader[i].ToString());
            //        }

            //        Console.WriteLine(string.Join(",", list));
            //    }
            //}
            //Console.WriteLine("OK");
            #endregion

            string sql = "select * from TestDataBase..Student;";
            using (StreamWriter writer = new StreamWriter("s.txt", true, Encoding.Default))
            {
                using (SqlDataReader reader = JKLibrary.SQLHelper.ExecuteReader(sql))
                {
                    List list = new List();
                    while (reader.Read())
                    {
                        list.Clear();
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            list.Add(reader[i].ToString());
                        }
                        writer.WriteLine(string.Join(",", list));
                    }
                }
            }

            Console.WriteLine("OK");
            Console.ReadKey();           
        }
    }
}




//--------------------------------------3 SQLHelper.cs---------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JKLibrary
{
    // 提供执行方法
    public class SQLHelper
    {
        // 连接字符串
        static string connStr = ConfigurationManager.ConnectionStrings["sql"].ConnectionString;

        public static int ExecuteNonQuery(string sql, params SqlParameter[] ps)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddRange(ps);
                    conn.Open();
                    return cmd.ExecuteNonQuery();
                }
            }
        }// ExecuteNonQuery End

       


        // ExecuteScalar 方法
        public static object ExecuteScalar(string sql, params SqlParameter[] ps)
        {
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddRange(ps);
                    conn.Open();
                    return cmd.ExecuteScalar();
                }
            }
        }

 

        public static SqlDataReader ExecuteReader(string sql, params SqlParameter[] ps)
        {
            SqlConnection conn = new SqlConnection(connStr);
            try
            {
                using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    cmd.Parameters.AddRange(ps);
                    conn.Open();
                    return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
                }
            }
            catch(Exception ex)
            {
                // 如果cmd执行错误,从这里将异常抛出
                conn.Dispose();
                throw ex;
            }
        }

        public static DataSet GetDataSet(string sql, params SqlParameter[] ps)
        {
            DataSet ds = new DataSet();
            using (SqlDataAdapter sda = new SqlDataAdapter(sql, connStr))
            {
                sda.SelectCommand.Parameters.AddRange(ps);
                sda.Fill(ds);
            }
            return ds;
        }
       
    }
}


       




你可能感兴趣的:(C#)