如何用wamp mysql连接vb

问题描述:

如何调用模块,以便vb中的每个表单都可以访问数据库而无需键入长代码和每个表单.该代码是否可用于模块,以及是否可以连接到数据库.或有其他解决方案?
谢谢,我感谢您的帮助


how to call for the module so that each form in vb can access the database without typing the long code and each form. whether the code can be used for module and can be or will not connect to database. or have other solution??
thanks and i appreciate for help


Option Strict On

Imports System.Data.SqlServerCe

Module Example

    Sub Main()
        Dim cs As String = "Database=apka;Data Source=localhost;" _
            & "User Id=root;Password="

        Dim stm As String = "SELECT VERSION()"
        Dim version As String
        Dim conn As MySqlConnection

        Try
            conn = New MySqlConnection(cs)
            conn.Open()

            Dim cmd As MySqlCommand = New MySqlCommand(stm, conn)

            version = Convert.ToString(cmd.ExecuteScalar())

            Console.WriteLine("MySQL version: {0}", version)

        Catch ex As MySqlException
            Console.WriteLine("Error: " & ex.ToString())
        Finally
            conn.Close()
        End Try

    End Sub

End Module

在不更改为更好的设计模式的情况下,您可以大量搬出.
但是,这并不是一个坏主意:看看实现 3层架构 [ ^ ]-想法是移动数据访问完全脱离表单.
There isn''t a huge amount you can move out without changing to a better design pattern.
However, that wouldn''t be a bad idea: have a look at implementing a 3 Tier Architecture[^] - the idea being to move the data access away from your forms completely.