简体   繁体   中英

Connect to MySql database used in PHP, hosted by server, in visual studio VB.NET

I want to know if it's possible to connect to the database is use when i use PHP, in Visual Studio. I have the database hosted on a VPS server and wounder if(and how) i could connect my vb.net application to it.

And i also want to know how to get data from the database. I use Visual Studio 2010. I have VS 2012 and can use it if i need to.

I've tried to do it in the Server Explorer, and clicked "Connect to database". But i don't know if that's right and i don't know where and what to write. I know the mySql details, but not where to write them.

Yes you can, Please follow this steps :

  1. 1.You need to download the mysqlconnect (mysql-connector-net) from the MySQL website and add the DLL files to Visual Studio, the website is http://dev.mysql.com/downloads/connector/ . Extract the download file.
  2. Add a reference to the MySql assembly. Click the Project from the menu and then select Add Reference. Under the .Net tab, browse for the MySQL.Data assembly.
  3. On your class add this tow imports lines :

     Imports System.Data Imports MySql.Data.MySqlClient 
  4. To connect to the MySql database, we need to use the MySqlConnection Class :

     Dim con = New MySqlConnection("Data Source=localhost;user id=root;database=my_test_projects;") 
  5. Use it like this :

     Try con.Open() 'Check if the connection is open If con.State = ConnectionState.Open Then con.ChangeDatabase("MYSQL") 'Use the MYSQL database for this example Console.WriteLine("Connection Open") Dim Sql As String = "SELECT * FROM USER" ' Query the USER table to get user information cmd = New MySqlCommand(Sql, con) reader = cmd.ExecuteReader() End If Catch ex As Exception Console.WriteLine(ex.Message) ' Display any errors. End Try 

See This link Using MySQL Database with Visual Basic .NET 2010

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM