| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 675 人关注过本帖
标题:[原创]这是我自己刚写的一个SQL操作小类
取消只看楼主 加入收藏
virusswb
Rank: 1
等 级:新手上路
威 望:1
帖 子:342
专家分:0
注 册:2005-8-6
结帖率:0
收藏
 问题点数:0 回复次数:1 
[原创]这是我自己刚写的一个SQL操作小类

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;
/// <summary>
/// sqlDB 的摘要说明
/// </summary>
public class sqlDB
{
//private static SqlConnection con;
protected static SqlConnection sqlcon=new SqlConnection();
protected static SqlCommand sqlcmd=new SqlCommand();
public sqlDB()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private static void openConnection()
{
if (sqlcon.State == ConnectionState.Closed)
{
sqlcon.ConnectionString = ConfigurationManager.ConnectionStrings["VoteDBConnectionString"].ConnectionString;
sqlcmd.Connection = sqlcon;
try
{
sqlcon.Open();
}
catch (Exception ex)
{ throw new Exception(ex.Message); }
}
}
private static void closeConnection()
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
sqlcon.Dispose();
sqlcmd.Dispose();
}
}
public static void ExecuteSQL(string SQLstring)
{
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlcmd.ExecuteNonQuery();
}
catch (Exception ex)
{ throw new Exception(ex.Message); }
finally
{ closeConnection(); }
}
public static SqlDataReader SQLDataReader(string SQLstring)
{
SqlDataReader sqldr = null;
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqldr = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);
}
catch (Exception ex)
{
try
{
sqldr.Close();
closeConnection();
}
catch { }
}
return sqldr;
}
public static void SQLDataReader(string SQLstring, ref SqlDataReader sqldr)
{
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqldr = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);

}
catch (Exception ex)
{
try
{
if (sqldr != null && !sqldr.IsClosed)
sqldr.Close();
}
catch { }
finally
{
closeConnection();
}
}
}
public static DataSet SQLDataSet(string SQLstring)
{
DataSet sqlds = new DataSet();
SqlDataAdapter sqlda = new SqlDataAdapter();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqlds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{ closeConnection(); }
return sqlds;
}
public static void SQLDataSet(string SQLstring, ref DataSet sqlds)
{
SqlDataAdapter sqlda = new SqlDataAdapter();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqlds);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{ closeConnection(); }
}
public static DataTable SQLDataTable(string SQLstring)
{
DataTable sqldt = new DataTable();
SqlDataAdapter sqlda = new SqlDataAdapter();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqldt);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
}
public static void SQLDataTable(string SQLstring, ref DataTable sqldt)
{
SqlDataAdapter sqlda = new SqlDataAdapter();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqldt);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{ closeConnection(); }
}
public static DataView SQLDataView(string SQLstring)
{
SqlDataAdapter sqlda = new SqlDataAdapter();
DataView sqldv = new DataView();
DataSet sqlds = new DataSet();
try
{
openConnection();
sqlcmd.CommandType = CommandType.Text;
sqlcmd.CommandText = SQLstring;
sqlda.SelectCommand = sqlcmd;
sqlda.Fill(sqlds);
sqldv = sqlds.Tables[0].DefaultView;
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
closeConnection();
}
return sqldv;
}
}
还不成熟,各位尽管批评,谢谢

763ONVco.txt (5.63 KB) [原创]这是我自己刚写的一个SQL操作小类


[此贴子已经被作者于2006-11-19 15:20:23编辑过]

搜索更多相关主题的帖子: using SQL System Web summary 
2006-11-19 15:17
virusswb
Rank: 1
等 级:新手上路
威 望:1
帖 子:342
专家分:0
注 册:2005-8-6
收藏
得分:0 
什么意思
都是静态的
直接使用类名不久可以了吗
我还不太明白你的意思
呵呵
我学了一年了,很多都不懂
很苦恼啊
怎么办呢
版主
你给点意见吧
谢谢

jorden008@
2006-11-20 11:20
快速回复:[原创]这是我自己刚写的一个SQL操作小类
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.038948 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved