vb.net 要将textbox中的信息写入数据库中的表中,代码如下,请大神们帮忙看看!
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handlesbt...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click
Dim connStr As String
connStr = String.Format("server={0};user id={1}; password={2};pooling=false", "localhost", "root", "123456")
Try
Dim Conn As New MySqlConnection(connStr)
Conn.Open()
Dim sql As String = "insert into xinxi(name,age,sex,Telephone)values('& TextBox1.Text & ''& TextBox2.Text & ''& TextBox4.Text & ''& TextBox5.Text & ')"
Dim sqlcmd As New MySqlCommand(sql, Conn)
sqlcmd.ExecuteScalar()
Conn.Close()
MessageBox.Show("插入成功!")
Catch ex As MySqlException 'try执行过程中发生错误就会提示'
'MessageBox.Show(ex.ToString)
MessageBox.Show("程序出现错误!请重启,或联系维护人员。", "抱歉")
Finally
conn.Close()
End Try
End Sub 展开
Dim connStr As String
connStr = String.Format("server={0};user id={1}; password={2};pooling=false", "localhost", "root", "123456")
Try
Dim Conn As New MySqlConnection(connStr)
Conn.Open()
Dim sql As String = "insert into xinxi(name,age,sex,Telephone)values('& TextBox1.Text & ''& TextBox2.Text & ''& TextBox4.Text & ''& TextBox5.Text & ')"
Dim sqlcmd As New MySqlCommand(sql, Conn)
sqlcmd.ExecuteScalar()
Conn.Close()
MessageBox.Show("插入成功!")
Catch ex As MySqlException 'try执行过程中发生错误就会提示'
'MessageBox.Show(ex.ToString)
MessageBox.Show("程序出现错误!请重启,或联系维护人员。", "抱歉")
Finally
conn.Close()
End Try
End Sub 展开
1个回答
展开全部
我觉得你这sql写的不对
Dim sql As String = "insert into xinxi (name, age, sex, Telephone) values ('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox4.Text & "','" & TextBox5.Text & "')"
注意这是一行,sql空格少一个都会出问题,text是字符两边加',数字就不用加了
给你推荐个网址
http://www.w3school.com.cn/sql/sql_insert.asp
再加一句,怕你看不清 ,这样写的' " & TextBox5.Text & " ')"
Dim sql As String = "insert into xinxi (name, age, sex, Telephone) values ('" & TextBox1.Text & "', '" & TextBox2.Text & "', '" & TextBox4.Text & "','" & TextBox5.Text & "')"
注意这是一行,sql空格少一个都会出问题,text是字符两边加',数字就不用加了
给你推荐个网址
http://www.w3school.com.cn/sql/sql_insert.asp
再加一句,怕你看不清 ,这样写的' " & TextBox5.Text & " ')"
追问
我代码按照你说的方法改完了 但是运行的结果还是跳出下面的错误的提示。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询