
标准的Sql添加 ,修改,删除,查询,单行单列查询
发布日期:2021-05-08 22:13:34
浏览次数:25
分类:精选文章
本文共 3132 字,大约阅读时间需要 10 分钟。
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Data.SqlClient;using System.Data;namespace DOT_DAL{ public class DBhelper { SqlConnection con = null; SqlCommand cmd = null; SqlDataAdapter sdater = null; DataTable table = null; string constring = "server=.;database=MyBack;uid=sa;pwd=123456"; //其中server后面跟的是你服务器的名字,database后面跟的是你要连接数据库的名字,SSPI不需要修改 ////// 执行查询语句 /// /// sql /// 参数 ///返回值 public DataTable select(string sql,Listparmaeter=null) { try { using (con = new SqlConnection(constring)) { con.Open(); using (cmd = new SqlCommand(sql, con)) { if (parmaeter != null) { cmd.Parameters.AddRange(parmaeter.ToArray()); } sdater = new SqlDataAdapter(sql, con); table = new DataTable(); sdater.Fill(table); } } } catch (Exception) { throw; } return table; } /// /// 查询单行单列 /// /// sql /// 参数 ///返回值 public object ExecuteScalar(string sql, Listparmaeter = null) { object obj=null; try { using (con = new SqlConnection(constring)) { con.Open(); using (cmd = new SqlCommand(sql, con)) { if (parmaeter != null) { cmd.Parameters.AddRange(parmaeter.ToArray()); } obj = cmd.ExecuteScalar(); } } } catch (Exception) { throw; } return obj; } /// /// 查询添加,修改,删除 /// /// sql /// 参数 ///返回值 public int ExecuteNonQuery(string sql, Listparmaeter = null) { int row = 0; try { using (con = new SqlConnection(constring)) { con.Open(); using (cmd = new SqlCommand(sql, con)) { if (parmaeter != null) { cmd.Parameters.AddRange(parmaeter.ToArray()); } row = cmd.ExecuteNonQuery(); } } } catch (Exception) { throw; } return row; } }}
发表评论
最新留言
留言是一种美德,欢迎回访!
[***.207.175.100]2025年04月13日 09时48分34秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
【Maven】POM基本概念
2021-05-09
【Java思考】Java 中的实参与形参之间的传递到底是值传递还是引用传递呢?
2021-05-09
【设计模式】单例模式
2021-05-09
远程触发Jenkins的Pipeline任务的并发问题处理
2021-05-09
entity framework core在独立类库下执行迁移操作
2021-05-09
Asp.Net Core 2.1+的视图缓存(响应缓存)
2021-05-09
【wp】HWS计划2021硬件安全冬令营线上选拔赛
2021-05-09
Ef+T4模板实现代码快速生成器
2021-05-09
JQuery选择器
2021-05-09
多线程之volatile关键字
2021-05-09
2.2.2原码补码移码的作用
2021-05-09
Java面试题:Servlet是线程安全的吗?
2021-05-09
Java集合总结系列2:Collection接口
2021-05-09
Linux学习总结(九)—— CentOS常用软件安装:中文输入法、Chrome
2021-05-09
比技术还重要的事
2021-05-09
linux线程调度策略
2021-05-09
软中断和实时性
2021-05-09
Linux探测工具BCC(可观测性)
2021-05-09
SNMP介绍及使用,超有用,建议收藏!
2021-05-09
HDU5589:Tree(莫队+01字典树)
2021-05-09