簡體   English   中英

從VB.NET將數據插入MySQL數據庫

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

我使用以下代碼將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

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

參數以@s標記

那是一種體面的方式; 但是,我也有這種方法:

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

結束子

試試看並寫回它是如何工作的。 它的功能更加強大,因為它使每個步驟的布局更加清晰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM