6个回答
展开全部
using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace DAL
{
/// <summary>
/// 数据库操作摘要
/// 定义:周龙
/// 2008/11/23
/// </summary>
public sealed class DBHelper
{
private static readonly string connectionStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
/// <summary>
/// 执行SQL语句,并返回受影响的行数
/// </summary>
/// <param name="cmdText">命令文本</param>
/// <param name="cmdType">命令类型</param>
/// <param name="sqlParams">参数</param>
/// <returns>返回受影响的行数</returns>
public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams)
{
using (SqlConnection conn = new SqlConnection(connectionStr))
{
SqlCommand sqlCmd = new SqlCommand();
PrepareCommand(conn, sqlCmd, null, cmdText, cmdType, sqlParams);
int val = sqlCmd.ExecuteNonQuery();
sqlCmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 执行SQL语句,并返回SqlDataReader对象
/// </summary>
/// <param name="cmdText">命令文本</param>
/// <param name="cmdType">命令类型</param>
/// <param name="sqlParams">参数</param>
/// <returns>返回SqlDataReader对象</returns>
public static SqlDataReader ExecuteReader(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams)
{
SqlConnection conn = new SqlConnection(connectionStr);
try
{
SqlCommand sqlCmd = new SqlCommand();
PrepareCommand(conn, sqlCmd, null, cmdText, cmdType, sqlParams);
SqlDataReader val = sqlCmd.ExecuteReader();
sqlCmd.Parameters.Clear();
return val;
}
catch(SqlException e)
{
conn.Close();
throw e;
}
}
/// <summary>
/// 执行Sql语句,并返回object
/// </summary>
/// <param name="cmdText">命令文本</param>
/// <param name="cmdType">命令类型</param>
/// <param name="sqlParams">参数</param>
/// <returns>返回object对象</returns>
public static object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams)
{
using (SqlConnection conn = new SqlConnection(connectionStr))
{
using (SqlCommand sqlCmd = new SqlCommand())
{
PrepareCommand(conn, sqlCmd, null, cmdText, cmdType, sqlParams);
object val = sqlCmd.ExecuteScalar();
sqlCmd.Parameters.Clear();
return val;
}
}
}
/// <summary>
/// 执行Sql语句,并返回DataSet
/// </summary>
/// <param name="cmdText">命令文本</param>
/// <param name="cmdType">命令类型</param>
/// <param name="sqlParams">参数</param>
/// <returns>返回DataSet对象</returns>
public static DataSet ExecuteDataSet(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams)
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(connectionStr))
{
using (SqlCommand sqlCmd = new SqlCommand(cmdText))
{
foreach (SqlParameter p in sqlParams)
{
sqlCmd.Parameters.Add(p);
}
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
try
{
da.Fill(ds);
}
catch(SqlException e)
{
throw e;
}
finally
{
sqlCmd.Parameters.Clear();
}
}
}
return ds;
}
/// <summary>
/// 执行之前的准备
/// </summary>
/// <param name="conn">数据库连接对象</param>
/// <param name="sqlCmd">数据库操作对象</param>
/// <param name="sqlTrans">事务</param>
/// <param name="cmdText">Sql语句或存储过程名称</param>
/// <param name="cmdType">执行类型</param>
/// <param name="sqlParams">参数</param>
private static void PrepareCommand(SqlConnection conn,SqlCommand sqlCmd,SqlTransaction sqlTrans,string cmdText, CommandType cmdType, SqlParameter[] sqlParams)
{
//打开连接
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
sqlCmd.Connection = conn;
sqlCmd.CommandText = cmdText;
sqlCmd.CommandType = cmdType;
//如果有事务,启动事务
if (sqlTrans != null)
{
sqlCmd.Transaction = sqlTrans;
}
//参数
if (sqlParams != null)
{
foreach (SqlParameter p in sqlParams)
{
sqlCmd.Parameters.Add(p);
}
}
}
/// <summary>
/// 使用事务执行多条SQL语句
/// </summary>
/// <param name="sqlTable">Hashtable: Key:执行的SQL语句, Value:存储多条SQL的参数</param>
public static void ExecuteTransaction(Hashtable sqlTable)
{
using (SqlConnection conn = new SqlConnection(connectionStr))
{
try
{
conn.Open();
}
catch(SqlException e)
{
System.Diagnostics.Debug.Print(e.Message);
}
using (SqlCommand cmd = conn.CreateCommand())
{
SqlTransaction trans = conn.BeginTransaction();
try
{
foreach (DictionaryEntry de in sqlTable)
{
cmd.CommandText = de.Key.ToString();
SqlParameter[] parameters = (SqlParameter[])de.Value;
cmd.Parameters.AddRange(parameters);
cmd.ExecuteNonQuery();
}
trans.Commit();
}
catch
{
trans.Rollback();
}
}
}
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace DAL
{
/// <summary>
/// 数据库操作摘要
/// 定义:周龙
/// 2008/11/23
/// </summary>
public sealed class DBHelper
{
private static readonly string connectionStr = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
/// <summary>
/// 执行SQL语句,并返回受影响的行数
/// </summary>
/// <param name="cmdText">命令文本</param>
/// <param name="cmdType">命令类型</param>
/// <param name="sqlParams">参数</param>
/// <returns>返回受影响的行数</returns>
public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams)
{
using (SqlConnection conn = new SqlConnection(connectionStr))
{
SqlCommand sqlCmd = new SqlCommand();
PrepareCommand(conn, sqlCmd, null, cmdText, cmdType, sqlParams);
int val = sqlCmd.ExecuteNonQuery();
sqlCmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 执行SQL语句,并返回SqlDataReader对象
/// </summary>
/// <param name="cmdText">命令文本</param>
/// <param name="cmdType">命令类型</param>
/// <param name="sqlParams">参数</param>
/// <returns>返回SqlDataReader对象</returns>
public static SqlDataReader ExecuteReader(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams)
{
SqlConnection conn = new SqlConnection(connectionStr);
try
{
SqlCommand sqlCmd = new SqlCommand();
PrepareCommand(conn, sqlCmd, null, cmdText, cmdType, sqlParams);
SqlDataReader val = sqlCmd.ExecuteReader();
sqlCmd.Parameters.Clear();
return val;
}
catch(SqlException e)
{
conn.Close();
throw e;
}
}
/// <summary>
/// 执行Sql语句,并返回object
/// </summary>
/// <param name="cmdText">命令文本</param>
/// <param name="cmdType">命令类型</param>
/// <param name="sqlParams">参数</param>
/// <returns>返回object对象</returns>
public static object ExecuteScalar(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams)
{
using (SqlConnection conn = new SqlConnection(connectionStr))
{
using (SqlCommand sqlCmd = new SqlCommand())
{
PrepareCommand(conn, sqlCmd, null, cmdText, cmdType, sqlParams);
object val = sqlCmd.ExecuteScalar();
sqlCmd.Parameters.Clear();
return val;
}
}
}
/// <summary>
/// 执行Sql语句,并返回DataSet
/// </summary>
/// <param name="cmdText">命令文本</param>
/// <param name="cmdType">命令类型</param>
/// <param name="sqlParams">参数</param>
/// <returns>返回DataSet对象</returns>
public static DataSet ExecuteDataSet(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams)
{
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection(connectionStr))
{
using (SqlCommand sqlCmd = new SqlCommand(cmdText))
{
foreach (SqlParameter p in sqlParams)
{
sqlCmd.Parameters.Add(p);
}
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
try
{
da.Fill(ds);
}
catch(SqlException e)
{
throw e;
}
finally
{
sqlCmd.Parameters.Clear();
}
}
}
return ds;
}
/// <summary>
/// 执行之前的准备
/// </summary>
/// <param name="conn">数据库连接对象</param>
/// <param name="sqlCmd">数据库操作对象</param>
/// <param name="sqlTrans">事务</param>
/// <param name="cmdText">Sql语句或存储过程名称</param>
/// <param name="cmdType">执行类型</param>
/// <param name="sqlParams">参数</param>
private static void PrepareCommand(SqlConnection conn,SqlCommand sqlCmd,SqlTransaction sqlTrans,string cmdText, CommandType cmdType, SqlParameter[] sqlParams)
{
//打开连接
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
sqlCmd.Connection = conn;
sqlCmd.CommandText = cmdText;
sqlCmd.CommandType = cmdType;
//如果有事务,启动事务
if (sqlTrans != null)
{
sqlCmd.Transaction = sqlTrans;
}
//参数
if (sqlParams != null)
{
foreach (SqlParameter p in sqlParams)
{
sqlCmd.Parameters.Add(p);
}
}
}
/// <summary>
/// 使用事务执行多条SQL语句
/// </summary>
/// <param name="sqlTable">Hashtable: Key:执行的SQL语句, Value:存储多条SQL的参数</param>
public static void ExecuteTransaction(Hashtable sqlTable)
{
using (SqlConnection conn = new SqlConnection(connectionStr))
{
try
{
conn.Open();
}
catch(SqlException e)
{
System.Diagnostics.Debug.Print(e.Message);
}
using (SqlCommand cmd = conn.CreateCommand())
{
SqlTransaction trans = conn.BeginTransaction();
try
{
foreach (DictionaryEntry de in sqlTable)
{
cmd.CommandText = de.Key.ToString();
SqlParameter[] parameters = (SqlParameter[])de.Value;
cmd.Parameters.AddRange(parameters);
cmd.ExecuteNonQuery();
}
trans.Commit();
}
catch
{
trans.Rollback();
}
}
}
}
}
}
展开全部
讲明白点。什么叫链接数据库功能啊?
连接?
哎
连接?
哎
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
ADO.NET的话连接数据库只要在配置文件里面写好,在外面累里调用就好了,连接数据库就一句话。还要看你连接那张表!你自己都要提供的呀,然后对数据库要做什么操作!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你说的这个是不是相当与写存储过程在类里面封装好,到时候直接调用这个类
public abstract class DbHelperSQL
{
protected static string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
public DbHelperSQL()
{
}
#region 执行简单SQL语句
。。。。。。。。。。。。。。语句
public abstract class DbHelperSQL
{
protected static string connectionString = ConfigurationManager.AppSettings["ConnectionString"];
public DbHelperSQL()
{
}
#region 执行简单SQL语句
。。。。。。。。。。。。。。语句
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个不用做了,去下一个,到网上搜索一下,微软的sqlhelper.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上有牛人写的SQLHELPER类,你找找吧,那个是用在SQL数据库的,其它数据库的就不是很了解了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询