www.pudn.com > HttpProxy.rar > SqlHelper.cs
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
namespace HttpProxy.Component
{
///
/// SqlHelper 类
///
public class SqlServerHelper:SqlHelper
{
private string connString;
private SqlConnection conn;
private SqlCommand cmd;
///
/// Initializes a new instance of the class.
///
/// The connection string.
public SqlServerHelper(string connectionString)
{
this.connString=connectionString;
this.conn=new SqlConnection(this.connString);
}
#region SqlHelper 成员
///
/// Executes the specified SQL.
///
/// The SQL.
///
public bool Execute(string sql)
{
this.cmd = new SqlCommand(sql,this.conn);
bool ret=true;
try
{
this.conn.Open();
ret=cmd.ExecuteNonQuery() >= 1;
}
catch
{
ret=false;
}
finally
{
if(this.conn.State==ConnectionState.Open)
this.conn.Close();
}
return ret;
}
///
/// Gets the list.
///
/// The SQL.
///
public DataSet GetList(string sql)
{
this.cmd =new SqlCommand(sql,this.conn);
SqlDataAdapter da=new SqlDataAdapter(this.cmd);
DataSet ds=new DataSet();
try
{
da.Fill(ds);
}
catch
{
ds=null;
}
finally
{
if(this.conn.State==ConnectionState.Open)
this.conn.Close();
}
return ds;
}
///
/// Retrieves the specified SQL.
///
/// The SQL.
///
public DataRow Retrieve(string sql)
{
DataTable dt=new DataTable();
dt.Columns.AddRange(new DataColumn[]{new DataColumn("id",typeof(int)),new DataColumn("name",typeof(string)),new DataColumn("password",typeof(string)),new DataColumn("description",typeof(string))});
DataRow dr=dt.NewRow();;
this.cmd =new SqlCommand(sql,this.conn);
try
{
this.conn.Open();
SqlDataReader sdr=this.cmd.ExecuteReader(CommandBehavior.CloseConnection);
sdr.Read();
dr["id"]=sdr["id"];
dr["name"]=sdr["name"];
dr["password"]=sdr["password"];
dr["description"]=sdr["description"];
sdr.Close();
}
catch
{
dr=null;
}
finally
{
if(this.conn.State==ConnectionState.Open)
this.conn.Close();
}
return dr;
}
#endregion
}
///
/// AccessHelper 类
///
public class AccessHelper:SqlHelper
{
#region SqlHelper 成员
public bool Execute(string sql)
{
// TODO: 添加 AccessHelper.Execute 实现
return false;
}
public DataSet GetList(string sql)
{
// TODO: 添加 AccessHelper.GetList 实现
return null;
}
public DataRow Retrieve(string sql)
{
// TODO: 添加 AccessHelper.Retrieve 实现
return null;
}
#endregion
}
///
/// Interface SqlHelper
///
public interface SqlHelper
{
bool Execute(string sql);
DataSet GetList(string sql);
DataRow Retrieve(string sql);
}
}