using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace MyQQ
{
class DBHelper
{
//连接字符串
private static string strConnection = @"server=127.0.0.1;database=company;uid=sa;pwd=sodi";
private static SqlConnection connection;
//Connection属性
public static SqlConnection Connection
{
get
{
if (connection == null)//判断是否为空
{
connection = new SqlConnection(strConnection);
connection.Open();
}//判断状态是否关闭
else if (connection.State == System.Data.ConnectionState.Closed)
connection.Open();
else if (connection.State == System.Data.ConnectionState.Broken)//判断当前连接是否损坏
{
connection.Close();
connection.Open();
}
return connection;
}
}
/// <summary>
/// 用于执行表的增加,删除,修改操作
/// </summary>
/// <param name="sql">拼写完整的SQL</param>
/// <returns>执行Sql后受影响的行数</returns>
public static int ExecuteNonQuery(string sql)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = Connection;
int result = cmd.ExecuteNonQuery();
Connection.Close();
return result;
}
/// <summary>
/// 用于执行表的增加,删除,修改操作
/// </summary>
/// <param name="sql">带参数的SQL语句</param>
/// <param name="pars">SQL中需要的参数数组</param>
/// <returns>受影响的行数</returns>
public static int ExecuteNonQuery(string sql, params SqlParameter[] pars)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
cmd.Connection = Connection;
int result = cmd.ExecuteNonQuery();
Connection.Close();
return result;
}
/// <summary>
/// 执行查询
/// </summary>
/// <param name="sql">拼写完整的SQL</param>
/// <returns>DataReader</returns>
public static SqlDataReader GetDataReader(string sql)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = Connection;
SqlDataReader dr = cmd.ExecuteReader();
return dr;
}
///
///
///
///
public static SqlDataReader GetDataReader(string sql, params SqlParameter[] pars)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
cmd.Connection = Connection;
int dr = cmd.ExecuteReader();
return dr;
}
/// <summary>
/// 执行查询
/// </summary>
/// <param name="sql">拼写完整的SQL</param>
/// <returns>DataTable</returns>
public static DataTable GetDataTable(string sql)
{
SqlDataAdapter da = new SqlDataAdapter(sql, Connection);
DataTable dt = new DataTable();
da.Fill(dt);
Connection.Close();
return dt;
}
/// <summary>
/// 执行查询
/// </summary>
/// <param name="sql">带参数的SQL</param>
/// <param name="pars">SQL中需要的参数数组</param>
/// <returns>DataTable</returns>
public static DataTable GetDataTable(string sql, params SqlParameter[] pars)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Parameters.AddRange(pars);
cmd.Connection = Connection;
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
Connection.Close();
return dt;
}
/// <summary>
/// 执行单值查询
/// </summary>
/// <param name="sql">完整的SQL</param>
/// <returns>返回第一行一列的值</returns>
public static object GetScalar(string sql)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = sql;
cmd.Connection = Connection;
object obj = cmd.ExecuteScalar();
return obj;
}
}
}
本文出自 “做什么事情” 博客,转载请与作者联系!