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访问的完整类的更多相关文章

  1. 【转载】微软官方提供的Sqlserver数据库操作帮助类SQLHelper类

    在.NET平台中,C#语言一般使用ADO.NET组件来操作Sqlserver数据库,通过ADO.NET组件可以实现连接数据库.查询数据集.执行SQL语句以及关闭数据库连接等操作,为此网上有很多开发者自 ...

  2. C#操作xml完整类文件

    C#操作xml完整类文件 xml_oper.cs using ...System; using System.Data; using System.Web; using System.Xml; /** ...

  3. MFC一个类访问另一个类成员对象的成员变量值

    MFC中一个类要访问另外一个类的的对象的成员变量值,这就需要获得原来那个类对象的指针,其实有好几种方法都可以实现. 比如维护一个单例模式.设置静态变量等等.我们这里举个列子,实现多个类之间的相互访问. ...

  4. winform中利用反射实现泛型数据访问对象基类(1)

    考虑到软件使用在客户端,同时想简化代码的实现,就写了一个泛型的数据访问对象基类,并不是特别健全,按道理应该参数化的方式实现insert和update,暂未使用参数化,抽时间改进. /// <su ...

  5. C++ - 派生类访问模板基类(templatized base class)命名

    派生类访问模板基类(templatized base class)命名 本文地址: http://blog.csdn.net/caroline_wendy/article/details/239936 ...

  6. SQLServer访问Oracle查询性能问题解决

    原文:SQLServer访问Oracle查询性能问题解决 1. 问题 系统有个模块,需要查询Oracle数据库中的数据.目前是通过建立链接服务器实现的. SQLServer访问Oracle实现 可参考 ...

  7. 【JVM虚拟机】(6)---深入理解Class中访问标志、类索引、父类索引、接口索引

    JVM(6)访问标志,类索引 上一篇博客讲[JVM虚拟机](5)---深入理解JVM-Class中常量池 我们知道一个class文件正常可以分为7个部分: 魔数与class文件版本 常量池 访问标志 ...

  8. PHP 获取当前访问的完整URL

    代码如下: <?php // php 获取当前访问的完整url function GetCurUrl() { $url = 'http://'; if(isset($_SERVER['HTTPS ...

  9. 4.3.6 对象的界定通过编写接口来访问带这类命名结构的表会出问题。如前所述,SQL Server的灵活性不应用作编写错误代码或创建问题对象的借口。 注意在使用Management Studio的脚本工具时,SQL Server会界定所有的对象。这不是因为这么做是必须的,也不是编写代码的最佳方式,而是因为在界定符中封装所有的对象,比编写脚本引擎来查找需要界定的对象更容易。

    如前所述,在创建对象时,最好避免使用内嵌的空格或保留字作为对象名,但设计人员可能并没有遵守这个最佳实践原则.例如,我当前使用的数据库中有一个审核表名为Transaction,但是Transaction ...

随机推荐

  1. vlc+flv.js 摄像头 H5 直播

    背景 业务需求:用最短的时间搞定摄像头直播到Web页面.因为没有过这方面经验,所以走了很多弯路,其实也不算弯路吧,大部分时间花在学习基础概念,寻找快速方案中.惯性思维想当然的以为找组件,配地址就能搞定 ...

  2. Beam Search快速理解及代码解析(上)

    Beam Search 简单介绍一下在文本生成任务中常用的解码策略Beam Search(集束搜索). 生成式任务相比普通的分类.tagging等NLP任务会复杂不少.在生成的时候,模型的输出是一个时 ...

  3. .Net Core微服务——自动收缩、健康检查:Consul(三)

    继续上一篇的话题,顺便放上一篇的传送门:点这里. 健康检查 经过之前的操作,我的consul已经支持自动扩展,并且调用也很靠谱.但是这里有个问题,一旦服务列表里的某个服务挂了,consul并不知道,还 ...

  4. 电子物流中的EDI 应用

    电子物流中的EDI 应用 背景 EDI 全称是Electronic data interchange, 即电子数据交换.在传统企业里,很多流程上的操作或者通信一般是由纸质媒介完成的,比如说采购订单.发 ...

  5. erase

    erase详细解释及原理 我们先定义一个字符串string string.erase(iterator) iterator表示要删除元素的迭代器. string.erase(it_begin,it_e ...

  6. redis数据类型及应用场景

    0.key的通用操作 KEYS * keys a keys a* 查看已存在所有键的名字 ****TYPE 返回键所存储值的类型 ****EXPIRE\ PEXPIRE 以秒\毫秒设定生存时间 *** ...

  7. java类与对象基础篇

    java面向对象基础篇 面向对象程序设计(Object Oriented Proframming ,OOP) 面向对象的本质是:以类的方式组织代码,以对象的方式组织(封装)数据. 面向对象的核心思想是 ...

  8. 【Azure 应用服务】App Service 运行状况健康检查功能简介 (Health check)

    通过Azure App Service门户,启用Health Check来监视应用服务的实例,当发现其中一个实例处于不健康(unhealthy)状态时,通过重新路由(即把有问题的实例从负载均衡器中移除 ...

  9. SQL SERVER获取某张表创建的索引

    1 SELECT 索引名称=a.name 2 ,表名=c.name 3 ,索引字段名=d.name 4 ,索引字段位置=d.colid 5 FROM sysindexes a 6 JOIN sysin ...

  10. Android Jetpack 架构组件最佳实践之“网抑云”APP

    背景 近几年,Android 相关的新技术层出不穷.往往这个技术还没学完,下一个新技术又出来了.很多人都是一脸黑人问号? 不少开发者甚至开始哀嚎:"求求你们别再创造新技术了,我们学不动了!& ...