Hopes

          Start Here..

           

          數據訪問類入門篇SqlDataBase.cs



          using System;
          using System.Collections.Generic;
          using System.Text;

          //**********************************************************************************************
          // 需新添加的命名空間如下:
          //**********************************************************************************************
          using System.Data;
          using System.Configuration;
          using System.Data.SqlClient;
          using System.Windows.Forms;

          namespace ClassLibrary1
          {
          public class SqlDataBase
          {
          private SqlConnection conn; //創建數據連接器;
          private SqlDataAdapter sda; //創建數據適配器;
          private SqlDataReader sdr; //創建數據讀取器;
          public SqlCommand SqlCmd; //創建Sql命令;
          private DataSet ds; //創建數據集;
          private DataView dv; //創建數據視圖;
          public SqlDataBase()
          {
          //
          // TODO: 在此處添加構造函數邏輯
          //
          }
          //**********************************************************************************************
          // 打開數據庫連接
          //**********************************************************************************************
          public void MyOpen()
          {
          try
          {
          conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
          //conn = new SqlConnection(@"data Source=F14BC4C8DAE9418\SQLEXPRESS;database=recipeDB;security=true;");
          conn.Open();
          }
          catch (SqlException e)
          {
          MessageBox.Show(e.Errors[0].Message.ToString());
          }
          }
          //**********************************************************************************************
          // 關閉數據庫連接并釋放資源
          //**********************************************************************************************
          public void MyClose()
          {
          if (conn != null)
          {
          conn.Close();
          conn.Dispose();
          }
          }

          //**********************************************************************************************
          // 返回數據集
          //**********************************************************************************************
          public DataSet GetDs(string SqlString)
          {
          try
          {
          MyOpen();
          sda = new SqlDataAdapter(SqlString, conn);
          ds = new DataSet();
          sda.Fill(ds);
          MyClose();
          return ds;
          }
          catch(SqlException e)
          {
          MessageBox.Show(e.Errors[0].Message.ToString());
          return null;
          }
          }

          //**********************************************************************************************
          // 返回數據視圖
          //**********************************************************************************************
          public DataView GetDv(string SqlString)
          {
          ds = GetDs(SqlString);
          dv = ds.Tables[0].DefaultView;
          return dv;
          }
          //**********************************************************************************************
          // 創建數據視圖
          //**********************************************************************************************
          public DataView CreateView(string strSql, int sRecord, int mRecord)
          {
          MyOpen();
          sda = new SqlDataAdapter(strSql, conn);
          ds = new DataSet();
          sda.Fill(ds, sRecord, mRecord, "temptbl");
          DataView dv = ds.Tables["temptbl"].DefaultView;
          conn.Close();
          conn.Dispose();
          return dv;
          }
          //**********************************************************************************************
          // 獲取數據表
          //**********************************************************************************************
          public DataTable GetDt(string SqlString)
          {
          return GetDs(SqlString).Tables[0];
          }

          //***********************************************************************************************
          // 返回數據讀取器,執行完后關閉連接(使用該方法切記要手工關閉SqlDataReader和連接)
          //***********************************************************************************************
          public SqlDataReader GetDr(string SqlString)
          {
          MyOpen();
          SqlCmd = new SqlCommand(SqlString, conn);
          try
          {
          sdr = SqlCmd.ExecuteReader();
          return sdr;
          }
          catch (System.Data.SqlClient.SqlException e)
          {
          throw new Exception(e.Message);
          }
          finally //不能在此關閉,否則,返回的對象將無法使用
          {
          //在這關閉 SqlDataReader對象.eg.
          //dr.Close();
          //SqlCmd.Dispose();
          //MyClose();
          }

          }

          //**********************************************************************************************
          // 執行無需返回的Sql語句
          //**********************************************************************************************
          public bool RunSql(string SqlString)
          {
          try
          {
          MyOpen();
          SqlCmd = new SqlCommand(SqlString, conn);
          if (SqlCmd.ExecuteNonQuery() > 0)
          {
          MyClose();
          return true;
          }
          else
          {
          MyClose();
          return false;
          }

          }
          catch { return false; }
          }

          public void ExecuteSql(string sqlstr)
          {
          try
          {
          MyOpen();
          SqlCmd = new SqlCommand(sqlstr, conn);

          SqlCmd.ExecuteNonQuery();
          }
          catch (Exception e)
          {
          throw new Exception(e.Message);
          }
          finally
          {
          MyClose();
          }
          }
          //************************************************************************************************
          // 返回Sql語句的第一行第一列,否則返回空
          //************************************************************************************************
          public string RunSqlReturn(string SqlSrting)
          {
          string returnString = "";
          MyOpen();
          SqlCmd = new SqlCommand(SqlSrting, conn);
          try
          {
          returnString = SqlCmd.ExecuteScalar().ToString();
          return returnString;
          }
          catch
          {
          return returnString;
          }
          finally
          {
          MyClose();
          }
          }

          //**********************************************************************************************
          // 返回一個數據行
          //**********************************************************************************************
          public DataRow GetDataRow(string SqlString)
          {
          DataSet dataset = GetDs(SqlString);
          dataset.CaseSensitive = false;
          if (dataset.Tables[0].Rows.Count > 0)
          {
          return dataset.Tables[0].Rows[0];
          }
          else
          {
          return null;
          }
          }
          //**********************************************************************************************
          // 返回object對象
          //**********************************************************************************************
          public object ExceScalar(string SqlString)
          {
          try
          {
          MyOpen();
          SqlCmd = new SqlCommand(SqlString, conn);
          object val = SqlCmd.ExecuteScalar();
          SqlCmd.Parameters.Clear();
          MyClose();
          return val;

          }
          catch
          {

          return null;

          }
          }
          #region ExeSqlFillTab(string sqlStr, ref DataSet TargetDataSet)執行sql,并將返回信息填充到到TargetDataSet中,執行成功返回true,否則為false
          /// <summary>
          ///
          /// 執行sql,并將返回信息填充到到TargetDataSet中的tableName中,執行成功返回true,否則為false
          ///</summary>
          public bool ExeSqlFillTab(string SqlString, ref DataSet TargetDataSet)
          {
          try
          {
          MyOpen();
          SqlCmd = new SqlCommand(SqlString, conn);
          SqlCmd.CommandType = CommandType.Text;

          sda = new SqlDataAdapter(SqlString, conn);

          sda.Fill(TargetDataSet);
          MyClose();
          return true;
          }
          catch (Exception ex)
          {
          Console.WriteLine(ex.Message);
          //TLog.WriteLog("ExeSqlFillTab Error:"+ex.Message+" sql:"+sqlStr);
          return false;
          }
          finally
          {
          MyClose();
          if (SqlCmd != null)
          SqlCmd.Dispose();
          }
          }
          #endregion
          }
          }




          注意:上面的代碼初學者可以拿來學學,但在做項目中不要用它,因為大多訪問數據庫方法沒有及時銷廢相關對象,導致如下問題出現:http://www.cnblogs.com/qiantuwuliang/archive/2009/05/31/1492959.html,建議使用微軟發布的SqlHelper.cs類,(在Petshop 當中可找到它)!!!

          posted on 2012-09-16 11:23 ** 閱讀(679) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          統計

          公告

          你好!

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          收藏夾

          C#學習

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 遵义市| 辛集市| 平潭县| 陕西省| 南岸区| 庆安县| 修文县| 琼中| 上杭县| 仁怀市| 尼木县| 吉木乃县| 铁岭县| 敦化市| 仙桃市| 辉县市| 胶南市| 武宁县| 尚义县| 高尔夫| 巧家县| 达州市| 东明县| 元氏县| 拉萨市| 饶河县| 冷水江市| 金山区| 新蔡县| 丰原市| 郓城县| 水富县| 伊宁市| 宁都县| 海南省| 海兴县| 乌海市| 密山市| 怀仁县| 天台县| 卢湾区|