C#連接數(shù)據(jù)庫代碼
作者:佚名 
  這里將介紹一段C#連接數(shù)據(jù)庫代碼,每種編程語言最重要的部分就是對數(shù)據(jù)庫的訪問了。希望本文對大家有所幫助。
 C#連接數(shù)據(jù)庫代碼主要是實(shí)現(xiàn)C#對數(shù)據(jù)庫的訪問,包括增刪改查等多項(xiàng)內(nèi)容。 MDB(Microsoft Database)格式是Microsoft Access軟件使用的一種存儲格式,因其對數(shù)據(jù)操作的方便性,常用在一些中小型程序中。
 
- ///系統(tǒng)引用
 - using System.Data;
 - using System.Data.OleDb;
 - using System.Data.SqlClient;
 - ///
 - /// 必需的設(shè)計(jì)器變量。
 - ///
 - private System.ComponentModel.Container components = null;
 - private System.Data.DataSet myDataSet;
 - C#連接數(shù)據(jù)庫代碼-本地MDB數(shù)據(jù)庫連接
 - private void menuItem2_Click(object sender, System.EventArgs e)
 - {
 - try
 - {
 - //創(chuàng)建一個 OleDbConnection對象
 - string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = ../../database/MDB數(shù)據(jù)庫名稱.mdb" ;
 - OleDbConnection myConn = new OleDbConnection ( strCon ) ;
 - string strCom = " SELECT * FROM 數(shù)據(jù)表名稱" ;
 - //創(chuàng)建一個 DataSet對象
 - myDataSet = new DataSet ( ) ;
 - myConn.Open ( ) ;
 - OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
 - myCommand.Fill ( myDataSet , "數(shù)據(jù)表名稱" ) ;
 - myConn.Close ( ) ;
 - //關(guān)閉連接
 - statusBar1.Text="本地MDB數(shù)據(jù)庫連接成功";
 - }
 - catch ( Exception ex )
 - {
 - statusBar1.Text="本地MDB數(shù)據(jù)庫連接失敗";
 - MessageBox.Show ( "連接本地MDB數(shù)據(jù)庫發(fā)生錯誤:" + ex.ToString ( ) , "錯誤!" ) ;
 - }
 - }
 - c#遠(yuǎn)程MDB數(shù)據(jù)庫連接
 - c#本地SQL數(shù)據(jù)庫連接
 - private void menuItem3_Click(object sender, System.EventArgs e)
 - {
 - try
 - {
 - //創(chuàng)建一個 SqlConnection對象
 - string strCon = "Integrated Security=SSPI;Initial Catalog='數(shù)據(jù)庫名稱';Data Source='本地計(jì)算機(jī)名';User ID='登錄用戶名';Password='登錄用戶密碼';Connect Timeout=30";
 - SqlConnection myConn = new SqlConnection ( strCon ) ;
 - string strCom = " SELECT * FROM 數(shù)據(jù)表名稱" ;
 - //創(chuàng)建一個 DataSet對象
 - myDataSet = new DataSet ( ) ;
 - myConn.Open ( ) ;
 - SqlDataAdapter myCommand = new SqlDataAdapter ( strCom , myConn ) ;
 - myCommand.Fill ( myDataSet , "數(shù)據(jù)表名稱" ) ;
 - myConn.Close ( ) ;
 - //關(guān)閉連接
 - statusBar1.Text="本地SQL數(shù)據(jù)庫連接成功";
 - }
 - catch ( Exception ex1 )
 - {
 - statusBar1.Text="本地SQL數(shù)據(jù)庫連接失敗";
 - MessageBox.Show ( "連接本地SQL數(shù)據(jù)庫發(fā)生錯誤:" + ex1.ToString ( ) , "錯誤!" ) ;
 - }
 - }
 - --------------------------------------------------------------------------------------------------------------
 - c#遠(yuǎn)程SQL數(shù)據(jù)庫連接
 - private void menuItem10_Click(object sender, System.EventArgs e)
 - {
 - try
 - {
 - //創(chuàng)建一個SqlConnection對象
 - string strCon = "Initial Catalog='數(shù)據(jù)庫名稱';Server='遠(yuǎn)程IP地址,1433';User ID='登錄用戶名';Password='登錄用戶密碼';Persist Security Info=True";
 - SqlConnection myConn = new SqlConnection ( strCon ) ;
 - string strCom = " SELECT * FROM 數(shù)據(jù)表名稱" ;
 - //創(chuàng)建一個 DataSet對象
 - myDataSet = new DataSet ( ) ;
 - myConn.Open ( ) ;
 - SqlDataAdapter myCommand = new SqlDataAdapter ( strCom , myConn ) ;
 - myCommand.Fill ( myDataSet , "數(shù)據(jù)表名稱" ) ;
 - myConn.Close ( ) ;
 - //關(guān)閉連接
 - statusBar1.Text="遠(yuǎn)程SQL數(shù)據(jù)庫連接成功";
 - }
 - catch ( Exception ex2 )
 - {
 - statusBar1.Text="連接遠(yuǎn)程SQL數(shù)據(jù)庫失敗";
 - MessageBox.Show ( "連接遠(yuǎn)程SQL數(shù)據(jù)庫發(fā)生錯誤:" + ex2.ToString ( ) , "錯誤!" ) ;
 - }
 - }
 
C#連接數(shù)據(jù)庫代碼就介紹到這里。
【編輯推薦】
責(zé)任編輯:彭凡 
                    來源:
                    百度空間
 














 
 
 
 
 
 
 