使用 VB.Net 连接到在线 MySQL 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2269237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connecting to a Online MySQL Database using VB.Net
提问by ritch
I've searched around and haven't been able to find anything along the lines of doing this.
我已经四处搜索,但没有找到任何与此相关的内容。
回答by Joel Coehoorn
You need to install Connector/Net, which will give you a full ADO.Net provider for MySql you can use. Be warned that this is GPL software, meaning if you distribute it as part of a commercial product you must also distribute your source code. It's an open legal question, but last I heard most web sites are okay with this, because you are not distributing your server code. Desktop apps may have an issue, though.
您需要安装Connector/Net,这将为您提供可以使用的 MySql 的完整 ADO.Net 提供程序。请注意,这是 GPL 软件,这意味着如果您将其作为商业产品的一部分分发,您还必须分发您的源代码。这是一个公开的法律问题,但最后我听说大多数网站都同意这一点,因为您没有分发服务器代码。不过,桌面应用程序可能有问题。
回答by MD SHANAWAZ RAHMAN
Imports System.Data.SqlClient
Imports MySql.Data.MySqlClient
Public Class LoginForm1
Dim MySQLConnection As MySqlConnection
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
Private Sub Cancel_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
End
End Sub
Private Sub OK_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
MySQLConnection = New MySqlConnection
MySQLConnection.ConnectionString = "server=db4free.net;Port=3306; User ID=db4freeusername; password=db4freepassword; database=nameofyourdatabase"
MySQLConnection.Open()
Dim MyAdapter As New MySqlDataAdapter
Dim SqlQuary = "SELECT * From nameofthetable WHERE Username='" & UsernameTextBox.Text & "' AND password = '" & PasswordTextBox.Text & "';"
Dim Command As New MySqlCommand
Command.Connection = MySQLConnection
Command.CommandText = SqlQuary
MyAdapter.SelectCommand = Command
Dim Mydata As MySqlDataReader
Mydata = Command.ExecuteReader
If Mydata.HasRows = 0 Then
MsgBox("Error During Login:Please Enter Valid Data")
Else
Form1.Show()
Me.Hide()
End If
End Sub
End Class
回答by GeoChatz
First of all you need to install MySQL connector for .NET.
Imports MySql.Data.MySqlClient
Dim myConnection As MySqlConnection = New MySqlConnection()
Dim myConnectionString As String = "Server=SERVERNAME;Database=DATABASE;Uid=root;Pwd=password;"
myConnection.ConnectionString = myConnectionString
myConnection.Open()
//execute queries, etc
myConnection.Close()
回答by klay
I use C#:
我使用 C#:
const String ConnectionString = "Driver={MySQL ODBC 5.1 Driver};Server=localhost;Port=3306;Database=test;User=root;Password=;Option=3;";
OdbcConnection conn = new OdbcConnection(ConnectionString);
conn.Open();
OdbcCommand command = new OdbcCommand();
command.CommandType = CommandType.StoredProcedure;
command.Connection = conn;
command.CommandText = "insert into search (tempsearchKey, state, suburb) values ('" + tempsearchKey+"','"+state+"','"+suburb+"')";
command.ExecuteNonQuery();
command.Cancel();
install odbc driver from mysql website
从 mysql 网站安装 odbc 驱动程序
and convert this to VB.NET,
并将其转换为 VB.NET,
Maybe this link could help:
也许这个链接可以帮助:
http://dev.mysql.com/tech-resources/articles/ebonat-load-and-search-mysql-data-using-vbnet-2005.html
http://dev.mysql.com/tech-resources/articles/ebonat-load-and-search-mysql-data-using-vbnet-2005.html
回答by Mineko
Install MySQL connector for .NET, and APACHE, also install XAMPP so you could use phpMyAdmin
为 .NET 和 APACHE 安装 MySQL 连接器,还安装 XAMPP,以便您可以使用 phpMyAdmin