2017-02-19 80 views
0

任何人都可以幫我解決這個SQL錯誤嗎?
如何使用xampp將它連接到phpmyadmin中的數據庫? 如果您認爲它有問題,請隨時糾正我的代碼。連接phpmyadmin

Imports System.Data.SqlClient 
Imports MySql.Data.MySqlClient 

Public Class Form1 
Dim con As SqlConnection 
Dim com As SqlCommand 

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load 
    con = New SqlConnection 
    con = New SqlCommand 
    con.ConnectionString = "initial catalog=student;user id=sa;pwd=" 
    con.Open() 
    con.Connection = con 
End Sub 

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click 
    If TextBox1.Text = " " Or TextBox2.Text = " " Or TextBox3.Text = " " Then 
     MsgBox("Please fill all the details") 
     Exit Sub 
    End If 
    com.CommandText = "insert into stud values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')" 
    com.ExecuteNonQuery() 
    MsgBox("1 record is stored successfully") 
    TextBox1.Clear() 
    TextBox2.Clear() 
    TextBox3.Clear() 
End Sub 
End Class 

enter image description here

+0

您是否閱讀過錯誤消息並查看了它所提到的幾行? 'con'只能是Command **或** Connection對象。還有更多的錯誤。您應該對IDisposable,COnnection Pooling和SQL參數進行一些研究。 – Plutonix

+0

另外MySQL和SQL Server是兩個非常不同的東西 - 沒有一個'System.Data.SqlClient'對象可以與MySQL一起工作。你也想訪問www.connectionstrings.com – Plutonix

回答

0

您應該使用的MySqlConnection,不是的SqlConnection。 SqlConnection用於SQL Server。它可以從Nuget包下載。

+1

當給出答案時,最好給[關於爲什麼你的答案的一些解釋](http://stackoverflow.com/help/how-to-answer)是一。 –