前言
對于專業(yè)的程序員來說,C#連接SqlServer進行新增、修改、查詢哪簡直就是小菜一碟,信手拈來 ,但對剛?cè)腴T孩童們就不是哪么一回事了,有時一個代碼出錯就折騰好幾天,所以下面這篇文章就來給大家介紹下C#連接SQL Server的相關(guān)內(nèi)容。
C# 用 Connection 連接數(shù)據(jù)庫,一般在連接字符串里需要指定要連接數(shù)據(jù)源的種類、數(shù)據(jù)庫服務(wù)器的名稱、數(shù)據(jù)庫名稱、登陸用戶名、密碼、等待連接時間、安全驗證設(shè)置等參數(shù)信息,這些參數(shù)之間用分號隔開。下面將詳細描述這些常用參數(shù)的使用方法。
1. Provider 參數(shù)
Provider 參數(shù)用來指定要連接數(shù)據(jù)源的種類。如果使用的是 SQL Server Data Provider,則不需要指定 Provider 參數(shù),因為 SQL Server Data Provider 已經(jīng)指定了所要連接的數(shù)據(jù)源是 SQL Server 服務(wù)器。如果要使用的是 OLE DB Provider 或其他連接數(shù)據(jù)庫,則必須指定 Provider 參數(shù)
2. Server 參數(shù)
Server 參數(shù)用來指定需要連接的數(shù)據(jù)庫服務(wù)器(或數(shù)據(jù)域)。例如,Server=(local)
指定連接的數(shù)據(jù)庫服務(wù)器是本地的。另外,如果連接的是遠端的數(shù)據(jù)庫服務(wù)器,則 Server 參數(shù)可以寫成 Server=IP
或 Server="遠程計算機名"的形式。Server 參數(shù)也可以寫成Data Source
,如:Data Source=IP
。例如:
1
2
3
|
server=(local); Initial Catalog=student; user Id=sa; password=; Data source=(local); Initial Catalog=student; user Id=sa; password=; |
3. DataBase 參數(shù)
DataBase 參數(shù)用來指定連接數(shù)據(jù)庫名,如:DataBase=Master
,說明連接的數(shù)據(jù)庫是 Master。DataBase 參數(shù)也可以寫成 Initial catalog,如:Initial catalog=Master
。
4. Uid 參數(shù)和 Pwd 參數(shù)
Uid 參數(shù)用來指定登錄數(shù)據(jù)源的用戶名,也可以寫成 user ID
Pwd 參數(shù)用來指定連接數(shù)據(jù)庫的密碼,也可以寫成 password
5. Connect Timeout 參數(shù)
Connect Timeout 參數(shù)用于指定打開數(shù)據(jù)庫時的最大等待時間,單位是秒。如果不設(shè)置此參數(shù),則默認為15秒。如果設(shè)置成-1,表示無限等待
6. Integrated Security 參數(shù)
Integrated Security 參數(shù)用來說明登錄到數(shù)據(jù)源時是否使用SQL Server的集成安全驗證。如果為 True,則使用 Windows 身份驗證模式
1
|
Data Source=(local); Initial catalog=student; Integrated Security=SSPI; |
下面是一個代碼實例:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
|
private void BindStudent() { // strCon 為連接字符串 string strCon = @"data source=(local);initial catalog=DRUGS;integrated security=true" ; using (SqlConnection con = new SqlConnection(strCon)) { con.Open(); if (con.State == ConnectionState.Open) { string strCmd = "select * from alldrugs" ; SqlDataAdapter da = new SqlDataAdapter(strCmd, strCon); //創(chuàng)建一個dataset接收da申請下來的數(shù)據(jù) DataSet ds = new DataSet(); da.Fill(ds); //創(chuàng)建三個空的table,分別接收ds中的0-29,30-59,60-89條數(shù)據(jù) DataTable table1 = new DataTable(); DataTable table2 = new DataTable(); DataTable table3 = new DataTable(); table1 = ds.Tables[0].Clone(); //克隆表的結(jié)構(gòu)傳遞給table1 table2 = ds.Tables[0].Clone(); //克隆表的結(jié)構(gòu)傳遞給table2 table3 = ds.Tables[0].Clone(); //克隆表的結(jié)構(gòu)傳遞給table3 for ( int i = 0; i < 90; i++) { DataRow dr = ds.Tables[0].Rows[i]; if (i < 30) { table1.Rows.Add(dr.ItemArray); } else if (i >= 30 && i < 60) { table2.Rows.Add(dr.ItemArray); } else if (i >= 60 && i < 90) { table3.Rows.Add(dr.ItemArray); } else { break ; } } this .Repeater1.DataSource = table1; this .Repeater1.DataBind(); this .Repeater2.DataSource = table2; this .Repeater2.DataBind(); this .Repeater3.DataSource = table3; this .Repeater3.DataBind(); } } } |
別忘了在使用 SqlConnection 之前要導入命名空間
1
2
|
using System.Data; using System.Data.SqlClient; |
最后說一下為什么要使用 using()
這種方式來連接數(shù)據(jù)庫并進行相關(guān)的操作,是因為使用 using 會自動幫我們關(guān)閉掉數(shù)據(jù)庫連接。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對服務(wù)器之家的支持。
原文鏈接:https://www.jianshu.com/p/d33be8683d9a