3

I am creating a vb .net winform project that uses a sql server 2008 database. I have the project more or less finished but I want to tweak it a bit. One of the tweaks is as follows. I have created the database within sql server 2008 but I am wondering how do I go about programmatically creating the database within the vb .net project. I have searched the internet on this matter but nothing is clear enough for me. Do I create the database within the opening form or do I create it in a separate class and call it in the other forms I am using the database in? Any help with this matter would be greatly appreciated.

4
  • support.microsoft.com/kb/305079 read Commented Jul 29, 2014 at 9:26
  • I know how to code the database but I am wondering where I should be putting the code? Should it be in the start up form or in a separate class and call the database from that class? Commented Jul 29, 2014 at 9:30
  • 1
    Personally I keep it in a separate project. If your WinForms project is fairly large, then create a 'DataAccess.dll' and all you then have to do is call 'GetListOfUsers' or whatever to retrieve your data. (In other words, separate class!). Or course it's a preference too. Commented Jul 29, 2014 at 9:43
  • Ok. So what you're saying is I should create the database in another class and call the database in the forms as I need it? If I create the exact same database programmatically in vb .net class as the one I have created in sql server, do I have to change the code I already have for querying the existing database? Or can I leave it as it is? Commented Jul 29, 2014 at 10:33

1 Answer 1

5

The following code will help you to create a database named my_db and a table within the database named as customer.

Note: necessary explanations are given in comments please read it for clarification

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        //creating and initializing the connection string
        Dim myConnectionString As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=master;Integrated Security=True;Pooling=False")
        //since we need to create a new database set the Initial Catalog as Master
        //Which means we are creating database under master DB
        Dim myCommand As String //to store the sql command to be executed
        myCommand = "CREATE database my_db" //the command that creates new database
        Dim cmd As SqlCommand = New SqlCommand(myCommand, myConnectionString) // creating command for execution
        Try
            cmd.Connection.Open() //open a connection with cmd
            cmd.ExecuteNonQuery() //Execute the query
            cmd.Connection.Close() //Close the connection
        Catch
            MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
        End Try
        //Creating table to the dynamicaly created database
        Try
            Dim cn As SqlConnection = New SqlConnection("Data Source=(local)\SQLEXPRESS;Initial Catalog=my_db;Integrated Security=True;Pooling=False")
          //here the connection string is initialized with Initial Catalog as my_db
            Dim sql As String //sql query string
            sql = "CREATE TABLE customer(cus_name varchar(50) NULL,address varchar(50) NULL,mobno numeric(18, 0) NULL,tin varchar(50) NULL,kg varchar(50) NULL)"
            cmd = New SqlCommand(sql, cn) // create command with connection and query string 
            cmd.Connection.Open()
            cmd.ExecuteNonQuery()
            cmd.Connection.Close()
          Catch
           MsgBox(" Already installed database", MsgBoxStyle.Critical, " MaS InfoTech- Warning")
          End Try
    End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.