SqlHelper 数据库操作类
生活随笔
收集整理的這篇文章主要介紹了
SqlHelper 数据库操作类
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
?
sqlHelper.cs
?
Codeusing?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
?
Codeusing?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 呢?
還是把上面的方法改為靜態方法?
?
轉載于:https://www.cnblogs.com/LinFx/archive/2009/02/26/2123694.html
總結
以上是生活随笔為你收集整理的SqlHelper 数据库操作类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 聚集索引和非聚集索引[转]
- 下一篇: mysql 5.0存储过程学习总结