/*
/' `\/ `.
. .' : `. `.
\\.' , `.` `.
`. ,___/|\. `. :
. \, .'./ ' '\ , '
.\ . \_.~ _; ; \/'.
`\ ..._`. : /.. ../
/' _._ \. ~ .' `\:
/'.'@ ` .---. `.
.' : ' @ `.\. \
/ ./`.._./ ~ . :\ `. __
.' / ( \....' `. .' /' `.
/'''\ .' `. / \ : ;' .' ..:
.' ; `\; : : : : .' : ; :
: `\. `\. ; : \.' " ' ;
`. `. \ / s . / `. .'
` . `. `\ `. ; /' ;___ ;
`. `. `. ` ; ;:__..'
`. `. `. :` ': _.' .' ; :
`. `. .\x./-`--...../' ; :
`. ..-:..-' ( :
`---'`. `; :
`. `,.. : :
`. `. `.___;
`. `.
`. `;
`-.,'
女神保佑 永无BUG
*/ public static class SQLHelper
{
private static SqlConnection Con;
private static SqlCommand cmd;
private static DataTable dt;
private static DataSet ds;
private static SqlDataAdapter da;
private static string sqlstr;
private static void SQLHelper()
{
sqlstr = "";
}
/// <summary>
/// 执行一个sql语句,返回第一行第一个值
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static object GetFirstValue(string sql)
{
using (Con = new SqlConnection(sqlstr))
{
Con.Open();
cmd = new SqlCommand(sql, Con);
return cmd.ExecuteScalar();
}
}
/// <summary>
/// 执行一个sql语句,返回受影响行数
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static int GetNonQuery(string sql)
{
using (Con = new SqlConnection(sqlstr))
{
Con.Open();
cmd = new SqlCommand(sql, Con);
return cmd.ExecuteNonQuery();
}
}
/// <summary>
/// 离线模式填充datatable并返回,数据少可以用此方法
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataTable GetDataTableA(string sql)
{
using (Con = new SqlConnection(sqlstr))
{
da = new SqlDataAdapter(sql, Con);
dt = new DataTable();
da.Fill(dt);
return dt;
}
} /// <summary>
/// 根据sql,填充datatable并返回。数据多的时候可以用此方法
/// </summary>
/// <param name="sql"></param>
/// <returns></returns>
public static DataTable GetDataTableB(string sql)
{
using (Con = new SqlConnection(sqlstr))
{
Con.Open();
cmd = new SqlCommand(sql, Con);
dt = new DataTable();
SqlDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
Con.Close();
return dt;
}
}
/// <summary>
/// 传入sql语句集,返回一个dataset
/// </summary>
/// <param name="sqllist"></param>
/// <returns></returns>
public static DataSet GetDataSet(List<string> sqllist)
{
using (Con = new SqlConnection(sqlstr))
{
Con.Open();
ds = new DataSet();
foreach (string c in sqllist)
{
cmd = new SqlCommand(c, Con);
dt = new DataTable();
SqlDataReader reader = cmd.ExecuteReader();
dt.Load(reader);
ds.Tables.Add(dt);
}
Con.Close();
return ds;
}
}
/// <summary>
/// 在事务中执行多条sql语句
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static bool ExecSQLSList(List<string> list)
{
using (Con = new SqlConnection(sqlstr))
{
Con.Open();
SqlTransaction tran = Con.BeginTransaction();
try
{
foreach (string sql in list)
{
cmd = new SqlCommand(sql, Con);
cmd.ExecuteNonQuery();
}
tran.Commit();
Con.Close();
return true; }
catch
{
tran.Rollback();
return false;
}
finally
{
Con.Close();
Con.Dispose();
}
} } /// <summary>
/// 执行一个不带参数的存储过程,返回datable
/// </summary>
/// <param name="StoredProcedureName"></param>
/// <returns></returns>
public static DataTable GetDataTableByStoredProcedure(string StoredProcedureName)
{
using (Con = new SqlConnection(sqlstr))
{
Con.Open();
cmd = new SqlCommand();
cmd.CommandText = StoredProcedureName;
cmd.CommandType = CommandType.StoredProcedure;
dt = new DataTable();
dt.Load(cmd.ExecuteReader());
return dt;
}
} }

备用~

自己写的操作sql的公共类的更多相关文章

  1. 用于JDBC操作数据库的公共类

    /* * @(#)CommonSql.java 2011-9-5 * * Copyright 2011 Bianjing,All rights reserved. */ import java.sql ...

  2. C#操作SQL Server通用类

    using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...

  3. 操作Sql数据库帮助类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. C# 操作SQL Server 公共库

    基本够用,存一下,以后找起来方便 public abstract class DB { //数据库连接字符串(config来配置) protected static string connection ...

  5. 写一个操作 .ini文件的类

    class IniHelp { private string iniPath; [DllImport("kernel32")] private static extern long ...

  6. 自己封装的C#操作redis公共类

    关于C#操作redis公共类,网上有很多版本,每个版本我都看了,发觉还是不够完美,都存在一个问题,只能操作单一的缓存数据库 redis指令支持上,这里可以自己去扩展,下面分享下我近期封装的一个redi ...

  7. dedecms清空所有文章怎么操作?sql语句如何写?

    小C新建了一个站,确切的说是复制,出于seo考虑,决定清空所有文章,那么dedecms清空所有文章怎么操作?sql语句如何写呢?特别提醒:修改之前一定要先做好备份,以防万一!下面的语句在迫不得已的情况 ...

  8. C#通过SC命令和静态公共类来操作Windows服务

    调用的Windows服务应用程序网址:http://www.cnblogs.com/pingming/p/5115304.html 一.引用 二.公共静态类:可以单独放到类库里 using Syste ...

  9. php操作oracle的方法类集全

    在网上开始找php中操作oracle的方法类~ 果然找到一个用php+oracle制作email表以及插入查询的教程,赶忙点开来看,从头到尾仔细的看了一遍,还没开始操作,便觉得收获很大了.地址在此:h ...

随机推荐

  1. WampServer下如何实现多域名配置

    原文:WampServer下如何实现多域名配置 之前在学习跨域的时候,我写过一篇叫做WampServer下使用多端口访问的文章,默认的 localhost 采用的是 80 端口,能使用多端口访问的核心 ...

  2. 可爱的 Python : Python中的函数式编程,第三部分

    英文原文:Charming Python: Functional programming in Python, Part 3,翻译:开源中国 摘要:  作者David Mertz在其文章<可爱的 ...

  3. Mysql两种存储引擎的优缺点

    MyISAM引擎是一种非事务性的引擎,提供高速存储和检索,以及全文搜索能力,适合数据仓库等查询频繁的应用.MyISAM中,一个table实际保存为三个文件,.frm存储表定义,.MYD存储数据,.MY ...

  4. SharePoint 2013 设置自己定义布局页

    在SharePoint中.我们常常须要自己定义登陆页面.错误页面.拒绝訪问等:不知道大家怎样操作,曾经自己常常在原来页面改或者跳转.事实上SharePoint为我们提供了PowerShell命令,来改 ...

  5. BNUOJ27873:A Special "Happy Birthday" Song!!!

    There are n people (excluding myself) in my 30th birthday party. They sing the traditional "hap ...

  6. JAVA策略模式

    <JAVA与模式>之策略模式 在阎宏博士的<JAVA与模式>一书中开头是这样描述策略(Strategy)模式的: 策略模式属于对象的行为模式.其用意是针对一组算法,将每一个算法 ...

  7. JqueryEasyUI中combox的数据不显示

    在做一个用jeasyui做的项目中遇到一个问题,对应的数据取出来了但是数据项一直出不来后来经过检查原来是赋值的时候对应的数组元素的名称写的有问题,以后一定要注意对应的问题,不能犯类似的错误 funct ...

  8. SharePoint 2013设置“以其他用户身份登录”

    登录web服务器,打开位于“C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\TEMPLATE\CONTR ...

  9. 学习Javascript DOM 编程艺术的一点心得

    最近又看了一遍JS DOM编程艺术,照例来写一写读后感. 其实,我从中学到最深的是几个概念:1.平稳退化.当浏览器并不支持JS的时候网页的基本核心功能是还可以用的:2.逐渐增强.在原始的信息层上用其他 ...

  10. leetcode Divide Two Integers python

    class Solution(object): def divide(self, dividend, divisor): """ :type dividend: int ...