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é),但在做項(xiàng)目中不要用它,因?yàn)榇蠖嘣L問數(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)中可找到它)!!!