我使用以下代码将VB.NET中的数据插入MySQL数据库。
Dim connString As String = "Database=user;Data Source=localhost;" _
& "User Id=root;Password="
Dim conn As New MySqlConnection(connString)
Dim cmd As New MySqlCommand()
Try
conn.Open()
cmd.Connection = conn
getFormData()
cmd.CommandText = "insert into privileges values (" & userId & "," & uname & "," & usb & "," & internet & "," & pro1 & "," & pro2 & "," & pro3 & "," & pro4 & "," & pro5 & ");"
cmd.Parameters.AddWithValue("userId", userId)
cmd.Parameters.AddWithValue("uname", uname)
cmd.Parameters.AddWithValue("usb", cbusb.CheckState) 'Status", Convert.ToInt32(usb))
cmd.Parameters.AddWithValue("internet", cbnet.CheckState) ' Convert.ToInt32(internet))
cmd.Parameters.AddWithValue("pro1", txtpro1.Text)
cmd.Parameters.AddWithValue("pro2", txtpro2.Text)
cmd.Parameters.AddWithValue("pro3", txtpro3.Text)
cmd.Parameters.AddWithValue("pro4", txtpro4.Text)
cmd.Parameters.AddWithValue("pro5", txtpro5.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("User Profile Created!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
conn.Close()
但是我收到以下错误消息
'字段列表'中的未知列'B5'
B5是我在文本框中输入的内容,但我不知道哪个字段列表指的是
我很瘦,这是一个参数声明
的问题v
答案 0 :(得分:1)
cmd.CommandText = "insert into privileges values (@userId,@uname....,@pro5)";
参数用@s
标记答案 1 :(得分:0)
这是一种体面的做法;但是,我也有这样的方式:
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim conn As MySqlConnection
'Connect to the database using these credentials
conn = New MySqlConnection
conn.ConnectionString = "server=your server site (generally long url); user id=login id for mysql user; password=self explanatory; database=name of the DB you're trying to reach"
'Try and connect (conn.open)
Try
conn.Open()
Catch myerror As MySqlException 'If it fails do this... (i.e. no internet connection, etc.)
MsgBox("Error connecting to database. Check your internet connection.", MsgBoxStyle.Critical)
End Try
'MySQL query (where to call for information)
Dim myAdapter As New MySqlDataAdapter
'Tell where to find the file with the emails/passes stored
Dim sqlquery = "SELECT * FROM the database you selected above WHERE Email = '" & txtEmail.Text & "' AND Password = '" & txtPassword.Text & "'"
Dim myCommand As New MySqlCommand
myCommand.Connection = conn
myCommand.CommandText = sqlquery
'Start query
myAdapter.SelectCommand = myCommand
Dim myData As MySqlDataReader
myData = myCommand.ExecuteReader
If myData.HasRows = 0 Then
MsgBox("Invalid email address or password.", MsgBoxStyle.Critical)
Else
MsgBox("Logged in as " & txtEmail.Text & ".", MsgBoxStyle.Information)
Me.Close()
End If
End Sub
尝试并回写它是如何工作的。它更具功能性,因为它使每一步更清晰。