方法是静态的,
有没有更好的操作类推荐
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>
/// SqlHelper 摘要說明
/// </summary>
public class SqlHelper
{
private static readonly string strConn = ConfigurationManager.AppSettings["cnn"];
//private static readonly string strName = ConfigurationManager.AppSettings("userName").ToString();
// private static readonly string strPass = ConfigurationManager.AppSettings("userPass").ToString();
//
// TODO: 在此處添加構造函數邏輯
//
public SqlHelper()
{
}
public static SqlDataReader ExcuteRead(string nText, CommandType nType, SqlParameter[] paras)
{ SqlConnection conn = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
try
{
PrepareCommand(conn, cmd, null, nType, nText, paras);
SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear(); return dr;
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex); }
}
public static int ExcuteNonQurey(string nText, CommandType nType, SqlParameter[] paras)
{ SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(strConn))
{ PrepareCommand(conn, cmd, null, nType, nText, paras);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows; }
}
public static object ExcuteSclare(string nText, CommandType nType, SqlParameter[] paras)
{
SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(strConn))
{ PrepareCommand(conn, cmd, null, nType, nText, paras);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return obj; }
}
public static DataSet ExcuteGetDataset(string nText, CommandType nType, SqlParameter[] paras)
{ SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
DataSet ds = new DataSet();
try
{
PrepareCommand(con, cmd, null, nType, nText, paras);
SqlDataAdapter sqlAdapter = new SqlDataAdapter(cmd);
sqlAdapter.Fill(ds, "table1"); cmd.Parameters.Clear(); return ds;
}
catch (SqlException ex)
{
throw new Exception(ex.Message, ex);
}
}
public static void PrepareCommand(SqlConnection con, SqlCommand cmd, SqlTransaction trans, CommandType nType, string nText, SqlParameter[] paras)
{ if (con.State != ConnectionState.Open)
{
con.Open();
}
cmd.Connection = con;
cmd.CommandText = nText;
if (trans != null)
{
cmd.Transaction = trans;
}
cmd.CommandType = nType;
if (paras != null)
{
foreach (SqlParameter para in paras)
{ cmd.Parameters.Add(para);
}
}
}
private static DataSet GetDataSet(string sql)
{
//constring = System.Configuration.ConfigurationSettings.AppSettings("ConnectionString")
SqlDataAdapter sda = new SqlDataAdapter(sql, strConn);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
}
解决方案 »
- .net运行时出现“/”应用程序中的服务器错误一般是何所致
- mssql导入导出数据出错,提示登录‘帐号名’不存在
- 请问ASP支持CSS Hack吗?
- sql 服务器角色 允许权限 应该选什么
- VS 2003+SQL2000 数据绑定和修改!
- 【求助100分】各位老大,有没有好的办法??
- gridview导出excel文件时 footer里的label无内容
- 如何把DataGrid控件的全部功能绑定到DropDownList的下拉框下?
- 关于,net 中的内框架页面如何跳转的问题
- 100分求关于DataList邦定XML的问题?
- 如何动态构造dropdownlist控件?
- 本人新手~想求几个asp.net技术群 谋求学习~请有群的朋友帮帮忙!
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.SqlClient;
using System.Web;
namespace DataAccess
{
public sealed class DataAccess
{
DataAccess()
{ }
static readonly DataAccess da = new DataAccess();
public static DataAccess GetDb
{
get
{
return da;
}
}
public static readonly Database db = DatabaseFactory.CreateDatabase();
public IList QueryDataSet(DbCommand cmd)
{
return db.ExecuteDataSet(cmd).Tables[0].DefaultView as IList;
}
public DbDataReader QueryDataReder(DbCommand cmd)
{
return db.ExecuteReader(cmd) as DbDataReader;
}
public bool Add<T>(T t) where T : Castle.ActiveRecord.ActiveRecordBase
{
try
{
t.Create();
return true;
}
catch
{
return false;
}
}
public bool Update<T>(T t) where T : Castle.ActiveRecord.ActiveRecordBase
{
try
{
t.Update();
return true;
}
catch
{
return false;
}
}
public bool Delete<T>(T t) where T : Castle.ActiveRecord.ActiveRecordBase
{
try
{
t.Delete();
return true;
}
catch
{
return false;
}
}
{
/////相关代码
}
个人认为这样好一点
using(SqlHelper dd=new SqlHelper(..)),这样的话就不需要去自己数据库的关闭了,调用完后程序会自动关闭数据的连接.