Imports MySql. Data. MySqlClient
Public Class Form1
Dim conStr As String="server=" & amp; "Localhost" & amp; "; Uid="& amp; "Root" & amp; "; The PWD="& amp; "* * * * * *" & amp; "; The database=customs; Pooling=false "
Dim sqlStr As String
Dim objConn As New MySqlConnection (conStr)
Dim objAdp As New MySqlDataAdapter
Dim objDataSet As New DataSet
'test connection
The Function Connect_DB (ByVal constr As String)
'objConn=New MySqlConnection (constr)
Try
ObjConn. The Open ()
TextBox1. Text="database connection success!"
Catch the ex As Exception
TextBox1. Text=ex. Message
MsgBox (ex. Message & amp; VbCrLf & amp; "The database connection failed!" )
The Finally
ObjConn. Close ()
End the Try
End the Function
'to execute queries
The Public Function Query_DB (ByVal SQL As String) As DataTable
Dim objAdp=New MySqlDataAdapter (SQL, objConn)
Try
ObjAdp. The Fill (objDataSet, "QueryData")
DataGridView1. The DataSource=objDataSet. Tables (" QueryData ")
'Return dTable
Catch the ex As Exception
TextBox1. Text=SQL + vbCrLf + ex. Message
Return Nothing
The Finally
ObjConn. Close ()
End the Try
End the Function
Private Sub Button1_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles for. Click
Connect_DB (conStr)
End Sub
Private Sub Button2_Click (ByVal sender As System. Object, ByVal e the As System. EventArgs) Handles Button2. Click
Dim SQL="select * from printing_data"
Query_DB (SQL)
End Sub
The End of the Class
Running, click for The connect to The database, an error: The given key was not present in The dictionary.
After the conStr plus charset=utf8, As follows: Dim conStr As String="server=" & amp; "Localhost" & amp; "; Uid="& amp; "Root" & amp; "; The PWD="& amp; "222310" & amp; "; The database=customs; Pooling=false; Charset=utf8 "
Run, connect to the database successfully,
Click Button2 querying data, running to objAdp. The Fill (objDataSet, "QueryData") and an error: The given key was not present in The dictionary.
Instant window tip: Exception thrown: 'System. Collections. Generic. KeyNotFoundException' in mscorlib. DLL
What was bad, the great god, please give directions
In addition, the MySQL database set charset setting of utf8mp4, have tried it to utf8, invalid,
Other configurations: the.net Framework 4.7.2 SDK MySQL Connector.net 8.0.19 is newly installed
CodePudding user response:
Dim the connectionString As String="Server=localhost; The Database={# myDB}; Uid=root; The Pwd=20200221; Port=3306; The Connection Timeout=180; Allow the Zero Datetime=True "
Try this kind of writing.
CodePudding user response:
Never use MySql. I give you some advice, directly using linq to SQL classes, VS platform has now fully accomplish the automatic operation of the data, even in the past, such writing is also a problem, a large amount of code, to look into the past, completely unreadable, the lowest minimum you have to make a data access class, interface is the interface logic, winform, all operations can also get into the operational level, should be performed as the idea of MVC,CodePudding user response:
Dim the connectionString As String="Server=localhost; The Database={# myDB}; Uid=root; The Pwd=20200221; Port=3306; The Connection Timeout=180; Allow the Zero Datetime=True "
Try this way,
Error phenomenon still,
CodePudding user response:
Never use MySql. I give you some advice, directly using linq to SQL classes, VS platform has now fully accomplish the automatic operation of the data, even in the past, such writing is also a problem, a large amount of code, to look into the past, completely unreadable, the lowest minimum you have to make a data access class, interface is the interface logic, winform, all operations can also get into the operational level, should be performed as the idea of MVC,Is data access classes should be made, I'm simply verify whether feasible, if possible, will make it class, thank you,
I look at the linq to SQL, seemingly only support SQL server, want to use the MySQL need additional configuration, give it a try, thanks again,
Before I actually used ODBC connection MySQL, just looking at ADO.Net useful just want to try, the result is always an error
CodePudding user response:
Can be a great god can tell?