明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

C#中數(shù)據(jù)庫(kù)設(shè)置

[摘要]/******************************* * 功能:數(shù)據(jù)庫(kù)操作相關(guān) * 作者:FlashICP * 時(shí)間:2005-7-22 * ******************************/using System;using System.Data;using Syst...
  /*******************************
 * 功能:數(shù)據(jù)庫(kù)操作相關(guān)
 * 作者:FlashICP
 * 時(shí)間:2005-7-22
 * ******************************/
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
namespace moban
{
 public class data
 {
  protected static string con="server=[yourserver];database=[yourdata];user id=[your id];password=[youpassword]";
  //數(shù)據(jù)庫(kù)字符串
  protected System.Data.SqlClient.SqlConnection DataConnection;
  //數(shù)據(jù)庫(kù)連接對(duì)象
  protected System.Data.SqlClient.SqlDataAdapter DataAdapter;
  //SqlDataAdapter對(duì)象
  protected System.Data.SqlClient.SqlCommand DataCommand;
  //SqlCommand對(duì)象
  protected System.Data.SqlClient.SqlCommandBuilder DataComBuilder;
  //SqlCommandBuilder對(duì)象
  protected System.Data.DataSet DataSet;
  //DataSet對(duì)象
  protected System.Data.DataRow DataRow;
  //DataRow對(duì)象
  protected System.Data.SqlClient.SqlDataReader DataReader=null;
  //構(gòu)造函數(shù)
  public data()
  {
   DataConnection=DataConn();
   
  }
  //連接數(shù)據(jù)庫(kù)參數(shù)
  public string Con
  {
   
   get{return con;}
   set{con= value;}
   
  }
  //數(shù)據(jù)庫(kù)連接參數(shù)
  public SqlConnection DataConn()
  {
   DataConnection=new SqlConnection(Con); 
   return DataConnection;
   
  }
  //返回?cái)?shù)據(jù)庫(kù)連接對(duì)象
  public SqlDataReader DataQuery(string Sql)
  {
   Sql=Sql.Replace("--","");
   Sql=Sql.Replace(";","");
   DataConnection=DataConn();
   DataConnection.Open();
   DataReader=null;
   DataCommand=new SqlCommand(Sql,DataConnection);
   DataReader=DataCommand.ExecuteReader();
   DataCommand.Dispose();
   return DataReader;
  }
  //返回DataSet
  public virtual DataSet DataSetQuery(string Sql,string TableName)
  {
   DataConnection=DataConn();
   DataAdapter=new SqlDataAdapter(Sql,DataConnection);
   DataSet=new DataSet();
   DataAdapter.Fill(DataSet,TableName);
   DataAdapter.Dispose();
   DataConnection.Close();
   DataConnection.Dispose();
   return DataSet;
  }
  //返回DataSet
  public DataSet DataSetQuery(DataSet myds,string TableName,string Sql)
  {
   DataConnection=DataConn();
   DataAdapter=new SqlDataAdapter(Sql,DataConnection);
   DataAdapter.Fill(myds,TableName);
   DataConnection.Close();
   DataConnection.Dispose();
   return DataSet;

  }
  //返回DataSet
  public DataSet DataSetQuery(string Sql,string TableName,int StarIndex,int Count)
  {
   DataConnection=DataConn();
   DataConnection.Open();
   DataAdapter=new SqlDataAdapter(Sql,DataConnection);
   DataSet=new DataSet();
   DataAdapter.Fill(DataSet,StarIndex,Count,TableName);
   DataConnection.Close();
   DataConnection.Dispose();
   return DataSet;
  }
  public void DataDo(string sqlstr)
  {  
   DataConnection=DataConn();
   SqlCommand mycom=DataConnection.CreateCommand();
   mycom.CommandText=sqlstr;
   DataConnection.Open();
   mycom.ExecuteNonQuery();
   DataConnection.Close();
  }
  //返回DataSet 開(kāi)始行到結(jié)束行
  public bool EditData(string Sql)
  {
   DataConnection=DataConn();
   DataConnection.Open();
   DataCommand=new SqlCommand(Sql,DataConnection);
   try
   {
    DataCommand.ExecuteNonQuery();
    DataConnection.Close();
    return true;
   }
   catch(Exception ex)
   { 
    System.Console.Write(ex.Message.ToString());
    DataConnection.Close();
    return false;
   }
  }
  //執(zhí)行成功返回真,否則返回假.
  public object ExecuteScalar(string Sql)
  {
   DataConnection=DataConn();
   DataConnection.Open();
   DataCommand=new SqlCommand(Sql,DataConnection);
   try
   {
    object back = DataCommand.ExecuteScalar();
    DataConnection.Close();
    return back;
   }
   catch(Exception ex)
   { 
    System.Console.Write(ex.Message.ToString());
    DataConnection.Close();
    return null;
   }
  }
  //返回一個(gè)值的查詢語(yǔ)句
  public DataSet EditData(DataSet DataSet,SqlDataAdapter myda,string TableName)
  {
    
   
   DataComBuilder=new SqlCommandBuilder(myda);
   myda.UpdateCommand=DataComBuilder.GetUpdateCommand();
   myda.DeleteCommand=DataComBuilder.GetDeleteCommand();
   myda.InsertCommand=DataComBuilder.GetInsertCommand();
   myda.Update(DataSet,TableName);
   DataSet.AcceptChanges();
   this.CloseConnection();
   return DataSet;
  }
  //修改DataSet 并且修改數(shù)據(jù)庫(kù)值存儲(chǔ)過(guò)程
  public bool DataProcedure(string ProcedureName)
  {
   DataConnection=DataConn();
   DataConnection.Open();
   DataCommand=new SqlCommand();
   DataCommand.CommandText=ProcedureName;
   DataCommand.Connection=DataConnection;
   
     
   DataCommand.CommandType=System.Data.CommandType.StoredProcedure;
   try
   {
    DataCommand.ExecuteNonQuery();
    return true;
   }
   catch(Exception ex)
   { 
    System.Console.Write(ex.Message.ToString());
    return false;
   }
  }
  
  //無(wú)參數(shù)存儲(chǔ)過(guò)程的執(zhí)行 返回執(zhí)行真假
  public void CloseConnection()
  {
   if(DataReader!=null)
   {
    DataReader.Close();
    DataReader=null;

   }
   DataConnection.Close();
   DataConnection.Dispose();
  }
  //關(guān)閉連接
 

 }
}