-1

如何将 Label.text 数据插入到 mySql 表中。我对 textbox.text 没有问题,但我不知道如何使用 Label.text 我尝试使用 textbox.text 相同的代码

parameterB_Answer.Value = TextBox1.Text

它可以找到但是当我尝试使用

parameterB_Answer.Value = Label1.Text

mySqlReader 似乎无法读取它。

更新:

1.1.1 是 label1.text。我的想法是从 Label1 插入文本“1.1.1”作为主键和 Textbox(textbox1.text) 如下

我的代码是:

Try
    Dim StrSQL As String = "INSERT INTO boranga" & _
                           "(IdBorangA,Answers)" & _
                           "VALUES (@B_IdA,@B_Answer);"
    Dim myCommand As MySqlCommand = New MySqlCommand(StrSQL, conn.open)
    myCommand.CommandType = CommandType.Text
    Dim parameterB_IdA As MySqlParameter = New MySqlParameter("@B_IdA", MySqlDbType.VarChar, 300)
    parameterB_IdA.Value = Label1.Text
    Dim parameterB_Answer As MySqlParameter = New MySqlParameter("@B_Answer", MySqlDbType.VarChar, 300)
    parameterB_Answer.Value = TextBox1.Text
    With myCommand.Parameters
        .Add(parameterB_IdA)
        .Add(parameterB_Answer)
    End With

    Dim result As MySqlDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
    MsgBox("Tersimpan", vbYes, "boranga")

Catch SqlEx As MySqlException
    Throw New Exception(SqlEx.Message.ToString())
End Try

但是当我将 Label1.text (1.1.1) 的值更改为 111 时,它工作得很好。可能是因为我为 label1.text 的列填充 INT 而“1.1.1”不是整数

非常感谢

PS:似乎我不能发布图片,因为声誉低

4

1 回答 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

结束子

并插入 label.text 尝试首先替换其中一个 textbox.text 字段,看看它是否会接受它。如果是这样,那么答案就在格式中。

另外,不要忘记致电:

imports mysql.data.mysqlclient

并确保添加 mysql.data 引用。

于 2012-06-05T23:03:00.147 回答