INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Programmatically create instance and database?

Programmatically create instance and database?

(OP)
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 

RE: Programmatically create instance and database?

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.

https://www.sqlite.org/







----------------------------------
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

RE: Programmatically create instance and database?

(OP)
Thank you, Vacunita, that info on SQLite appears to be exactly the kind of help I needed for this.

RE: Programmatically create instance and database?

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

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close