Hopes

          Start Here..

           

          數(shù)據(jù)訪問類入門篇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; //創(chuàng)建數(shù)據(jù)連接器;
          private SqlDataAdapter sda; //創(chuàng)建數(shù)據(jù)適配器;
          private SqlDataReader sdr; //創(chuàng)建數(shù)據(jù)讀取器;
          public SqlCommand SqlCmd; //創(chuàng)建Sql命令;
          private DataSet ds; //創(chuàng)建數(shù)據(jù)集;
          private DataView dv; //創(chuàng)建數(shù)據(jù)視圖;
          public SqlDataBase()
          {
          //
          // TODO: 在此處添加構(gòu)造函數(shù)邏輯
          //
          }
          //**********************************************************************************************
          // 打開數(shù)據(jù)庫連接
          //**********************************************************************************************
          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());
          }
          }
          //**********************************************************************************************
          // 關(guān)閉數(shù)據(jù)庫連接并釋放資源
          //**********************************************************************************************
          public void MyClose()
          {
          if (conn != null)
          {
          conn.Close();
          conn.Dispose();
          }
          }

          //**********************************************************************************************
          // 返回數(shù)據(jù)集
          //**********************************************************************************************
          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;
          }
          }

          //**********************************************************************************************
          // 返回數(shù)據(jù)視圖
          //**********************************************************************************************
          public DataView GetDv(string SqlString)
          {
          ds = GetDs(SqlString);
          dv = ds.Tables[0].DefaultView;
          return dv;
          }
          //**********************************************************************************************
          // 創(chuàng)建數(shù)據(jù)視圖
          //**********************************************************************************************
          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;
          }
          //**********************************************************************************************
          // 獲取數(shù)據(jù)表
          //**********************************************************************************************
          public DataTable GetDt(string SqlString)
          {
          return GetDs(SqlString).Tables[0];
          }

          //***********************************************************************************************
          // 返回數(shù)據(jù)讀取器,執(zhí)行完后關(guān)閉連接(使用該方法切記要手工關(guān)閉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 //不能在此關(guān)閉,否則,返回的對象將無法使用
          {
          //在這關(guān)閉 SqlDataReader對象.eg.
          //dr.Close();
          //SqlCmd.Dispose();
          //MyClose();
          }

          }

          //**********************************************************************************************
          // 執(zhí)行無需返回的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();
          }
          }

          //**********************************************************************************************
          // 返回一個數(shù)據(jù)行
          //**********************************************************************************************
          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)執(zhí)行sql,并將返回信息填充到到TargetDataSet中,執(zhí)行成功返回true,否則為false
          /// <summary>
          ///
          /// 執(zhí)行sql,并將返回信息填充到到TargetDataSet中的tableName中,執(zhí)行成功返回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
          }
          }




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

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


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


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

          統(tǒng)計

          公告

          你好!

          常用鏈接

          留言簿(2)

          隨筆檔案

          文章分類

          文章檔案

          新聞檔案

          相冊

          收藏夾

          C#學(xué)習(xí)

          友情鏈接

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 玉山县| 谢通门县| 英德市| 阳城县| 临泉县| 壶关县| 鄂托克前旗| 银川市| 梅河口市| 峨边| 六安市| 仁寿县| 西充县| 云龙县| 格尔木市| 琼结县| 安岳县| 宜都市| 杭州市| 湟源县| 广水市| 安庆市| 英吉沙县| 定边县| 永丰县| 军事| 福泉市| 广德县| 清涧县| 方正县| 眉山市| 唐海县| 德兴市| 乌恰县| 平山县| 长乐市| 宜州市| 礼泉县| 黄陵县| 宣威市| 天等县|