Private Sub BuildDB()
btnBuildDatabase.Enabled = False
cmdAddColumn.Enabled = True
Dim
DBName As String = "MyBuiltDB"
' Build a connect
string.
Dim
connect_string As String
= "Server=SCHIMSKY;Integrated
Security=SSPI;Connect Timeout=5"
' Connect to the
server.
lblStatus.Text = "Connecting"
lblStatus.Refresh()
Dim
db_connection As New
SqlConnection(connect_string)
db_connection.Open()
' Make a SqlCommand
object.
Dim
sql_command As New
SqlCommand()
sql_command.Connection = db_connection
' Create the MyDB
database.
lblStatus.Text = "Building
database"
lblStatus.Refresh()
sql_command.CommandText = "CREATE DATABASE " & DBName
'sql_command.CommandText
= _
' "CREATE DATABASE
" & DBName & " " & _
' "ON (" & _
' " NAME = " & DBName &
"_data, " & _
' " FILENAME = 'C:\" & DBName
& ".mdf'," & _
' " SIZE = 2MB," & _
' " MAXSIZE = 10MB," & _
' " FILEGROWTH = 2MB)" & _
' "LOG ON("
& _
' " NAME = MyDB_log," & _
' " FILENAME = 'C:\" & DBName
& ".ldf'," & _
' " SIZE = 1MB," & _
' " MAXSIZE = 5MB," & _
' "FILEGROWTH = 1MB)"
Try
sql_command.ExecuteNonQuery()
Catch
ex As SqlException
MessageBox.Show(ex.Message)
End Try
' Use the new
database.
lblStatus.Text = "Selecting
database"
lblStatus.Refresh()
sql_command.CommandText = "USE " & DBName
sql_command.ExecuteNonQuery()
' Create the Books
table.
lblStatus.Text = "Creating
table"
lblStatus.Refresh()
sql_command.CommandText = _
"CREATE TABLE Books ("
& _
" Title VARCHAR(60)," & _
" URL VARCHAR(60) NOT NULL," & _
" ID INT NOT NULL PRIMARY KEY IDENTITY(1,1)
)"
'In the code
above we've used the CREATE TABLE command to create
'our BOOKS table. Notice how the ID field is specified as a
primary key
'and also as a unique identity. The IDENTITY(1,1)
part of the code tells SQL Server
'that the widgetId field should have a start value of 1 and
should be incremented by 1
'every time a new record is added to the widgets table.
Try
sql_command.ExecuteNonQuery()
Catch
ex As SqlException
MessageBox.Show(ex.Message)
End Try
' Insert a record.
lblStatus.Text = "Inserting
record"
lblStatus.Refresh()
sql_command.CommandText = _
"INSERT INTO Books (Title,
URL) VALUES (" & _
"'Visual Basic .NET Database
Programming'," & _
"'http://www.vb-helper.com/vbdb.htm')"
sql_command.ExecuteNonQuery()
' Stop using the
new database.
lblStatus.Text = "Using
Master"
lblStatus.Refresh()
sql_command.CommandText = "USE
Master"
sql_command.ExecuteNonQuery()
' Drop the
database.
' lblStatus.Text = "Dropping
database"
' lblStatus.Refresh()
' sql_command.CommandText = "DROP
DATABASE MyDB"
' sql_command.ExecuteNonQuery()
' Close the
connection.
db_connection.Close()
lblStatus.Text = "Done"
End Sub
Private Sub cndDelete_Click(ByVal sender As
System.Object, ByVal e As
System.EventArgs) Handles cndDelete.Click
cndDelete.Enabled = False
btnBuildDatabase.Enabled = True
Dim
DBName As String = "MyBuiltDB"
' Build a connect
string.
Dim
connect_string As String
= "Server=SCHIMSKY;Integrated
Security=SSPI;Connect Timeout=5"
' Connect to the
server.
lblStatus.Text = "Connecting"
lblStatus.Refresh()
Dim
db_connection As New
SqlConnection(connect_string)
db_connection.Open()
' Make a SqlCommand
object.
Dim
sql_command As New
SqlCommand()
sql_command.Connection = db_connection
' Stop using the
new database.
lblStatus.Text = "Using
Master"
lblStatus.Refresh()
sql_command.CommandText = "USE
Master"
sql_command.ExecuteNonQuery()
'Drop the
database.
lblStatus.Text = "Dropping
database"
lblStatus.Refresh()
sql_command.CommandText = "DROP DATABASE " & DBName
Try
sql_command.ExecuteNonQuery()
Catch
ex As SqlException
MessageBox.Show(ex.Message)
End Try
'Close the
connection.
db_connection.Close()
lblStatus.Text = "Done"
End Sub
Private Sub cmdAddColumn_Click(ByVal sender As
System.Object, ByVal e As
System.EventArgs) Handles cmdAddColumn.Click
cmdAddColumn.Enabled = False
cndDelete.Enabled = True
Dim
DBName As String = "MyBuiltDB"
' Build a connect
string.
Dim
connect_string As String
= "Server=SCHIMSKY;Integrated
Security=SSPI;Connect Timeout=5"
' Connect to the
server.
lblStatus.Text = "Connecting"
lblStatus.Refresh()
Dim
db_connection As New
SqlConnection(connect_string)
db_connection.Open()
Dim
sql_command As New
SqlCommand()
sql_command.Connection = db_connection
sql_command.CommandText = "USE " & DBName
sql_command.ExecuteNonQuery()
sql_command.CommandText = "ALTER
TABLE Books ADD PRICE MONEY NOT NULL DEFAULT 0"
sql_command.ExecuteNonQuery()
lblStatus.Text = "Column
Added"
End Sub