C# 一个SqlHelper例子
2013-06-17 14:12
316 查看
using System; using System.Collections.Generic; using System.Web; using System.Data.SqlClient; using System.Configuration; using System.Data; /// <summary> /// Summary description for SqlHelper /// </summary> public class SqlHelper : IDisposable { #region 基本属性 private string _lastError; public string LastError { get { return _lastError; } set { _lastError = value; } } public static string ConnectionString = ConfigurationSettings.AppSettings["DBConnectionString"]; protected SqlConnection cnn; #endregion public SqlHelper() { this._lastError = string.Empty; try { cnn = new SqlConnection(ConnectionString); cnn.Open(); } catch (Exception ex) { this._lastError = ex.Message; throw ex; } } ///执行sql public bool Execute(string sql) { try { this._lastError = string.Empty; using (SqlCommand cmd = new SqlCommand(sql.Trim(), cnn)) { cmd.ExecuteNonQuery(); return true; } } catch (Exception ex) { this._lastError = ex.Message; return false; } } ///执行,返回DataReader public SqlDataReader Reader(string sql) { try { this._lastError = string.Empty; using (SqlCommand cmd = new SqlCommand(sql.Trim(), cnn)) { SqlDataReader dr = cmd.ExecuteReader(); return dr; } } catch (Exception ex) { this._lastError = ex.Message; return null; } } ///执行sql,返回DataTable public DataTable Query(string sql) { this._lastError = string.Empty; DataTable dt = new DataTable(); try { using (SqlDataAdapter da = new SqlDataAdapter(sql.Trim(), cnn)) { da.Fill(dt); return dt; } } catch (Exception ex) { this._lastError = ex.Message; return null; } } ///执行sql,返回DataSet public DataSet Query(string sql, object inputNull) { this._lastError = string.Empty; DataSet ds = new DataSet(); try { using (SqlDataAdapter da = new SqlDataAdapter(sql.Trim(), cnn)) { da.Fill(ds); return ds; } } catch (Exception ex) { this._lastError = ex.Message; return null; } } ///执行sql,返回一串字符 public string GetValue(string sql, int ColumnIndex) { try { this._lastError = string.Empty; using (SqlDataReader dr = this.Reader(sql)) { if (dr.Read()) { return dr[ColumnIndex].ToString().Trim(); } return string.Empty; } } catch (Exception ex) { this._lastError = ex.Message; return string.Empty; } } public string GetValue(string sql, string ColumnName) { try { this._lastError = string.Empty; using (SqlDataReader dr = this.Reader(sql)) { if (dr.Read()) { return dr[ColumnName].ToString().Trim(); } return string.Empty; } } catch (Exception ex) { this._lastError = ex.Message; return string.Empty; } } ~SqlHelper() { this.Dispose(false); } private bool disposed = false; public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } private void Dispose(bool disposing) { if (!this.disposed && disposing) { try { cnn.Close(); cnn.Dispose(); } catch { } } disposed = true; } }
相关文章推荐
- C#3—做一个类似注册的一个界面例子
- C#中关于委托练习的一个例子
- 一个简单的C#多线程间同步的例子
- c#中子线程控制进度条的一个简单例子
- 一个简单的C#多线程间同步的例子 (转)
- 一个简单的C#多线程间同步的例子
- 一个极其简单的在线C#IDE例子
- C# 一个简单的多线程例子
- 一个用C#操作OpenLDAP的例子
- C# Remoting的一个简单例子
- .net C# 跨越线程调用窗体上的控件一个完整的例子
- 一个简单的C#多线程间同步的例子
- 一个简单的C#多播委托的小例子
- 我见过的讲述C# 委托的一个最直白的例子
- 一个C#中使用 event 和 delegate的小例子。
- C#中练习属性使用的一个例子
- C#的一个客户端和服务器端例子
- C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子
- C# 串口操作系列(1) -- 入门篇,一个标准的,简陋的串口例子。
- C# 中子线程控制进度条的一个简单例子