Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Programmatically create instance and database? 1

Status
Not open for further replies.

GhostWolf

Programmer
Jun 27, 2003
290
US
First question: is it possible to programmatically create both a MySQL instance and a database within it?

I'm working in VB.NET; have MySQL Connector Net v6.9.8 installed. I'd like to write a program that can create/use a MySQL database without MySQL having to be installed on the user's PC - but haven't yet managed to succeed at it.

I've found, and tried, several snippets but it looks like they all are predicated on a running instance of MySQL being in place. The only mentions I've been able to find of creating an instance of MySQL dealt with installing the product.

The code from my latest attempt:
Code:
Imports MySql.Data.MySqlClient

Public Class frmMain
    Dim cnData As MySqlConnection
    Dim cnStr As String
    Dim cmData As MySqlCommand
    Dim cmdSQL As String

    Private Sub frmMain_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        cnStr = "Server=localhost; userid=root; password=mysql;"
        Try
            cnData = New MySqlConnection(cnStr)
            cnData.Open()
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try

        Try
            cmData = New MySqlCommand("create database if not exists SBE49", cnData)
            cmData.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try

        Try
            cnData.ChangeDatabase("SBE49")
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try

        Try
            cmdSQL = "create table if not exists Report (" & _
                     "rYear int,rMonth int,Precinct char(4)," & _
                     "Office varchar(50),Candidate varchar(50)," & _
                     "Votes int,CD int,SEQ int)"
            cmData = New MySqlCommand(cmdSQL, cnData)
            cmData.ExecuteNonQuery()
        Catch ex As Exception
            MsgBox(ex.Message)
            Exit Sub
        End Try

        cnData.Close()
        End
    End Sub
End Class
 
I'll answer your second question first:

You can create a DB programmatically. Since creating a database just requires you to issue a "query" statement. i.e "CREATE DATABASE mydbname" etc.... You can issue the create statement just like you would any other query. Assuming the user you are connecting as has sufficient privileges to create DBs. That's the simple part.

But to answer your first question, if MYSQL is not installed where are you going to create the DB?

Unless you are going to write your own Database Server within your application, you cannot magically invoke a server instance out of the ether. There needs to be something there for you to call upon.

There are certain database managers out there that can be used without a server installation, but MYSQL is not one of them.

Perhaps you may want to look into SQLite. This can be bundled with an Application, and used directly without a server application being installed.










----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Thank you, Vacunita, that info on SQLite appears to be exactly the kind of help I needed for this.
 
You're welcome. Glad I could help.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top