Quantcast
Channel: Oracle, MySQL, Sybase, Informix and other databases
Viewing all articles
Browse latest Browse all 1350

Newbie question - How do I connect to a MySql database using VB.NET?

$
0
0

Been trying to solve this by myself to no avail.... Been reading lots of info on Internet on the topic but still clueless. OK.

Example 1 - This is the code in a .aspx file I use to connect to my MSSQL server. Works just fine:

###

<%@ Page Language="VB"%>

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<html>

<head>

<title>Test</title>

</head>

<body> 

 <%

Dim sqlConn as SqlConnection  

sqlConn = new SqlConnection("Data Source=PCNAME\SQLEXPRESS;Initial Catalog=test1;Integrated Security=SSPI;Persist Security Info=false;User ID=testuser3;Password=;")

sqlConn.Open()

Response.Write("Connected to database") %>

</body>

</html>

###

Then, after some reading online I installed MySQL Connector Net 6.6.4 and created another .aspx file containing this code:

###

<%@ Page Language="VB" debug="true" %>

<%@ Import Namespace = "System.Data" %>

<%@ Import Namespace = "Microsoft.Data.Odbc" %>

<%@ Import Namespace = "MySql.Data" %>

<%@ Import Namespace = "MySql.Data.MySqlClient" %>

<html>

<head>

<title>Test</title>

</head>

 <body>

<div>

<% Dim mysqlConn as MySqlConnection

'open the connection

mysqlConn = new MySqlConnection("DRIVER={MySQL Connector Net 6.6.4 Driver};Server=127.0.0.1;Database=mysqltestdb;Uid=root;Pwd=rootmysqlpassword;OPTION=3")

mysqlConn.Open()

' do some operations here...

Response.Write("Connected to MySQL database")

' close the connection

mysqlConn.Close() %>

</div>

</body>

</html>

###

That didn't work, got this error:

Compiler Error Message: BC30002: Type 'MySqlConnection' is not defined.

Source Error:

Line 12: <div> Line 13: <% Line 14: Dim mysqlConn as MySqlConnection Line 15: Line 16: 'open the connection

Source File: C:\inetpub\wwwroot\dbplay2\ex_vbnetmysqlconn.aspx    Line: 14

Grasping for straws, I tried to a .aspx file with this code:

###

<%@ Page Language="VB" debug="true" %>

<%@ Import Namespace = "System.Data" %>

<%@ Import Namespace="System.Data.MySqlClient" %>

<html>

<head>

<title>Test</title>

</head>

<body>

<div>

<%

Dim mysqlConn as MySqlConnection

'open the connection

mysqlConn = new MySqlConnection("DRIVER={MySQL Connector Net 6.6.4 Driver};Server=127.0.0.1;Database=mysqltestdb;Uid=root;Pwd=rootmysqlpassword;OPTION=3")

mysqlConn.Open()

' do some operations here...

Response.Write("Connected to MySQL database")

' close the connection

mysqlConn.Close() %>

</div>

</body>

 </html>

###

Got the same error:

Compiler Error Message: BC30002: Type 'MySqlConnection' is not defined.

Source Error:

Line 10: <div> Line 11: <% Line 12: Dim mysqlConn as MySqlConnection Line 13: Line 14: 'open the connection

Source File: C:\inetpub\wwwroot\dbplay2\ex_vbnetmysqlconn.aspx    Line: 12

So, I'm guessing I'm doing something wrong when it comes to Dim mysqlConn as MySqlConnection... But what? Connecting to MSSQL in this manner works just fine (?)

 

 

 

 

 

 


Viewing all articles
Browse latest Browse all 1350

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>