當(dāng)前位置:
首頁(yè) >
[C#] C#访问数据库(SQL Server版本)
發(fā)布時(shí)間:2025/4/5
30
豆豆
生活随笔
收集整理的這篇文章主要介紹了
[C#] C#访问数据库(SQL Server版本)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
//導(dǎo)入程序中用的的所有名稱(chēng)空間
using System ;
using System.Data.OleDb ;?
class OleDbTest {?
??? public static void Main ( ){?
??????? string strConnect = " Provider=SQLOLEDB.1;Persist Security Info=False;User ID = sa;Initial Catalog=xsgl;Data Source = czdy1 " ;?
??? //建立指向數(shù)據(jù)庫(kù)的連接
?? OleDbConnection aConnection = new OleDbConnection ( strConnect ) ;
??? //設(shè)計(jì)所需要返回的數(shù)據(jù)集的內(nèi)容?
?? OleDbCommand aCommand = new OleDbCommand ( "select * from xsk" , aConnection ) ;
?? try {
??????? //打開(kāi)指向數(shù)據(jù)庫(kù)連接
?? aConnection.Open ( ) ;
?? //返回需要的數(shù)據(jù)集內(nèi)容?
?? OleDbDataReader aReader = aCommand.ExecuteReader ( ) ;??
?? Console.WriteLine ( "以下就是打開(kāi)后的數(shù)據(jù)集的一個(gè)字段的所有內(nèi)容!" ) ;
????????//屏幕輸出數(shù)據(jù)集的第一個(gè)字段的所有內(nèi)容,如果要第二個(gè)字段把"0"改為"1"
?? while ( aReader.Read ( ) ) {
?? Console.WriteLine ( aReader.GetString (0) ) ;
?? }
?? ??? //關(guān)閉數(shù)據(jù)集?
?? ??? aReader.Close ( ) ;
?? ????//關(guān)閉指向數(shù)據(jù)庫(kù)的連接?
?????? aConnection.Close ( ) ;??
?? }
?? catch ( OleDbException e )
?? {?
??????? //如果出錯(cuò),輸出錯(cuò)誤信息
?? Console.WriteLine ( "錯(cuò)誤類(lèi)型:", e.Errors[0].Message ) ;
?? }
??}?
}
[說(shuō)明] 訪問(wèn)Sql server的程序代碼和訪問(wèn)Acess的程序代碼的主要區(qū)別有二點(diǎn):?
1.選用的數(shù)據(jù)庫(kù)引擎不一樣。訪問(wèn)Sql server的指向數(shù)據(jù)庫(kù)的連接要用以下語(yǔ)句:?
??string strConnect = " Provider=SQLOLEDB.1 ; Persist Security Info=False ; User ID = sa ; Initial Catalog=xsgl ; Data Source = czdy1 " ;?其中"Initial Catalog"是要選用的數(shù)據(jù)庫(kù)名稱(chēng)。"Data Source"是提供數(shù)據(jù)庫(kù)服務(wù)的服務(wù)器名稱(chēng)。?
2.不要在導(dǎo)入System..Windows.Forms名稱(chēng)空間?,因?yàn)樵L問(wèn)的是遠(yuǎn)程數(shù)據(jù)庫(kù),不需要用到Application類(lèi)。 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀
using System ;
using System.Data.OleDb ;?
class OleDbTest {?
??? public static void Main ( ){?
??????? string strConnect = " Provider=SQLOLEDB.1;Persist Security Info=False;User ID = sa;Initial Catalog=xsgl;Data Source = czdy1 " ;?
??? //建立指向數(shù)據(jù)庫(kù)的連接
?? OleDbConnection aConnection = new OleDbConnection ( strConnect ) ;
??? //設(shè)計(jì)所需要返回的數(shù)據(jù)集的內(nèi)容?
?? OleDbCommand aCommand = new OleDbCommand ( "select * from xsk" , aConnection ) ;
?? try {
??????? //打開(kāi)指向數(shù)據(jù)庫(kù)連接
?? aConnection.Open ( ) ;
?? //返回需要的數(shù)據(jù)集內(nèi)容?
?? OleDbDataReader aReader = aCommand.ExecuteReader ( ) ;??
?? Console.WriteLine ( "以下就是打開(kāi)后的數(shù)據(jù)集的一個(gè)字段的所有內(nèi)容!" ) ;
????????//屏幕輸出數(shù)據(jù)集的第一個(gè)字段的所有內(nèi)容,如果要第二個(gè)字段把"0"改為"1"
?? while ( aReader.Read ( ) ) {
?? Console.WriteLine ( aReader.GetString (0) ) ;
?? }
?? ??? //關(guān)閉數(shù)據(jù)集?
?? ??? aReader.Close ( ) ;
?? ????//關(guān)閉指向數(shù)據(jù)庫(kù)的連接?
?????? aConnection.Close ( ) ;??
?? }
?? catch ( OleDbException e )
?? {?
??????? //如果出錯(cuò),輸出錯(cuò)誤信息
?? Console.WriteLine ( "錯(cuò)誤類(lèi)型:", e.Errors[0].Message ) ;
?? }
??}?
}
[說(shuō)明] 訪問(wèn)Sql server的程序代碼和訪問(wèn)Acess的程序代碼的主要區(qū)別有二點(diǎn):?
1.選用的數(shù)據(jù)庫(kù)引擎不一樣。訪問(wèn)Sql server的指向數(shù)據(jù)庫(kù)的連接要用以下語(yǔ)句:?
??string strConnect = " Provider=SQLOLEDB.1 ; Persist Security Info=False ; User ID = sa ; Initial Catalog=xsgl ; Data Source = czdy1 " ;?其中"Initial Catalog"是要選用的數(shù)據(jù)庫(kù)名稱(chēng)。"Data Source"是提供數(shù)據(jù)庫(kù)服務(wù)的服務(wù)器名稱(chēng)。?
2.不要在導(dǎo)入System..Windows.Forms名稱(chēng)空間?,因?yàn)樵L問(wèn)的是遠(yuǎn)程數(shù)據(jù)庫(kù),不需要用到Application類(lèi)。 《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專(zhuān)家共同創(chuàng)作,文字、視頻、音頻交互閱讀
總結(jié)
以上是生活随笔為你收集整理的[C#] C#访问数据库(SQL Server版本)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 人生如梦啊~~~
- 下一篇: C#集合类(HashTable, Dic