简体   繁体   English

从VB.NET将数据插入MySQL数据库

[英]Inserting data to MySQL database from VB.NET

I used the following code to insert data from my VB.NET into the MySQL database. 我使用以下代码将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()

However I get the following error message 但是我收到以下错误消息

Unknown column 'B5' in 'field list' “字段列表”中的未知列“ B5”

B5 is what I entered into the text box, but I do not know what field list refers to B5是我在文本框中输入的内容,但我不知道该字段列表指的是什么

I thin, its a problem with the Parameters statement 我瘦了,这是参数语句的问题

v v

cmd.CommandText = "insert into privileges values (@userId,@uname....,@pro5)";

参数以@s标记

Thats a decent way of doing it; 那是一种体面的方式; however, I have this way that works as well: 但是,我也有这种方法:

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 结束子

Try that and write back how it worked. 试试看并写回它是如何工作的。 Its a bit more functional because it lays out each step clearer. 它的功能更加强大,因为它使每个步骤的布局更加清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM