这个也借鉴了几个版本的一些代码。我没有写对存储过程的操作,因为我写不好存储过程。。。。。这个写完之后,我连测试都没测过。就当做给大家的反面教材吧!
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace Model
{
class SqlHelper
{
#region 通用方法
// 数据连接池
private SqlConnection con;
/// <summary>
/// 返回数据库连接字符串
/// </summary>
/// <returns></returns>
public static String GetSqlConnection()
{
// 连接字符串
String conn = "";
return conn;
}
#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
#endregion
#region 执行sql字符串
#region 执行不带参数的SQL语句
/// <summary>
/// 执行不带参数的SQL语句
/// </summary>
/// <param name="Sqlstr"></param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr)
{
String ConnStr = GetSqlConnection();
int re = 0;
try
{
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
conn.Open();
re = cmd.ExecuteNonQuery();
conn.Close();
}
}
catch(Exception e)
{
throw e;
}
return re;
}
#endregion
#region 执行带参数的SQL语句
/// <summary>
/// 执行带参数的SQL语句
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <param name="param">参数对象数组</param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr, SqlParameter[] Params)
{
String ConnStr = GetSqlConnection();
int re = 0;
try
{
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
cmd.Parameters.AddRange(Params);
conn.Open();
re = cmd.ExecuteNonQuery();
conn.Close();
}
}
catch(Exception e)
{
throw e;
}
return re;
}
#endregion
#region 执行SQL语句并返回数据表(无参数)
/// <summary>
/// 执行SQL语句并返回数据表(无参数)
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <returns></returns>
public static DataSet ExecuteDt(String Sqlstr)
{
String ConnStr = GetSqlConnection();
DataSet dt = new DataSet();
try
{
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlDataAdapter da = new SqlDataAdapter(Sqlstr, conn);
conn.Open();
da.Fill(dt);
conn.Close();
}
}
catch (Exception e)
{
throw e;
}
return dt;
}
#endregion
#region 执行SQL语句并返回数据表(有参数)
/// <summary>
/// 执行SQL语句并返回数据表(有参数)
/// </summary>
/// <param name="Sqlstr">SQL语句</param>
/// <returns></returns>
public static DataSet ExecuteDt(String Sqlstr, SqlParameter[] Params)
{
String ConnStr = GetSqlConnection();
DataSet dt = new DataSet();
try
{
using (SqlConnection conn = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = Sqlstr;
cmd.Parameters.AddRange(Params);
SqlDataAdapter da = new SqlDataAdapter(cmd);
conn.Open();
da.Fill(dt);
conn.Close();
}
}
catch(Exception e)
{
throw e;
}
return dt;
}
#endregion
#endregion
}
}
欢迎来喷。如果喷,请轻喷。。。。。。。
我的第一个SqlHelper.cs
TAG: