惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
GbyAI
GbyAI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
SegmentFault 最新的问题
Y
Y Combinator Blog
Recorded Future
Recorded Future
博客园 - Franky
I
InfoQ
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
阮一峰的网络日志
阮一峰的网络日志
T
Tailwind CSS Blog
Cyberwarzone
Cyberwarzone
The Register - Security
The Register - Security
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
P
Palo Alto Networks Blog
G
GRAHAM CLULEY
Cloudbric
Cloudbric
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
C
Check Point Blog
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
W
WeLiveSecurity
T
Threat Research - Cisco Blogs
U
Unit 42
N
Netflix TechBlog - Medium
The Cloudflare Blog
Spread Privacy
Spread Privacy
Microsoft Azure Blog
Microsoft Azure Blog
美团技术团队
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
H
Heimdal Security Blog
TaoSecurity Blog
TaoSecurity Blog
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
B
Blog
S
Securelist
H
Hacker News: Front Page
Google Online Security Blog
Google Online Security Blog
G
Google Developers Blog

博客园 - 强悍的抽屉

基于 Dapper 的一个 DbUtils WebAPI 操作返回 c#版 mqtt 3.1.1 client 实现 mqtt 协议之 PINGREQ, PINGRESP httpWebRequest 文件下载 一个 go 文件服务器 ssdb MongoDB 刷新几次就报错 C# Win32API - 强悍的抽屉 - 博客园 回车跳转控件焦点 让程序只启动一次 -- Mutex C# 排序 WINDEF.h 变量类型 SqlHelper 数据库操作类 第一个 Windows 应用程序 JavaScript 字符串处理函数 - 强悍的抽屉 - 博客园 JavaScript 字符串函数扩充 - 强悍的抽屉 - 博客园 C# 字符串处理一些方法 希望找人一起写个 Ajax 的封装 几种流行的JS框架的选择
SqlHelper 数据库操作类2
强悍的抽屉 · 2009-03-02 · via 博客园 - 强悍的抽屉

using System;
using System.Data;
using System.Data.SqlClient;namespace lsb.DBUtility
{
    
/// <summary>
    
/// SQL Server 数据库操作类
    
/// </summary>
    public class SqlHelper
    {
        
private static string connectionString = "Data Source=.;Initial Catalog=FBMS;Integrated Security=True";/// <summary>
        
/// 执行查询, 返回单个值
        
/// </summary>
        public static object ExecuteScalar(string sql)
        {
            
using (SqlConnection conn = new SqlConnection(connectionString))
            {
                
using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    
try
                    {
                        conn.Open();
                        
return cmd.ExecuteScalar();
                    }
                    
catch
                    {
                        
throw;
                    }
                }
            }
        }
/// <summary>
        
/// 执行带参数查询, 返回单个值
        
/// </summary>
        public static object ExecuteScalar(string sql, SqlParameter[] cmdParms)
        {
            
using (SqlConnection conn = new SqlConnection(connectionString))
            {
                
using (SqlCommand cmd = new SqlCommand())
                {
                    
try
                    {
                        PrepareCommand(cmd, conn, 
null, sql, cmdParms);
                        
object obj = cmd.ExecuteScalar();
                        cmd.Parameters.Clear();
                        
return obj;
                    }
                    
catch
                    {
                        
throw;
                    }
                }
            }
        }
/// <summary>
        
/// 执行查询, 返回多个值
        
/// </summary>
        public static SqlDataReader ExecuteReader(string sql)
        {
            
using (SqlConnection conn = new SqlConnection(connectionString))
            {
                
using (SqlCommand cmd = new SqlCommand(sql, conn))
                {
                    
try
                    {
                        conn.Open();
                        
return cmd.ExecuteReader();
                    }
                    
catch
                    {
                        
throw;
                    }
                }
            }
        }
/// <summary>
        
/// 执行带参数查询, 返回多个值
        
/// </summary>
        public static SqlDataReader ExecuteReader(string sql, SqlParameter[] cmdParms)
        {
            
using (SqlConnection conn = new SqlConnection(connectionString))
            {
                
using (SqlCommand cmd = new SqlCommand())
                {
                    
try
                    {
                        PrepareCommand(cmd, conn, 
null, sql, cmdParms);
                        SqlDataReader sdr 
= cmd.ExecuteReader();
                        cmd.Parameters.Clear();
                        
return sdr;
                    }
                    
catch
                    {
                        
throw;
                    }
                }
            }
        }
/// <summary>
        
/// 执行非查询, 返回影响行数
        
/// </summary>
        public static int ExceuteNonQuery(string sql, params SqlParameter[] cmdParms)
        {
            
using (SqlConnection conn = new SqlConnection(connectionString))
            {
                
using (SqlCommand cmd = new SqlCommand())
                {
                    
try
                    {
                        PrepareCommand(cmd, conn, 
null, sql, cmdParms);
                        
int rows = cmd.ExecuteNonQuery();
                        cmd.Parameters.Clear();
                        
return rows;
                    }
                    
catch
                    {
                        
throw;
                    }
                }
            }
        }
/// <summary>
        
/// 
        
/// </summary>
        
/// <param name="cmd"></param>
        
/// <param name="conn"></param>
        
/// <param name="trans"></param>
        
/// <param name="cmdText"></param>
        
/// <param name="cmdParms"></param>
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, string cmdText, SqlParameter[] cmdParms)
        {
            
if ((conn.State == ConnectionState.Broken) || (conn.State == ConnectionState.Closed))
            {
                conn.Open();
            }

            cmd.Connection 

= conn;
            cmd.CommandText 
= cmdText;if (trans != null)
                cmd.Transaction 
= trans;

            cmd.CommandType 

= CommandType.Text;if (cmdParms != null)
            {
                
foreach (SqlParameter parm in cmdParms)
                {
                    
if ((parm.Direction == ParameterDirection.InputOutput || parm.Direction == ParameterDirection.Input) && (parm.Value == null))
                    {
                        parm.Value 
= DBNull.Value;
                    }
                    cmd.Parameters.Add(parm);
                }
            }
        }
    }
}