sqlHelper.cs
Code
using System.Data;
using System.Data.SqlClient;
namespace lsb.DBUtility
{
/// <summary>
/// SQL Server 数据库操作类
/// </summary>
public class SqlHelper : lsb.DBtility.IDBHelper
{
private string ConnectionString;
private SqlConnection conn;
private SqlDataAdapter da;
private SqlCommand cmd;
public bool GetConnectionString(ref string ErrMsg)
{
ConnectionString = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";
return true;
}
public bool Open(ref string ErrMsg)
{
try
{
conn = new SqlConnection();
if ((conn.State == ConnectionState.Broken) || (conn.State == ConnectionState.Closed))
{
if (!GetConnectionString(ref ErrMsg))
{
return false;
}
conn.ConnectionString = ConnectionString;
conn.Open();
return true;
}
return false;
}
catch (SqlException ex)
{
this.SetErrMsg(ex, ref ErrMsg);
return false;
}
}
public bool Close(ref string ErrMsg)
{
try
{
if ((conn.State != ConnectionState.Broken) && (conn.State != ConnectionState.Closed))
{
conn.Close();
return true;
}
return false;
}
catch (SqlException ex)
{
this.SetErrMsg(ex, ref ErrMsg);
return false;
}
}
public bool ExceuteNonQuery(string sql, ref string ErrMsg)
{
throw new System.NotImplementedException();
}
public bool GetDataSet(string sql, ref DataSet ds, ref string ErrMsg)
{
throw new System.NotImplementedException();
}
private bool SetErrMsg(SqlException ex, ref string ErrMsg)
{
ErrMsg = ex.Message;
return true;
}
}
}
using System.Data;
using System.Data.SqlClient;
namespace lsb.DBUtility
{
/// <summary>
/// SQL Server 数据库操作类
/// </summary>
public class SqlHelper : lsb.DBtility.IDBHelper
{
private string ConnectionString;
private SqlConnection conn;
private SqlDataAdapter da;
private SqlCommand cmd;
public bool GetConnectionString(ref string ErrMsg)
{
ConnectionString = "Data Source=.;Initial Catalog=Hotel;Integrated Security=True";
return true;
}
public bool Open(ref string ErrMsg)
{
try
{
conn = new SqlConnection();
if ((conn.State == ConnectionState.Broken) || (conn.State == ConnectionState.Closed))
{
if (!GetConnectionString(ref ErrMsg))
{
return false;
}
conn.ConnectionString = ConnectionString;
conn.Open();
return true;
}
return false;
}
catch (SqlException ex)
{
this.SetErrMsg(ex, ref ErrMsg);
return false;
}
}
public bool Close(ref string ErrMsg)
{
try
{
if ((conn.State != ConnectionState.Broken) && (conn.State != ConnectionState.Closed))
{
conn.Close();
return true;
}
return false;
}
catch (SqlException ex)
{
this.SetErrMsg(ex, ref ErrMsg);
return false;
}
}
public bool ExceuteNonQuery(string sql, ref string ErrMsg)
{
throw new System.NotImplementedException();
}
public bool GetDataSet(string sql, ref DataSet ds, ref string ErrMsg)
{
throw new System.NotImplementedException();
}
private bool SetErrMsg(SqlException ex, ref string ErrMsg)
{
ErrMsg = ex.Message;
return true;
}
}
}
IDBHelper.cs
Code
using System;
using System.Data;
namespace lsb.DBtility
{
public interface IDBHelper
{
/// <summary>
/// 取得数据库连接字符串
/// </summary>
/// <returns></returns>
bool GetConnectionString(ref string ErrMsg);
/// <summary>
/// 打开数据库连接
/// </summary>
bool Open(ref string ErrMsg);
/// <summary>
/// 关闭数据库连接
/// </summary>
bool Close(ref string ErrMsg);
/// <summary>
/// 执行 SQL 语句 无返回结果
/// </summary>
bool ExceuteNonQuery(string sql, ref string ErrMsg);
/// <summary>
/// 取得DataSet数据集
/// </summary>
bool GetDataSet(string sql, ref DataSet ds, ref string ErrMsg);
}
}
using System;
using System.Data;
namespace lsb.DBtility
{
public interface IDBHelper
{
/// <summary>
/// 取得数据库连接字符串
/// </summary>
/// <returns></returns>
bool GetConnectionString(ref string ErrMsg);
/// <summary>
/// 打开数据库连接
/// </summary>
bool Open(ref string ErrMsg);
/// <summary>
/// 关闭数据库连接
/// </summary>
bool Close(ref string ErrMsg);
/// <summary>
/// 执行 SQL 语句 无返回结果
/// </summary>
bool ExceuteNonQuery(string sql, ref string ErrMsg);
/// <summary>
/// 取得DataSet数据集
/// </summary>
bool GetDataSet(string sql, ref DataSet ds, ref string ErrMsg);
}
}
不知道这样写好不好, 心里还没有数 ~~
上面的方法中要不要用 ref stirng ErrMsg 呢?
还是把上面的方法改为静态方法?