2012-06-05 131 views
-1

如何将Label.text数据插入到mySql表中。 我有textbox.text没有问题,但我无法弄清楚它是如何与Label.text 我尝试用textbox.text将VB.net标签插入到mysql表中

parameterB_Answer.Value = TextBox1.Text 

相同的代码它的工作发现,但是当我试图用

parameterB_Answer.Value = Label1.Text 

mySqlReader似乎无法读取它。

更新:

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

我的代码是:

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,它工作得很好。可能是因为我把INT的列label1.text来填补,而“1.1.1”是不是整数

感谢很多

PS:看来我不能发布图片,因为低信誉

+1

这不应该是可能的,一个文本是一个文本。你确定你的Label1有文字吗?请显示你的代码。 – Steve

+0

如果mySQLReader不能读取它,那么它的错误信息是什么?在这两种情况下,您都将一个字符串分配给'parameterB_Answer.Value' –

回答

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参考。