2012-06-05 76 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參考。