数据访问通用类(C#)
<pre name="code" class="csharp">using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace SqlHelper
{
public class SQLHelper
{
/// <summary>
/// 获取数据连接字符串
/// </summary>
/// <returns></returns>
public static string GetConnectionString()
{
return ConfigurationManager.ConnectionStrings["ASPNET3_5_DataBase"].ConnectionString;
}
/// <summary>
/// 执行查询并将结果返回至DataTable中
/// </summary>
/// <param name="strSql">查询语句</param>
/// <param name="parameters">可能带的参数</param>
/// <returns>返回一张查询结果表</returns>
public static DataTable ExecuteDataTable(string strSql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(GetConnectionString()))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = strSql;
foreach (SqlParameter p in parameters)
{
cmd.Parameters.Add(p);
}
DataSet ds = new DataSet();
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(ds);
return ds.Tables[0];
}
}
}
}
/// <summary>
/// 执行对数据的增删改操作
/// </summary>
/// <param name="strSql"></param>
/// <param name="parameters"></param>
public static void ExecuteNonQuery(string strSql, params SqlParameter[] parameters)
{
using (SqlConnection conn = new SqlConnection(GetConnectionString()))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = strSql;
foreach (SqlParameter p in parameters)
{
cmd.Parameters.Add(p);
}
cmd.ExecuteNonQuery();
}
}
}
/// <summary>
/// 执行查询并返回结果集中第一行第一列的值
/// </summary>
/// <param name="strSql"></param>
/// <param name="parameters"></param>
/// <returns>&l
补充:软件开发 , C# ,