将mysql数据库添加到VB.net应用程序 [英] Add mysql database to VB.net application

查看:81
本文介绍了将mysql数据库添加到VB.net应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用mysql workbench创建了数据库,我想使用数据源配置向导将其添加到我的vb.net应用程序数据源,请帮帮我。

解决方案

Quote:

在本文中,我将解释如何连接到VB.net中的MySql数据库。 MySql是一个开源的免费数据库。为了完成本教程,您需要在计算机上安装MySql。你可以从MySql网站下载MySql .www.mysql.com。



连接器/网

除了下载MySql数据库,您需要下载MySql Connector / Net驱动程序。此驱动程序使开发人员可以轻松创建.NET应用程开发人员可以使用他们选择的.NET语言来构建应用程序。 MySql Connector / Net是一个完全托管的ADO.NET驱动程序,用100%纯C#编写。



您需要从MySql网站下载此驱动程序。下载后只需完成安装过程。



控制台应用程序

是时候查看一些代码了。加载Visual Studios IDE并选择一个新的Visual Basic控制台应用程序。一旦你的新投影加载了你应该有一个空模块。



我们需要做的第一件事是添加对MySql程序集的引用。单击菜单中的项目,然后选择添加引用。在.Net选项卡下,浏览MySQL.Data程序集。



既然添加了引用,我们需要使用Imports指令导入

MySql.Data.MySqlClient命名空间。



您的进口指令应如下所示:



导入System.Data

导入MySql.Data.MySqlClient



要连接到MySql数据库,我们需要使用MySqlConnection类。这个类有两个构造函数。默认构造函数不带参数。第二个构造函数将连接字符串作为参数。如果使用默认构造函数,则可以使用ConnectionString属性在代码中稍后指定连接字符串。



在清单1.1中,我们使用第二个构造函数。< br $>


清单1.1





 con = New MySqlConnection(\ Server = \+ _host + \; User Id = \+ _user + \; Password = \+ _pass + \; \)

< br $> b $ b



引用:

在清单1.1中有一个MySqlConnection对象已创建。然后使用此对象连接到数据库。



下面的清单1.2显示了连接到MySql数据库和查询表的完整代码。 MySql数据库用于此示例。



清单1.2







 Imports System.Data 
Imports MySql.Data.MySqlClient

Module Module1
Private con As New MySqlConnection
Private cmd作为New MySqlCommand
私有阅读器As MySqlDataReader

Private _host As String = \localhost\\'连接到localhost数据库
Private _user As String = \ root \\'输入您的用户名,默认为root
私有_pass As String = \\\'输入您的密码

Sub Main()
con = New MySqlConnection(\Server = \+ _host + \; User Id = \+ _user + \; Password = \+ _pass + \; \)
尝试
con.Open()
\'检查连接是否打开
如果con.State = ConnectionState.Open则
con。 ChangeDatabase(\MYSQL \)\'在这个例子中使用MYSQL数据库
Console.WriteLine(\Connection Open \)
Dim Sql As String = \SELECT * FROM USER \\查询USER表以获取用户信息
cmd = New MySqlCommand(Sql,con)
reader = cmd.ExecuteReader()


\'浏览所有用户
虽然reader.Read()
Console.WriteLine(\HOST:\& reader.GetString(0))\'获取主机
Console.WriteLine(\USER:\& reader.GetString(1))\'获取用户名
控制台。 WriteLine(\PASS:\& reader.GetString(2))\'获取密码
结束时

结束如果


Catch ex As Exception
Console.WriteLine(ex.Message)\'显示任何错误。
结束尝试


Console.ReadKey()
结束子

结束模块





Quote:

检查以下链接

http:// java-samples.com/showtutorial.php?tutorialid=1019


I have created database with mysql workbench and I want to add it to my vb.net application datasource using the datasource configuration wizard, please help me.

解决方案

Quote:

In this article I will explain how to connect to a MySql database in VB.net. MySql is an open source free database. In order to follow along with this tutorial you will need to have MySql installed on your machine. You can download MySql from the MySql website.www.mysql.com.

Connector/Net
As well as downloading the MySql database, you will need to download the MySql Connector/Net driver. This driver enables developers to easily create .NET applications. Developers can build applications using their choice of .NET languages. MySql Connector/Net is a fully-managed ADO.NET driver written in 100% pure C#.

You will need to download this driver from the MySql website. Once downloaded simply go through the installation process.

Console Application
It’s time to look at some code. Load the Visual Studios IDE and select a new Visual Basic Console Application. Once your new projected has loaded you should have an empty module.

The first thing we need to do is 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.

Now that the reference has been added, we need to use the Imports directive to import the
MySql.Data.MySqlClient namespace.

Your imports directives should look like the following:

Imports System.Data
Imports MySql.Data.MySqlClient

To connect to the MySql database, we need to use the MySqlConnection Class. This class has two constructors. The default constructor takes no arguments. The second constructor takes a connection string as an argument. If you use the default constructor, you can specify the connection string later in your code by using the ConnectionString property.

Below in listing 1.1 we use the second constructor.

Listing 1.1



con = New MySqlConnection(\"Server=\" + _host + \";User Id=\" + _user + \";Password=\" + _pass + \";\")




Quote:

In listing 1.1 a MySqlConnection object is created. This object is then used to connect to the database.

Listing 1.2 below shows the complete code to connect to a MySql database and query a table. The MySql database is used for this example.

Listing 1.2




Imports System.Data
 Imports MySql.Data.MySqlClient

 Module Module1
    Private con As New MySqlConnection
    Private cmd As New MySqlCommand
    Private reader As MySqlDataReader

    Private _host As String = \"localhost\" \' Connect to localhost database
    Private _user As String = \"root\" \'Enter your username, default is root
    Private _pass As String = \"\" \'Enter your password

    Sub Main()
        con = New MySqlConnection(\"Server=\" + _host + \";User Id=\" + _user + \";Password=\" + _pass + \";\")
        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()


                \'Loop through all the users
                While reader.Read()
                    Console.WriteLine(\"HOST: \" & reader.GetString(0)) \'Get the host
                    Console.WriteLine(\"USER: \" & reader.GetString(1)) \'Get the username
                    Console.WriteLine(\"PASS: \" & reader.GetString(2)) \'Get the password
                End While

            End If


        Catch ex As Exception
            Console.WriteLine(ex.Message) \' Display any errors.
        End Try


        Console.ReadKey()
    End Sub

 End Module



Quote:

check the below link
http://java-samples.com/showtutorial.php?tutorialid=1019


这篇关于将mysql数据库添加到VB.net应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆