Solved

VB.net Creating a SQL database

Posted on 2013-12-10
2
235 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.net PaintEventArgs Handling. 10 33
Visual Studio Front End "Web Forms" For SQL Server Tables 5 72
Hide Tab Page 3 33
Getting multiple values from a dataset ASP.NET(VB) 3 37
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.

932 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

13 Experts available now in Live!

Get 1:1 Help Now