C# 对SQlServer访问的完整类
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Net;
using System.ServiceModel.Web;
using System.Text; namespace Common
{
public class DBHelper
{
private string m_dbs; /// <summary>
/// 构造函数
/// </summary>
public DBHelper() { } /// <summary>
/// 构造函数
/// </summary>
/// <param name="connectString">数据库连接字符串</param>
public DBHelper (string connectString)
{
m_dbs = connectString;
}
public string ConnectString
{
get { return m_dbs; }
set { m_dbs = value; }
} /// <summary>
/// 插入并获取ID
/// </summary>
/// <param name="connectString">数据库连接字符串</param>
/// <param name="commandStr">SQL语句 包含获取ID的命令</param>
/// <returns>新记录的ID</returns>
public int ExecuteScalarInsert(string connectString,string commandStr)
{
string err = "";
int ret = 0;
if(string.IsNullOrEmpty (connectString ))
{
return -1;
}
using (SqlConnection dbc = new SqlConnection(connectString))
{
SqlCommand insert = new SqlCommand(commandStr, dbc); try
{
dbc.Open();
ret = Convert.ToInt32(insert.ExecuteScalar());
}
catch(Exception ex)
{
err = ex.Message;
}
} if (err.Length > 0)
{
throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
}
else
{
return ret;
}
} /// <summary>
/// 插入并获取ID
/// </summary>
/// <param name="commandStr">SQL语句 包含获取ID的命令</param>
/// <returns>新记录的ID</returns>
public int ExecuteScalarInsert(string commandStr)
{
string err = "";
int ret = 0;
if (string.IsNullOrEmpty(m_dbs ))
{
return -1;
}
using (SqlConnection dbc = new SqlConnection(m_dbs))
{
SqlCommand insert = new SqlCommand(commandStr, dbc); try
{
dbc.Open();
ret = Convert.ToInt32(insert.ExecuteScalar());
}
catch(Exception ex)
{
err = ex.Message;
}
} if (err.Length > 0)
{
throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
}
else
{
return ret;
}
} /// <summary>
/// 添加、删除、更新操作
/// </summary>
/// <param name="connectString">数据库连接字符串</param>
/// <param name="commandstr">SQL语句</param>
/// <returns>受影响的行数</returns>
public int CommandExecuteNonQuery(string connectString, string commandstr)
{
if(string .IsNullOrEmpty (connectString)||string .IsNullOrEmpty (commandstr ))
{
return -1;
}
string err = "";
int result = 0;
using (SqlConnection dbc = new SqlConnection(connectString))
{
SqlCommand command = new SqlCommand(commandstr, dbc);
try
{
dbc.Open();
result = command.ExecuteNonQuery();
}
catch(Exception ex)
{
err = ex.Message;
}
} if (err.Length > 0)
{
throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
}
else
{
return result;
} } /// <summary>
/// 添加、删除、更新操作
/// </summary>
/// <param name="commandstr">SQL语句</param>
/// <returns>受影响的行数</returns>
public int CommandExecuteNonQuery(string commandstr)
{
if (string.IsNullOrEmpty(m_dbs )||string .IsNullOrEmpty (commandstr ))
{
return -1;
}
string err = "";
int result = 0;
using (SqlConnection dbc = new SqlConnection(m_dbs))
{
SqlCommand command = new SqlCommand(commandstr, dbc);
try
{
dbc.Open();
result = command.ExecuteNonQuery();
}
catch (Exception ex)
{
err = ex.Message;
}
} if (err.Length > 0)
{
throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
}
else
{
return result;
}
} /// <summary>
/// 执行查询
/// </summary>
/// <param name="connectString">数据库连接字符串</param>
/// <param name="selectstr">SQL语句</param>
/// <returns>数据表</returns>
public DataTable GetCommand(string connectString, string selectstr)
{
if(string .IsNullOrEmpty (connectString )||string .IsNullOrEmpty (selectstr ))
{
return null;
}
DataTable table = new DataTable(); string err = "";
using (SqlConnection dbc = new SqlConnection(connectString))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(selectstr, dbc);
adapter.Fill(table);
}
catch(Exception ex)
{
err = ex.Message;
}
} if (err.Length > 0)
{
throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
}
else
{
return table;
}
} /// <summary>
/// 执行查询
/// </summary>
/// <param name="selectstr">SQL语句</param>
/// <returns>数据表</returns>
public DataTable GetCommand(string selectstr)
{
if (string.IsNullOrEmpty(m_dbs))
{
return null;
}
DataTable table = new DataTable();
string err = "";
using (SqlConnection dbc = new SqlConnection(m_dbs))
{
try
{
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = new SqlCommand(selectstr, dbc);
adapter.Fill(table);
}
catch(Exception ex)
{
err = ex.Message;
}
} if (err.Length > 0)
{
throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
}
else
{
return table;
}
} /// <summary>
/// 执行一个事务
/// </summary>
/// <param name="commands">事务中要执行的所有语句</param>
/// <returns>事务是否成功执行</returns>
public bool ExecuteTransaction(List <string >commands)
{
if(string .IsNullOrEmpty (m_dbs)||commands ==null )
{
return false;
} string err = "";
bool ret = false;
using (SqlConnection dbc = new SqlConnection(m_dbs))
{
dbc.Open();
using (SqlTransaction transaction = dbc.BeginTransaction())
{
try
{
foreach (string commandstr in commands)
{
SqlCommand command = new SqlCommand(commandstr, dbc);
command.Transaction = transaction;
command.ExecuteNonQuery();
}
transaction.Commit();
ret = true;
}
catch (Exception ex)
{
transaction.Rollback();
err = ex.Message;
}
}
} if (err.Length > 0)
{
throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
}
else
{
return ret;
}
} /// <summary>
/// 执行一个事务
/// </summary>
/// <param name="connectString">数据库连接字符串</param>
/// <param name="commands">事务中要执行的所有语句</param>
/// <returns>事务是否成功执行</returns>
public bool ExecuteTransaction(string connectString,List<string> commands)
{
if (string.IsNullOrEmpty(connectString) || commands == null)
{
return false;
} string err = "";
bool ret = false;
using (SqlConnection dbc = new SqlConnection(connectString))
{
dbc.Open();
using (SqlTransaction transaction = dbc.BeginTransaction())
{
try
{
foreach (string commandstr in commands)
{
SqlCommand command = new SqlCommand(commandstr, dbc);
command.Transaction = transaction;
command.ExecuteNonQuery();
}
transaction.Commit();
ret = true;
}
catch (Exception ex)
{
transaction.Rollback();
err = ex.Message;
}
}
} if (err.Length > 0)
{
throw new WebFaultException<SimpleException>(new SimpleException() { Message = err }, HttpStatusCode.InternalServerError);
}
else
{
return ret;
}
}
}
}
这种类写了又写,故作记录。
C# 对SQlServer访问的完整类的更多相关文章
- 【转载】微软官方提供的Sqlserver数据库操作帮助类SQLHelper类
在.NET平台中,C#语言一般使用ADO.NET组件来操作Sqlserver数据库,通过ADO.NET组件可以实现连接数据库.查询数据集.执行SQL语句以及关闭数据库连接等操作,为此网上有很多开发者自 ...
- C#操作xml完整类文件
C#操作xml完整类文件 xml_oper.cs using ...System; using System.Data; using System.Web; using System.Xml; /** ...
- MFC一个类访问另一个类成员对象的成员变量值
MFC中一个类要访问另外一个类的的对象的成员变量值,这就需要获得原来那个类对象的指针,其实有好几种方法都可以实现. 比如维护一个单例模式.设置静态变量等等.我们这里举个列子,实现多个类之间的相互访问. ...
- winform中利用反射实现泛型数据访问对象基类(1)
考虑到软件使用在客户端,同时想简化代码的实现,就写了一个泛型的数据访问对象基类,并不是特别健全,按道理应该参数化的方式实现insert和update,暂未使用参数化,抽时间改进. /// <su ...
- C++ - 派生类访问模板基类(templatized base class)命名
派生类访问模板基类(templatized base class)命名 本文地址: http://blog.csdn.net/caroline_wendy/article/details/239936 ...
- SQLServer访问Oracle查询性能问题解决
原文:SQLServer访问Oracle查询性能问题解决 1. 问题 系统有个模块,需要查询Oracle数据库中的数据.目前是通过建立链接服务器实现的. SQLServer访问Oracle实现 可参考 ...
- 【JVM虚拟机】(6)---深入理解Class中访问标志、类索引、父类索引、接口索引
JVM(6)访问标志,类索引 上一篇博客讲[JVM虚拟机](5)---深入理解JVM-Class中常量池 我们知道一个class文件正常可以分为7个部分: 魔数与class文件版本 常量池 访问标志 ...
- PHP 获取当前访问的完整URL
代码如下: <?php // php 获取当前访问的完整url function GetCurUrl() { $url = 'http://'; if(isset($_SERVER['HTTPS ...
- 4.3.6 对象的界定通过编写接口来访问带这类命名结构的表会出问题。如前所述,SQL Server的灵活性不应用作编写错误代码或创建问题对象的借口。 注意在使用Management Studio的脚本工具时,SQL Server会界定所有的对象。这不是因为这么做是必须的,也不是编写代码的最佳方式,而是因为在界定符中封装所有的对象,比编写脚本引擎来查找需要界定的对象更容易。
如前所述,在创建对象时,最好避免使用内嵌的空格或保留字作为对象名,但设计人员可能并没有遵守这个最佳实践原则.例如,我当前使用的数据库中有一个审核表名为Transaction,但是Transaction ...
随机推荐
- 网页如何嵌套网页__HTML框架
通过使用html框架,可以在一个浏览器窗口中展示多个页面.也就是一个html文件中可以引入多个html文件.在网页中框架使用比较少,但我们还是需要了解下. 方式1:iframe 使用iframe标签来 ...
- Cookie学习总结
Cookie简述 1. 概念 一种客户端会话技术,可以将一些少量的数据保存在客户端. 2. 快速使用 步骤 创建cookie对象,并设定数据 new Cookie(String name, Strin ...
- 记一次Hvv中遇到的API接口泄露而引起的一系列漏洞
引言 最近朋友跟我一起把之前废弃的公众号做起来了,更名为鹿鸣安全团队,后面陆续会更新个人笔记,有趣的渗透经历,内网渗透相关话题等,欢迎大家关注 前言 Hvv中的一个很有趣的漏洞挖掘过程,从一个简单的A ...
- 第十二篇 -- 关于U盘制作启动盘后在本机上显示不出来的解决方案
喜欢玩电脑的朋友应该都重装过系统,最常用的就是用U盘装系统.以前装系统都没问题,不过偶然一次发现了一个问题,就是那个被制作成启动盘的U盘,插在本机上只能显示EFI启动文件部分,而其他空间全都显示不出来 ...
- sql select 1 和 exists
SELECT * FROM LACOMMISION WHERE MANAGECOM LIKE'8694%' AND STATE='1' AND EXISTS(SELECT 1 FROM laagent ...
- 论文笔记:(2017NIPS)DeepSets
目录 摘要 一.引言 二.置换不变性和等变性 2.1 问题定义 2.2 结构 2.3 相关结果 三.Deep Sets 3.1 架构 3.2 其他相关工作 四.应用和实验结果 4.1 设置输入标量响应 ...
- Java中Arrays数组的定义与使用
初始化 Java中数组是固定长度,数组变量是个对象. NullPointerException 空指针异常. ArrayIndexOutOfBoundsException 索引值越界. 数组三种初始化 ...
- helm离线安装helm-push插件
helm-push版本:helm-push_0.9.0_linux_amd64 helm-push安装包 百度云: 链接: helm-push_0.9.0_linux_amd64 提取码: 26b ...
- QML用Instantiator动态创建顶级窗口
关键点 使用Model驱动Instantiator QML里面的hashmap: QQmlPropertyMap 上一次说到用 QQmlApplicationEngine 多次load的方式创建多个一 ...
- YsoSerial 工具常用Payload分析之Common-Collections2、4(五)
前言 Common-Collections <= 3.2.1 对应与YsoSerial为CC1.3.5.6.7 ,Commno-collections4.0对应与CC2.4. 这篇文章结束官方原 ...