Solved

VB.net Creating a SQL database

Posted on 2013-12-10
2
240 Views
Last Modified: 2013-12-11
Hi

I found the following code to create a SQL database on one's local machine
I am not running SQL on my machine but instead have a few SQL databases
hosted on a web server and connect to them using a connection string in the
following format
Server=winsqlr01.cpt.wa.co.za;Database=MyDatabase;Uid=MyID;Password=pass
I just need a little advice on how to modify the following code to create
a database on a web server like the one my databases are hosted on

Private Sub btnCreateDatabase_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCreateDatabase.Click
    Dim str As String

    Dim myConn As SqlConnection = New SqlConnection("Server=(local)\netsdk;" & _
                                                    "uid=sa;pwd=;database=master")

    str = "CREATE DATABASE MyDatabase ON PRIMARY " & _
          "(NAME = MyDatabase_Data, " & _
          " FILENAME = 'D:\MyFolder\MyDatabaseData.mdf', " & _
          " SIZE = 2MB, " & _
          " MAXSIZE = 10MB, " & _
          " FILEGROWTH = 10%) " & _
          " LOG ON " & _
          "(NAME = MyDatabase_Log, " & _
          " FILENAME = 'D:\MyFolder\MyDatabaseLog.ldf', " & _
          " SIZE = 1MB, " & _
          " MAXSIZE = 5MB, " & _
          " FILEGROWTH = 10%) "

    Dim myCommand As SqlCommand = New SqlCommand(str, myConn)

    Try
        myConn.Open()
        myCommand.ExecuteNonQuery()
        MessageBox.Show("Database is created successfully", _
                        "MyProgram", MessageBoxButtons.OK, _
                         MessageBoxIcon.Information)
       Catch ex As Exception
           MessageBox.Show(ex.ToString())
       Finally
           If (myConn.State = ConnectionState.Open) Then
               myConn.Close()
           End If
       End Try

End Sub
0
Comment
Question by:murbro
2 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 39710238
Change your connection string in the code to connect to the remote SQL Server and probably drop the FILENAME attributes so it defaults to whatever the correct location is on the server.
0
 

Author Closing Comment

by:murbro
ID: 39711063
Thanks
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now