SQLHelper



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

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionStrings>
    <add name="sql" connectionString="server=.;database=MySecondDB;uid=sa;pwd=520;"/>
  </connectionStrings>
</configuration>


//-------------------------------------------------------------------------------------------------------------------------------

//  主要包含两个类: Program.cs  &  SQLHelper.cs

//   1.  Program.cs 

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
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
            Console.ReadKey();           
        }
    }
}


// 2  SQLHelper.cs

using System;
using System.Collections.Generic;
using System.Configuration;
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
       
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值