Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 15516

Create a Database Programmatically and

$
0
0
I am learning VB2019 Student edition and it is the very first time that I interact with the VB2019 code. The project to do is to Create a Database and a table programmatically separately. One program for creating a database and create a table. So, I just started with the database, and I got this code from a tutorial over the internet and I just compiled it but, I got this error BC30002 Type 'sqlconnection' is not defined. I was looking for some references on the internet and I did not find this error. Can somebody with visual basic knowledge help me, please?


Public Class Form1

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

Viewing all articles
Browse latest Browse all 15516

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>