代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
SQL操作类
作者:
yyj
/ 发布于
2012/10/8
/
528
<div>?using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient; using System.Collections; using System.Collections.Generic;</div> <div>/// <summary> /// Database 的摘要说明 /// </summary> public class Database { protected SqlConnection Connection; protected String ConnectionString; List<SqlParameter> paraArray = new List<SqlParameter>(); public Database() { //构造函数 ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; } ~Database() { //析构函数 try { if (Connection != null) Connection.Close(); } catch { } try { Dispose(); } catch { } } <div> public void OpenMasterStation() { ConnectionString = ConfigurationManager.ConnectionStrings["HNIUConnectionString"].ConnectionString; } <div> protected void Open() { // //保护方法,打开数据库连接。 if (Connection == null) { Connection = new SqlConnection(ConnectionString); } if (Connection.State.Equals(ConnectionState.Closed)) { Connection.Open(); } } <div> public void Close() { //公有方法,关闭数据库连接。 if (Connection != null) Connection.Close(); } <div> public void Dispose() { //公有方法,释放资源。 // 确保连接被关闭 if (Connection != null) { Connection.Dispose(); Connection = null; } } <div> //public void GetDataReader() //{ //公有方法,返回一个DataRead //Open(); //SqlCommand cmd = new SqlCommand(SqlString, Connection); //OleDbDataReader dr = cmd.ExecuteReader(); //return dr; //} </div> <div> public DataSet GetDataSet(String SqlString) { //公有方法,获取数据,返回一个DataSet。 Open(); SqlDataAdapter adapter = new SqlDataAdapter(SqlString, Connection); DataSet dataset = new DataSet(); adapter.Fill(dataset); Close(); return dataset; } <div> public int ExecuteSQL(String SqlString) { // 公有方法,执行Sql语句。 // <param name="SqlString">Sql语句</param> // <returns>对Update、Insert、Delete为影响到的行数,其他情况为-1</returns></div> <div> int count = -1; Open(); try { SqlCommand cmd = new SqlCommand(SqlString, Connection); count = cmd.ExecuteNonQuery(); } catch { count = -1; } finally { Close(); } return count; } public int ExecuteSQL(String SqlProcName, SqlParameter[] SqlParaArray) { // 公有方法,执行Sql语句。 // <param name="SqlProcName">存储过程名称</param> // <param name="SqlParaArray">参数数组</param> // <returns>对Update、Insert、Delete为影响到的行数,其他情况为-1</returns></div> <div> int count = -1; Open(); try { SqlCommand cmd = new SqlCommand(); cmd.CommandText = SqlProcName; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = Connection; cmd.Parameters.AddRange(SqlParaArray); count = cmd.ExecuteNonQuery(); } catch { count = -1; } finally { Close(); } return count; } public void AddSqlParaArray(string name,object value) { paraArray.Add(new SqlParameter(name,value)); } <div> public object ExecuteScalar(string SqlProcName) { // 公有方法,执行Sql语句。 // <param name="SqlProcName">存储过程名称</param> // <returns>返回单个值,如果没有查询结果返回为NULL</returns></div> <div> Open(); object result = null; try { SqlCommand cmd = new SqlCommand(); cmd.CommandText = SqlProcName; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = Connection; cmd.Parameters.AddRange(paraArray.ToArray()); result = cmd.ExecuteScalar(); } catch(Exception ee) { throw new Exception(ee.ToString()); } finally { Close(); //if (result == null) MsgInfo.showMessage("DataAccess", 5); } return result; } <div> public DataRow GetDataRow(string SqlProcName) { Open(); DataRow dr = null; DataSet ds = new DataSet(); try { SqlCommand cmd = new SqlCommand(); cmd.CommandText = SqlProcName; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = Connection; cmd.Parameters.AddRange(paraArray.ToArray()); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(ds); if (ds.Tables[0].Rows.Count > 0) dr = ds.Tables[0].Rows[0]; } catch { } Close(); return dr; } <div> public DataRow GetDataRow(String SqlProcName, SqlParameter[] SqlParaArray) { //公有方法,获取数据,返回一个DataRow。 DataSet dataset = GetDataSet(SqlProcName,SqlParaArray); dataset.CaseSensitive = false; if (dataset.Tables[0].Rows.Count > 0) { return dataset.Tables[0].Rows[0]; } else { return null; } } <div> public DataSet GetDataSet(String SqlProcName, SqlParameter[] SqlParaArray) { //公有方法,获取数据,返回一个DataSet。 Open(); SqlDataAdapter adapter = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = SqlProcName; cmd.Connection = Connection; cmd.Parameters.AddRange(SqlParaArray); cmd.CommandType = CommandType.StoredProcedure; adapter.SelectCommand = cmd; DataSet dataset = new DataSet(); adapter.Fill(dataset); Close(); return dataset; } <div> public int ExecuteNonQuery(String SqlProcName) { // 公有方法,执行Sql语句。 // <param name="SqlProcName">存储过程名称</param> // <returns>对Update、Insert、Delete为影响到的行数,其他情况为-1</returns></div> <div> int count = -1; Open(); try { SqlCommand cmd = new SqlCommand(); cmd.CommandText = SqlProcName; cmd.CommandType = CommandType.StoredProcedure; cmd.Connection = Connection; cmd.Parameters.AddRange(paraArray.ToArray()); count = cmd.ExecuteNonQuery(); } catch (Exception ee) { //MsgInfo.pushMsg(ee.ToString()); throw new Exception(ee.ToString()); } finally { Close(); } return count; } <div> public DataTable GetDataTable(String SqlProcName) { //公有方法,获取数据,返回一个DataTable。 DataTable dt = null; try { Open(); SqlDataAdapter adapter = new SqlDataAdapter(); SqlCommand cmd = new SqlCommand(); cmd.CommandText = SqlProcName; cmd.Connection = Connection; cmd.Parameters.AddRange(paraArray.ToArray()); cmd.CommandType = CommandType.StoredProcedure; adapter.SelectCommand = cmd; dt = new DataTable(); adapter.Fill(dt); } catch(Exception ee) { throw new Exception(ee.ToString()); } finally { Close(); } return dt; } } </div>
试试其它关键字
SQL操作类
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
yyj
贡献的其它代码
(
1
)
.
SQL操作类
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3