SQLHelper如何使用(急,!!!!!!!!在线等)
我是个初学者,想用三层架构去做个网站,我做DAL层,已经加载了SQLHelper.dll。然后做什么就不知道了,是不是要在web.config里面设置一些东西?怎么能简单明了的写出来,存储过程我已经写好了,但是数据库怎么和SQLHelper连上就不知道了,完全搞不懂了,谢谢各位大侠 --------------------编程问答-------------------- dll的? 汗。 cs的吧。。。。 难道我落伍了 --------------------编程问答-------------------- SQLHelper.dll????哪里搞的?
Sqlhelper只是 个类,有一些方法,
调用类的方法就行了。 --------------------编程问答-------------------- 下载petshop看看
web.config配置连接字符串
http://topic.csdn.net/u/20090325/01/2d7737da-13bd-4330-98f4-f3fa45eef021.html --------------------编程问答-------------------- 大侠们,那怎么在SQLHELPER.cs文件里里面改下让他连到数据库呢,我看别人用下面的代码就能连到存储结构了:
var sqlHelper = new SQLHelper.SQLHelper();--------------------编程问答-------------------- 建议楼主使用源码的SQLHelper.cs方便查看。使用的时候把里面用到的数据库连接字符串修改成你用的就可以了。带中文注释的 --------------------编程问答-------------------- 同意楼上的,SQLHelper是有源码的,使用SQLHelper.cs --------------------编程问答-------------------- up --------------------编程问答-------------------- SQLHelper.cs一步一步跟踪一下他的代码就什么都明白了 --------------------编程问答-------------------- 下载个SQLHelper.cs类文件 你可以看到里边所有的方法 至于连接数据库最好在webconfig里 配置,给你个例子:
SqlDataReader rec;
SqlParameter[] ps = {
sqlHelper.CreateInParam("@startIndex", SqlDbType.Int, 4, startIndex),
sqlHelper.CreateInParam("@endIndex", SqlDbType.Int, 4, endIndex),
sqlHelper.CreateInParam("@doCount", SqlDbType.Bit, 4, 0)
};
try
{
sqlHelper.RunProc("Parabola_GetProblemsAdmin", ps, out rec); //存储过程接口
}
catch (Exception ex)
{
SystemError.CreateErrorLog(ex.Message);
throw new Exception(ex.Message, ex);
}
webconfig里在appsettings结点里注册
<appSettings>
<add key="ConnectQuickLoader" value="server=GAVIN\SQLEXPRESS;database=QuickLoader;uid=sa;pwd=111111;" />
</appSettings>
server=这里必须是数据库实例名 打开sqlserver看一下
代码里:
public static string ConnQuickLoader
{
get { return System.Configuration.ConfigurationManager.AppSettings["ConnectQuickLoader"].ToString() ; }
}
public static DataSet GetFileRecord (int fileId)
{
DataSet dsFileRecord;
try
{
DataSet ds = SqlHelper.ExecuteDataset(ConnQuickLoader, CommandType.Text,"select * from QuickLoader_FileInfo where id="+fileId.ToString());
dsFileRecord = ds;
}
catch (Exception ex)
{
throw new Exception("GetFileRecord::A database error occurred, please try again later. Details: " + ex.Message);
}
return dsFileRecord;
}
试试吧 --------------------编程问答-------------------- SqlHelper.cs 从PetShop 4.0 中拷贝出来的,你直接用就行了!
//===============================================================================
// This file is based on the Microsoft Data Access Application Block for .NET
// For more information please go to
// http://msdn.microsoft.com/library/en-us/dnbda/html/daab-rm.asp
//===============================================================================
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace PetShop.DBUtility {
/// <summary>
/// The SqlHelper class is intended to encapsulate high performance,
/// scalable best practices for common uses of SqlClient.
/// </summary>
public abstract class SqlHelper {
//Database connection strings
public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.ConnectionStrings["SQLConnString1"].ConnectionString;
public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString2"].ConnectionString;
public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.ConnectionStrings["SQLConnString3"].ConnectionString;
public static readonly string ConnectionStringProfile = ConfigurationManager.ConnectionStrings["SQLProfileConnString"].ConnectionString;
// Hashtable to store cached parameters
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
/// <summary>
/// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">a valid connection string for a SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
/// <returns>an int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
--------------------编程问答-------------------- SqlCommand cmd = new SqlCommand();
using (SqlConnection conn = new SqlConnection(connectionString)) {
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset) against an existing database connection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">an existing database connection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
/// <returns>an int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// Execute a SqlCommand (that returns no resultset) using an existing SQL Transaction
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="trans">an existing sql transaction</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
/// <returns>an int representing the number of rows affected by the command</returns>
public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// Execute a SqlCommand that returns a resultset against the database specified in the connection string
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">a valid connection string for a SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
/// <returns>A SqlDataReader containing the results</returns>
public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection(connectionString);
// we use a try/catch here because if the method throws an exception we want to
// close the connection throw code, because no datareader will exist, hence the
// commandBehaviour.CloseConnection will not work
try {
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch {
conn.Close();
throw;
}
}
/// <summary>
/// Execute a SqlCommand that returns the first column of the first record against the database specified in the connection string
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="connectionString">a valid connection string for a SqlConnection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
SqlCommand cmd = new SqlCommand();
using (SqlConnection connection = new SqlConnection(connectionString)) {
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// Execute a SqlCommand that returns the first column of the first record against an existing database connection
/// using the provided parameters.
/// </summary>
/// <remarks>
/// e.g.:
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));
/// </remarks>
/// <param name="conn">an existing database connection</param>
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>
/// <param name="commandText">the stored procedure name or T-SQL command</param>
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters) {
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// add parameter array to the cache
/// </summary>
/// <param name="cacheKey">Key to the parameter cache</param>
/// <param name="cmdParms">an array of SqlParamters to be cached</param>
public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters) {
parmCache[cacheKey] = commandParameters;
}
/// <summary>
/// Retrieve cached parameters
/// </summary>
/// <param name="cacheKey">key used to lookup parameters</param>
/// <returns>Cached SqlParamters array</returns>
public static SqlParameter[] GetCachedParameters(string cacheKey) {
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];
if (cachedParms == null)
return null;
SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];
for (int i = 0, j = cachedParms.Length; i < j; i++)
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();
return clonedParms;
}
/// <summary>
/// Prepare a command for execution
/// </summary>
/// <param name="cmd">SqlCommand object</param>
/// <param name="conn">SqlConnection object</param>
/// <param name="trans">SqlTransaction object</param>
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>
/// <param name="cmdText">Command text, e.g. Select * from Products</param>
/// <param name="cmdParms">SqlParameters to use in the command</param>
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms) {
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null) {
foreach (SqlParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
}
} --------------------编程问答-------------------- 顶,我也不知道sqlhelper是怎么用的。 我搞不懂,他们说只要操作数据库的就 连接到SQLhelper
怎么用 ? --------------------编程问答-------------------- up --------------------编程问答-------------------- 建议LZ还是先补补基础。 --------------------编程问答-------------------- 参考FortuneBase中
FortuneBase2(Oracle11g)\FortuneDomain\Common\DAAB\SQLHelper.cs
参考地址
www.cnblogs.com/mail-ricklee --------------------编程问答--------------------
up --------------------编程问答--------------------
建议LZ还是先补补基础 --------------------编程问答--------------------
有什么好书可以补基础? --------------------编程问答-------------------- 别指望有什么万能好书能救你
书都是一本一本看过来的
多看几本就好了
建议看老外的
--------------------编程问答--------------------
顶这个。 --------------------编程问答-------------------- 这个组件似乎写的复杂了,本人也写了一个,只有四个方法,但可以对各种数据库进行增删查改,使用方法超级简单,详见:http://blog.csdn.net/xxfaxy/archive/2010/12/16/6080798.aspx,我宣传哈我的组件,萝卜芹菜各有所爱哈。 --------------------编程问答-------------------- bin里面调用 类文件头引用命名空间
LZ明显对三层不清楚 --------------------编程问答-------------------- 在sqlhelper是一个类,类里面是你访问数据库和操作数据库的方法。 --------------------编程问答-------------------- 写好过后在DAL里面引用下。调用里面的想法方法进行增删改查操作 --------------------编程问答-------------------- 你直接把sqlhelp的那个类文件放到你项目中就可以了!不是dll文件 --------------------编程问答-------------------- 一步步调试下就知道怎么用了 --------------------编程问答-------------------- 你还SqlHelper.dll??????
这东西是开源的,自己建类啊 --------------------编程问答-------------------- lz可以试着反编译一下。看一下里面都提供哪些可供外部调用的方法 --------------------编程问答-------------------- SQL有直接调用的控件了吗?
我都是动软生成的 --------------------编程问答-------------------- 。net 第三方dll多了去了 , SQLHelper.dll , xmlHelper.dll 什么的一大堆 --------------------编程问答-------------------- 参考petshop4.0的来看就好了,我想楼主,你调用方法总是会的把,sqlheper就是一个类,里面定义了方法,我想调用方法你应该还是会的把。 --------------------编程问答-------------------- 汗...,有直接的源代码呀 --------------------编程问答--------------------
+1 楼主果然是新手 --------------------编程问答-------------------- 下载个SQLHelper.cs类文件 你可以看到里边所有的方法 至于连接数据库最好在webconfig里 配置,给你个例子:
webconfig里在appsettings结点里注册
<appSettings>
<add key="ConnectQuickLoader" value="server=GAVIN\SQLEXPRESS;database=QuickLoader;uid=sa;pwd=111111;" />
</appSettings>
server=这里必须是数据库实例名 打开sqlserver看一下
代码里:
public static string ConnQuickLoader
{
get { return System.Configuration.ConfigurationManager.AppSettings["ConnectQuickLoader"].ToString() ; }
}
public static DataSet GetFileRecord (int fileId)
{
DataSet dsFileRecord;
try
{
DataSet ds = SqlHelper.ExecuteDataset(ConnQuickLoader, CommandType.Text,"select * from QuickLoader_FileInfo where id="+fileId.ToString());
dsFileRecord = ds;
}
catch (Exception ex)
{
throw new Exception("GetFileRecord::A database error occurred, please try again later. Details: " + ex.Message);
}
return dsFileRecord;
}
试试吧 --------------------编程问答-------------------- SQLHelper.cs的经典代码
分类: C#xuex 2009-02-24 20:32 993人阅读 评论(0) 收藏 举报
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace HelloWinForm.DBUtility
{
class SQLHelper
{
#region 通用方法
// 数据连接池
private SqlConnection con;
/// <summary>
/// 返回数据库连接字符串
/// </summary>
/// <returns></returns>
public static String GetSqlConnection()
{
String conn = ConfigurationManager.AppSettings["connectionString"].ToString();
return conn;
}
#endregion
#region 执行sql字符串
/// <summary>
/// 执行不带参数的SQL语句
/// </summary>
/// <param name="Sqlstr"></param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}
}
/// <summary>
/// 执行带参数的SQL语句
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <param name="param">参数对象数组</param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr, SqlParameter[] param)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
cmd.Parameters.AddRange(param);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
return 1;
}
}
/// <summary>
/// 返回DataReader
/// </summary>
/// <param name="Sqlstr"></param>
/// <returns></returns>
public static SqlDataReader ExecuteReader(String Sqlstr)
{
String ConnStr = GetSqlConnection();
SqlConnection conn = new SqlConnection(ConnStr);//返回DataReader时,是不可以用using()的
try
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
return cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);//关闭关联的Connection
}
catch //(Exception ex)
{
return null;
}
}
/// <summary>
/// 执行SQL语句并返回数据表
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <returns></returns>
public static DataTable ExecuteDt(String Sqlstr)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
DataTable dt = new DataTable();
conn.Open();
da.Fill(dt);
conn.Close();
return dt;
}
}
/// <summary>
/// 执行SQL语句并返回DataSet
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <returns></returns>
public static DataSet ExecuteDs(String Sqlstr)
{
String ConnStr = GetSqlConnection();
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
DataSet ds = new DataSet();
conn.Open();
da.Fill(ds);
conn.Close();
return ds;
}
}
#endregion
#region 操作存储过程
/// <summary>
/// 运行存储过程(已重载)
/// </summary>
/// <param name="procName">存储过程的名字</param>
/// <returns>存储过程的返回值</returns>
public int RunProc(string procName)
{
SqlCommand cmd = CreateCommand(procName, null);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters["ReturnValue"].Value;
}
/// <summary>
/// 运行存储过程(已重载)
/// </summary>
/// <param name="procName">存储过程的名字</param>
/// <param name="prams">存储过程的输入参数列表</param>
/// <returns>存储过程的返回值</returns>
public int RunProc(string procName, SqlParameter[] prams)
{
SqlCommand cmd = CreateCommand(procName, prams);
cmd.ExecuteNonQuery();
this.Close();
return (int)cmd.Parameters[0].Value;
}
/// <summary>
/// 运行存储过程(已重载)
/// </summary>
/// <param name="procName">存储过程的名字</param>
/// <param name="dataReader">结果集</param>
public void RunProc(string procName, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, null);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// <summary>
/// 运行存储过程(已重载)
/// </summary>
/// <param name="procName">存储过程的名字</param>
/// <param name="prams">存储过程的输入参数列表</param>
/// <param name="dataReader">结果集</param>
public void RunProc(string procName, SqlParameter[] prams, out SqlDataReader dataReader)
{
SqlCommand cmd = CreateCommand(procName, prams);
dataReader = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
}
/// <summary>
/// 创建Command对象用于访问存储过程
/// </summary>
/// <param name="procName">存储过程的名字</param>
/// <param name="prams">存储过程的输入参数列表</param>
/// <returns>Command对象</returns>
private SqlCommand CreateCommand(string procName, SqlParameter[] prams)
{
// 确定连接是打开的
Open();
//command = new SqlCommand( sprocName, new SqlConnection( ConfigManager.DALConnectionString ) );
SqlCommand cmd = new SqlCommand(procName, con);
cmd.CommandType = CommandType.StoredProcedure;
// 添加存储过程的输入参数列表
if (prams != null)
{
foreach (SqlParameter parameter in prams)
cmd.Parameters.Add(parameter);
}
// 返回Command对象
return cmd;
}
--------------------编程问答-------------------- /// <summary>
/// 创建输入参数
/// </summary>
/// <param name="ParamName">参数名</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Value">参数值</param>
/// <returns>新参数对象</returns>
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
/// 创建输出参数
/// </summary>
/// <param name="ParamName">参数名</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新参数对象</returns>
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// <summary>
/// 创建存储过程参数
/// </summary>
/// <param name="ParamName">参数名</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Direction">参数的方向(输入/输出)</param>
/// <param name="Value">参数值</param>
/// <returns>新参数对象</returns>
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (Size > 0)
{
param = new SqlParameter(ParamName, DbType, Size);
}
else
{
param = new SqlParameter(ParamName, DbType);
}
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
{
param.Value = Value;
}
return param;
}
#endregion
#region 数据库连接和关闭
/// <summary>
/// 打开连接池
/// </summary>
private void Open()
{
// 打开连接池
if (con == null)
{
//这里不仅需要using System.Configuration;还要在引用目录里添加
con = new SqlConnection(GetSqlConnection());
con.Open();
}
}
/// <summary>
/// 关闭连接池
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}
/// <summary>
/// 释放连接池
/// </summary>
public void Dispose()
{
// 确定连接已关闭
if (con != null)
{
con.Dispose();
con = null;
}
}
#endregion
}
} --------------------编程问答-------------------- /// <summary>
/// 创建输入参数
/// </summary>
/// <param name="ParamName">参数名</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Value">参数值</param>
/// <returns>新参数对象</returns>
public SqlParameter MakeInParam(string ParamName, SqlDbType DbType, int Size, object Value)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Input, Value);
}
/// <summary>
/// 创建输出参数
/// </summary>
/// <param name="ParamName">参数名</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <returns>新参数对象</returns>
public SqlParameter MakeOutParam(string ParamName, SqlDbType DbType, int Size)
{
return MakeParam(ParamName, DbType, Size, ParameterDirection.Output, null);
}
/// <summary>
/// 创建存储过程参数
/// </summary>
/// <param name="ParamName">参数名</param>
/// <param name="DbType">参数类型</param>
/// <param name="Size">参数大小</param>
/// <param name="Direction">参数的方向(输入/输出)</param>
/// <param name="Value">参数值</param>
/// <returns>新参数对象</returns>
public SqlParameter MakeParam(string ParamName, SqlDbType DbType, Int32 Size, ParameterDirection Direction, object Value)
{
SqlParameter param;
if (Size > 0)
{
param = new SqlParameter(ParamName, DbType, Size);
}
else
{
param = new SqlParameter(ParamName, DbType);
}
param.Direction = Direction;
if (!(Direction == ParameterDirection.Output && Value == null))
{
param.Value = Value;
}
return param;
}
#endregion
#region 数据库连接和关闭
/// <summary>
/// 打开连接池
/// </summary>
private void Open()
{
// 打开连接池
if (con == null)
{
//这里不仅需要using System.Configuration;还要在引用目录里添加
con = new SqlConnection(GetSqlConnection());
con.Open();
}
}
/// <summary>
/// 关闭连接池
/// </summary>
public void Close()
{
if (con != null)
con.Close();
}
/// <summary>
/// 释放连接池
/// </summary>
public void Dispose()
{
// 确定连接已关闭
if (con != null)
{
con.Dispose();
con = null;
}
}
#endregion
}
} --------------------编程问答-------------------- 把SQLHelper.cs类文件放在数据访问层dal里,在后台代码里先声明,再调用
例如:声明dfDAL.ContractFee dal = new dfDAL.ContractFee();再用dal调用
补充:.NET技术 , ASP.NET