C# 數據庫鏈接字符串集合

1.C#鏈接鏈接Access   
  程序代碼:     
    
  using   System.Data;   
  using   System.Data.OleDb;   
  ..   
    
  string   strConnection="Provider=Microsoft.Jet.OleDb.4.0;";   
  strConnection+=@"Data   Source=C:BegASPNETNorthwind.mdb";   
    
  OleDbConnection   objConnection=new   OleDbConnection(strConnection);   
  ..   
    
  objConnection.Open();   
  objConnection.Close();   
  解釋:   
    
   鏈接Access數據庫須要導入額外的命名空間,因此有了最前面的兩條using命令,這是必不可少的!   
    
   strConnection這個變量裏存放的是鏈接數據庫所須要的鏈接字符串,他指定了要使用的數據提供者和要使用的數據源.   
    
   "Provider=Microsoft.Jet.OleDb.4.0;"是指數據提供者,這裏使用的是Microsoft   Jet引擎,也就是Access中的數據引擎,asp.net就是靠這個和Access的數據庫鏈接的.   
    
   "Data   Source=C:\BegASPNET\Northwind.mdb"是指明數據源的位置,他的標準形式是"Data   Source=MyDrive:MyPath\MyFile.MDB".   
    
  PS:   
   1."+="後面的"@"符號是防止將後面字符串中的"\"解析爲轉義字符.   
   2.若是要鏈接的數據庫文件和當前文件在同一個目錄下,還可使用以下的方法鏈接:   
    strConnection+="Data   Source=";   
    strConnection+=MapPath("Northwind.mdb");   
    這樣就能夠免得你寫一大堆東西了!   
   3.要注意鏈接字符串中的參數之間要用分號來分隔.   
    
   "OleDbConnection   objConnection=new   OleDbConnection(strConnection);"這一句是利用定義好的鏈接字符串來創建了一個連接對象,之後對數據庫的操做咱們都要和這個對象打交道.   
    
   "objConnection.Open();"這用來打開鏈接.至此,與Access數據庫的鏈接完成.   
  --------------------------------------------------------------------------------   
    
  2.C#鏈接SQL   Server   
  程序代碼:     
    
  using   System.Data;   
  using   System.Data.SqlClient;   
  ..   
    
  string   strConnection="user   id=sa;password=;";   
  strConnection+="initial   catalog=Northwind;Server=YourSQLServer;";   
  strConnection+="Connect   Timeout=30";   
    
  SqlConnection   objConnection=new   SqlConnection(strConnection);   
  ..   
    
  objConnection.Open();   
  objConnection.Close();   
  解釋:   
    
  鏈接SQL   Server數據庫的機制與鏈接Access的機制沒有什麼太大的區別,只是改變了Connection對象和鏈接字符串中的不一樣參數.   
    
  首先,鏈接SQL   Server使用的命名空間不是"System.Data.OleDb",而是"System.Data.SqlClient".   
    
  其次就是他的鏈接字符串了,咱們一個一個參數來介紹(注意:參數間用分號分隔):   
   "user   id=sa":鏈接數據庫的驗證用戶名爲sa.他還有一個別名"uid",因此這句咱們還能夠寫成"uid=sa".   
   "password=":鏈接數據庫的驗證密碼爲空.他的別名爲"pwd",因此咱們能夠寫爲"pwd=".   
   這裏注意,你的SQL   Server必須已經設置了須要用戶名和密碼來登陸,不然不能用這樣的方式來登陸.若是你的SQL   Server設置爲Windows登陸,那麼在這裏就不須要使用"user   id"和"password"這樣的方式來登陸,而須要使用"Trusted_Connection=SSPI"來進行登陸.   
   "initial   catalog=Northwind":使用的數據源爲"Northwind"這個數據庫.他的別名爲"Database",本句能夠寫成"Database=Northwind".   
   "Server=YourSQLServer":使用名爲"YourSQLServer"的服務器.他的別名爲"Data   Source","Address","Addr".若是使用的是本地數據庫且定義了實例名,則能夠寫爲"Server=(local)\實例名";若是是遠程服務器,則將"(local)"替換爲遠程服務器的名稱或IP地址.   
   "Connect   Timeout=30":鏈接超時時間爲30秒.   
    
   在這裏,創建鏈接對象用的構造函數爲:SqlConnection.   
    
  3.C#鏈接Oracle   
  程序代碼:     
  --------------------------------------------------------------------------------   
    
  using   System.Data.OracleClient;   
  using   System.Data;   
    
  //在窗體上添加一個按鈕,叫Button1,雙擊Button1,輸入如下代碼   
  private   void   Button1_Click(object   sender,   System.EventArgs   e)   
  {   
  string   ConnectionString="Data   Source=sky;user=system;password=manager;";//寫鏈接串   
  OracleConnection   conn=new   OracleConnection(ConnectionString);//建立一個新鏈接   
  try   
  {   
  conn.Open();   
  OracleCommand   cmd=conn.CreateCommand();   
    
  cmd.CommandText="select   *   from   MyTable";//在這兒寫sql語句   
  OracleDataReader   odr=cmd.ExecuteReader();//建立一個OracleDateReader對象   
  while(odr.Read())//讀取數據,若是odr.Read()返回爲false的話,就說明到記錄集的尾部了                                   
  {   
  Response.Write(odr.GetOracleString(1).ToString());//輸出字段1,這個數是字段索引,具體怎麼使用字段名還有待研究   
  }   
  odr.Close();   
  }   
  catch(Exception   ee)   
  {   
  Response.Write(ee.Message);   //若是有錯誤,輸出錯誤信息   
  }   
  finally   
  {   
  conn.Close();   //關閉鏈接   
  }   
  }   
  -------------------------------------------------------------------------------   
    
  4.C#鏈接MySQL   
  程序代碼:     
  --------------------------------------------------------------------------------   
    
  using   MySQLDriverCS;     
    
  //   創建數據庫鏈接   
  MySQLConnection   DBConn;   
  DBConn   =   new   MySQLConnection(new   MySQLConnectionString("localhost","mysql","root","",3306).AsString);   
  DBConn.Open();     
    
  //   執行查詢語句   
  MySQLCommand   DBComm;   
  DBComm   =   new   MySQLCommand("select   Host,User   from   user",DBConn);     
    
  //   讀取數據   
  MySQLDataReader   DBReader   =   DBComm.ExecuteReaderEx();     
    
  //   顯示數據   
  try   
  {   
  while   (DBReader.Read())   
  {   
  Console.WriteLine("Host   =   {0}   and   User   =   {1}",   DBReader.GetString(0),DBReader.GetString(1));   
  }   
  }   
  finally   
  {   
  DBReader.Close();   
  DBConn.Close();   
  }     
    
  //關閉數據庫鏈接   
  DBConn.Close();   
  --------------------------------------------------------------------------------   
    
  5.C#鏈接IBM   DB2   
  程序代碼:     
  --------------------------------------------------------------------------------   
    
  OleDbConnection1.Open();   
  //打開數據庫鏈接   
  OleDbDataAdapter1.Fill(dataSet1,"Address");   
  //將得來的數據填入dataSet   
  DataGrid1.DataBind();   
  //綁定數據   
  OleDbConnection1.Close();   
  //關閉鏈接     
    
  //增長數據庫數據   
  在Web   Form上新增對應字段數量個數的TextBox,及一個button,爲該按鍵增長Click響應事件代碼以下:   
    
  this.OleDbInsertCommand1.CommandText   =   "INSERTsintosADDRESS(NAME,   
  EMAIL,   AGE,   ADDRESS)   VALUES   
  ('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"')";   
  OleDbInsertCommand1.Connection.Open();   
  //打開鏈接   
  OleDbInsertCommand1.ExecuteNonQuery();   
  //執行該SQL語句   
  OleDbInsertCommand1.Connection.Close();   
  //關閉鏈接     
  --------------------------------------------------------------------------------   
    
  6.C#鏈接SyBase   
  程序代碼:   (OleDb)   
  --------------------------------------------------------------------------------   
    
  Provider=Sybase.ASEOLEDBProvider.2;Initial   Catalog=數據庫名;User   ID=用戶名;Data   Source=數據源;Extended   Properties="";Server   Name=ip地址;Network   Protocol=Winsock;Server   Port   Address=5000;mysql

相關文章
相關標籤/搜索