2013-07-22 48 views
0

我想插入到一個MySQL表。這裏是我的代碼...如何從asp.net使用vb插入到mysql?

 Dim cmdText = "INSERT INTO complete_log ('cust_id', 'business', 'note', 'date_stamp') values (@cid,@bus,@notey,@datey)" 
    Dim conn As New MySqlConnection("server=theipofserver;user id=id;password=mypass;database=thedatabase") 
    Using cmd = New MySqlCommand(cmdText, conn) 
     conn.Open() 
     Dim testy As String 
     testy = TextBox1.Text 
     Dim dater = Date.Today + TimeOfDay 
     cmd.Parameters.AddWithValue("@cid", c_id) 
     cmd.Parameters.AddWithValue("@bus", c_business) 
     cmd.Parameters.AddWithValue("@notey", testy) 
     cmd.Parameters.AddWithValue("@datey", dater) 
     cmd.ExecuteNonQuery() 
    End Using 

不過,我得到以下錯誤:

MySql.Data.MySqlClient.MySqlException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''cust_id', 'business', 'note', 'date_stamp') values ('ID','null','first one'' at line 1 

我試圖插入到表有4列均與足夠的空間(VARCHAR VARCHAR處理(100))

下面是有關該錯誤的詳細信息...在線路31發生錯誤...

Line 29:    cmd.Parameters.AddWithValue("@notey", testy) 
Line 30:    cmd.Parameters.AddWithValue("@datey", dater) 
Line 31:    cmd.ExecuteNonQuery() 
Line 32:   End Using 
Line 33:   TextBox1.Font.Bold = True 

那麼,我在這裏做錯了什麼?任何有關更好的方法插入到MySQL表的任何提示將不勝感激!

+1

不要忘記關閉連接(更好地在使用語句中) – Steve

回答

3

不要在表名或列名周圍使用引號。

INSERT INTO complete_log (`cust_id`, `business`, `note`, `date_stamp`) 
values (@cid,@bus,@notey,@datey) 

如果您想要轉義表和列名稱,請使用反引號。但你只需要逃避reserved words in MySQL

+0

現在就試試這個! – Feign

+0

嗯,我現在感到很傻...謝謝你!我會在幾分鐘內將此標記爲答案。 – Feign

1

不指定apostrophies列名

而不是

Dim cmdText = "INSERT INTO complete_log ('cust_id', 'business', 'note', 'date_stamp') values (@cid,@bus,@notey,@datey)" 

Dim cmdText = "INSERT INTO complete_log (cust_id, business, note, date_stamp) values (@cid,@bus,@notey,@datey)" 
+0

謝謝Yuriy,我不知道爲什麼我甚至沒有看到兩次...... – Feign

1

變化

INSERT INTO complete_log ('cust_id', 'business', 'note', 'date_stamp') 

INSERT INTO complete_log (cust_id, business, note, date_stamp)