?
Solved

VB.net Creating a SQL database

Posted on 2013-12-10
2
Medium Priority
?
246 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 2000 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:Murray Brown
ID: 39711063
Thanks
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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