2014-03-04 196 views
0

對不起,我剛開始使用VB。我終於設法將我的程序與我的phpmyadmin帳戶連接起來,但是現在我遇到了這個小問題。將SQL數據插入VB數據庫

我想輸入用戶在表格中輸入的數據到數據庫表中。 我的語法錯了。 Im做以下幾點:

Dim SQLStatment As String = "INSERT INTO students(Title, Initial, Surname, Address, City, Country, Postcode) VALUES('" & vtital & vinital & surname & vstreet & vcity & vcountry & vpcode '")" 

如果有人能這麼好心給我如何進入多個可變數據到用vb.net這將是非常讚賞分貝的簡要說明。

+1

避免串連在sql命令..使用參數化的一個.. :) – matzone

回答

1

你忘了分離

應該是這樣..

Dim SQLStatment As String = "INSERT INTO students(Title, Initial, Surname, Address, City, Country, Postcode) VALUES('" & vtital & "','" & vinital & "','" & surname & "','" & vstreet & "','" & vcity & "','" & vcountry & "','" & vpcode & "')" 

後來..使用參數..

+0

非常感謝你給它的答案我錯誤'運算符&沒有爲類型字符串和windows.forms.textboxes定義。我在這裏錯過了什麼? – Marilee

+0

@Marilee ..對不起我的錯..它的更新.. – matzone

+0

同樣的錯誤:-(我明顯做錯了什麼,數據是由整數和字符串類型,這是從文本框接收。也許我的DIM聲明是錯誤? – Marilee

0

這應該工作:

Dim SQLStatment As String = String.Format("INSERT INTO students(Title, Initial, Surname, Address, City, Country, Postcode) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')", 
vtital, 
vinital, 
surname, 
vstreet, 
vcity, 
vcountry, 
vpcode)