原創(chuàng)|使用教程|編輯:郝浩|2013-06-07 14:05:14.000|閱讀 202 次
概述:如何插入數(shù)據(jù)到表格是使用 SQL Server的重要部分。本文主要講述了插入數(shù)據(jù)到表格的方法,簡(jiǎn)單、易懂。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
相關(guān)鏈接:
之前講過(guò)了如何連接服務(wù)器和在SQL Server創(chuàng)建數(shù)據(jù)庫(kù)對(duì)象。本文則主要講了如何依靠執(zhí)行SQL詢(xún)問(wèn),使用SqlCommand組件插入數(shù)據(jù)到表格。
基本信息
服務(wù)器上的數(shù)據(jù)可以用數(shù)據(jù)操作語(yǔ)言修改。有特權(quán)的賬戶(hù)可以在服務(wù)器上執(zhí)行DML語(yǔ)句。
有兩種方法可以操控?cái)?shù)據(jù)。其一是你可以手動(dòng)建造DML語(yǔ)句并在像 SqlCommand的組件中運(yùn)行她們。另一種方法是使用提供圖形用戶(hù)界面來(lái)管理數(shù)據(jù)庫(kù)的設(shè)計(jì)時(shí)功能。
這個(gè)教程的目標(biāo)就是插入下面的數(shù)據(jù)到表格dept和emp:
表格dept
deptno |
dname |
loc |
10 |
Accounting |
New York |
20 |
Sales |
Dallas |
30 |
Sales2 |
Chicago |
表格emp
empno |
ename |
job |
mgr |
hiredate |
sal |
comm |
deptno |
7369 |
Smith |
Clerk |
7566 |
1980-12-17 |
800 |
Null |
20 |
7499 |
Allen |
Salesman |
7698 |
1981-02-20 |
1600 |
300 |
30 |
7521 |
Ward |
Salesman |
7698 |
1981-02-22 |
1250 |
500 |
30 |
7566 |
Jones |
Manager |
7839 |
1981-04-02 |
2975 |
Null |
20 |
7654 |
Martin |
Salesman |
7698 |
1981-09-28 |
1250 |
1400 |
30 |
7698 |
Blake |
Manager |
7839 |
1981-05-01 |
2850 |
Null |
30 |
7839 |
King |
President |
Null |
1981-11-17 |
5000 |
Null |
10 |
在運(yùn)行時(shí)插入數(shù)據(jù)
你可以用下面的語(yǔ)句把第一行插入表格dept:
插入dept(deptno, dname, loc)值(10,'Accounting','New York')
下面的代碼片段執(zhí)行查詢(xún):
[C#]
SqlConnection conn = new SqlConnection("User Id=sa;Password=mypassword;DataSource=127.0.0.1;Database=Test"); SqlCommand cmd = new SqlCommand(); cmd.CommandText = "INSERT INTO dept (deptno, dname, loc) VALUES (10,'Accounting','New York')"; cmd.Connection = conn; conn.Open(); try { int aff = cmd.ExecuteNonQuery(); MessageBox.Show(aff + " rows were affected."); } catch { MessageBox.Show("Error encountered during INSERT operation."); } finally { conn.Close(); }
[Visual Basic]
Dim conn As SqlConnection = New SqlConnection("User Id=sa;Password=mypassword;DataSource=127.0.0.1;Database=Test") Dim cmd As SqlCommand = New SqlCommand() cmd.CommandText = "INSERT INTO dept (deptno, dname, loc) VALUES (10,'Accounting','New York')" cmd.Connection = conn conn.Open() Try Dim aff As Integer = cmd.ExecuteNonQuery() MessageBox.Show(aff & " rows were affected.") Catch MessageBox.Show("Error encountered during INSERT operation.") Finally conn.Close() End Try
這個(gè)例子先與硬編碼連接字符串創(chuàng)建了一個(gè)連接。然后創(chuàng)建了SqlCommand對(duì)象,指定查詢(xún)的文本和連接到SqlCommand實(shí)例。然后連接就打開(kāi)了。SqlCommand 的ExecuteNonQuery()方法在CommandText屬性中運(yùn)行了SQL語(yǔ)句并回到被查詢(xún)影響的行中。
如果查詢(xún)成功執(zhí)行,你會(huì)被通知受影響的行數(shù)。如果一些錯(cuò)誤出現(xiàn)你會(huì)得到錯(cuò)誤信息。連接就會(huì)關(guān)閉。建議你使用try ... finally確保連接正常關(guān)閉。
設(shè)計(jì)時(shí)的建立
在設(shè)計(jì)時(shí)中相同的操作包括以下步驟:
INSERT INTO dept VALUES (20,'Sales','Dallas');
INSERT INTO dept VALUES (30,'Sales2','Chicago');
注意最后兩個(gè)步驟在SqlCommand編輯器中可能會(huì)更容易。為了調(diào)用可以它從SqlCommand快捷菜單中選擇CommandText項(xiàng)或單擊屬性窗口中這個(gè)屬性的點(diǎn)。
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:慧都控件網(wǎng)