[英]VB.net mysql insert not working
I am trying to run an insert statement in vb.net to add a record. 我正在尝试在vb.net中运行插入语句以添加记录。 the line provided by the application works when I run it on mysql console - and the mysql query is erroring at adaptr.Fill(utable), I am now thinking it is because there is not any data to fill to my datatable.
当我在mysql控制台上运行它时,该应用程序提供的行有效-并且mysql查询在Adapter.Fill(utable)处出错,我现在想这是因为没有任何数据可填充到我的数据表中。 This exact format is working elsewhere in my application with a Select and an update statement.
这种精确格式可以在我的应用程序中的其他地方使用Select和update语句工作。 Any ideas?
有任何想法吗?
Dim con As New MySqlConnection()
Dim adptr As New MySqlDataAdapter
Public utable As New DataTable
con.ConnectionString = "server=localhost;" _
& "user id=user;" _
& "password=password;" _
& "database=DMT;"
adptr = New MySqlDataAdapter(String.Format("INSERT INTO users (uname, pword, fname, lname, Usertype) VALUES ({0}, md5('{1}'), {2}, {3}, {4});", MySqlHelper.EscapeString(TextBox4.Text), MySqlHelper.EscapeString(passw), MySqlHelper.EscapeString(TextBox1.Text), MySqlHelper.EscapeString(TextBox2.Text), ulevl), con)
Try
adptr.Fill(utable)
Catch err As Exception
Dim strError As String = "Exception: " & err.ToString()
MsgBox(strError)
End Try
I would look into using parameters when building your SQL statement. 在构建SQL语句时,我会考虑使用参数。 You can use the code below to insert a record into the database.
您可以使用下面的代码将记录插入数据库。
Dim cb As New MySqlConnectionStringBuilder
cb.Database = "DMT"
cb.Server = "localhost"
cb.UserID = "user"
cb.Password = "password"
Using cnn As New MySqlConnection(cb.ConnectionString)
Using cmd As New MySqlCommand("INSERT INTO users (uname, pword, fname, lname, Usertype) VALUES (@uname, md5(@pword), @fname, @lname, @usertype)", cnn)
cmd.Parameters.AddWithValue("@uname", TextBox4.Text)
cmd.Parameters.AddWithValue("@pword", passw)
cmd.Parameters.AddWithValue("@fname", TextBox1.Text)
cmd.Parameters.AddWithValue("@lname", TextBox2.Text)
cmd.Parameters.AddWithValue("@usertype", ulevl)
Try
cnn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End Using
End Using
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.