|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis article explains step by step dynamic creation of a SQL Server database, tables and stored procedure through Whidbey (or VB.NET). Steps to create the Database Application1. Database Connection Classes and its ParametersThe Imports System.Data.SqlClient
The The Dim myDBSQLConnection As New SqlConnection(MyConnection)
You need to be initialize connection strings for connecting to the database for both SQL connection and MSDE connection. This way if SQL DB connection fails, it will look through the MSDE connection to connect the database. The line of code for declaration of these connections are as shown below: MyConnection = "Server=localhost;" & _
"DataBase=;" & _
"Integrated Security=SSPI"Protected Const MY_MSDE_CONNECTION_STRING As String = _
"Server=(local)\;" & _
"DataBase=;" & _
"Integrated Security=SSPI"
2. Declare and Initialize the SqlCommand ObjectA Dim cmd As New SqlCommand(strSQL, MyConnection)
You can also declare Dim mySQL As String = _
"IF EXISTS (" & _
"SELECT * " & _
"FROM master..sysdatabases " & _
"WHERE Name = 'MyDatabase')" & vbCrLf & _
"DROP DATABASE MyDatabase " & vbCrLf & _
"CREATE DATABASE MyDatabase "
The above SQL Statement do the following operations:
3. Open the Connection, Execute the query for creating the databaseOnce you successfully create the The following lines of code explain how to open the connection, execute the command and close the database connection from SQL Server database. MyConnection.Open()
cmd.ExecuteNonQuery()
MyConnection.Close()
Please note that the You can use You can also use Although For 4. Catching SQL Exception ErrorsCatching a SQL exception error is another way to avoid the SQL Server connection problems. The The following line of code explains about this. Try
Dim myCmd As New SqlCommand(strSQL, MyConnection)
' Open the connection, execute the command, and close
‘ the connection. It is more efficient to
‘ ExecuteNonQuery when ‘ data is
' not being returned.
myConnection.Open()
myCmd.ExecuteNonQuery()
MyConnection.Close()
' Data has been successfully submitted
MessageBox.Show("New Database has been ' successfully created !", _
" Database Creation Completed", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
Catch sqlExc As SqlException
MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch exc As Exception
If mstrConn = MyConnection Then
' Couldn't connect to SQL Server. Now try MSDE.
mstrConn = MY_MSDE_CONNECTION_STRING
‘ Connecting to MSDE
Else
' Unable to connect to SQL Server or MSDE
MessageBox.Show(“SQL Or MSDE Connection Failed”, _
"Connection Failed!", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End
End If
End Try
5. Creating Database Tables/Store ProcedureOnce you successfully opens the connection, next step is to create database tables, stored procedures of creating database views. ' A SqlCommand object is used to execute the SQL commands.
Dim myCmd As New SqlCommand(mySQL, myConnection)
Try
' Creating tables
cmd.CommandText = "USE MyDatabase" & vbCrLf & _
"if exists (select * from dbo.sysobjects " & _
"where id = object_id(N'[dbo].[MyTable]') " & _
" and OBJECTPROPERTY(id, N'IsUserTable') = 1)” & vbcrlf & _
“drop table [dbo].[Customer] ” & vbcrlf & “GO “ & vbcrlf & _
“CREATE TABLE [dbo].[Customer] ( " & vbcrlf & _
"[CustomerID] [int] IDENTITY (1, 1) NOT NULL ,” & vbcrlf & _
“[NameLast] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,,“ & _
vbcrlf & _
“[NameFirst] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,,“ & _
vbcrlf & _
“[NameMiddle] [varchar] (16) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,,“ &_
vbcrlf & _
“[Prefix] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, “ & _
vbcrlf & _
“[Suffix] [varchar] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, “ & _
vbcrlf & _
“[Address1] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, “ & _
vbcrlf & _
“[Address2] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL,““ & _
vbcrlf & _
“[City] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , “ & _
vbcrlf & _
“[State] [varchar] (4) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , “ & _
vbcrlf & _
“[Zip] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , “ & _
vbcrlf & _
“[Phone] [varchar] (20) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) “ & _
vbcrlf & _
“ON [PRIMARY] “ & vbcrlf & _
“GO”
' Open the connection, execute the command, and close ‘ the connection.
' It is more efficient to ExecuteNonQuery when data ‘
‘ is not being returned.
myConnection.Open()
mycmd.ExecuteNonQuery()
myConnection.close()
MessageBox.Show("Database Table ‘MyTable” & _
“successfully created.", " Creation“ & _ “Status", _
MessageBoxButtons.OK,MessageBoxIcon.Information)
Catch sqlExc As SqlException
MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
‘ Creating stored procedures
Try
cmd.CommandText = "USE MyDatabase" & vbCrLf & _
"IF EXISTS (" & _
"SELECT * " & _
"FROM MyDatabase.dbo.sysobjects " & _
"WHERE Name = ‘MyStoredProc’ " & _
"AND TYPE = 'p')" & vbCrLf & _
"BEGIN" & vbCrLf & _
"DROP PROCEDURE MyStoredProc" & vbCrLf & _
"END"
' Open the connection, execute the command, and close ‘ the connection.
' It is more efficient to ExecuteNonQuery when data ‘
‘ is not being returned.
myConnection.Open()
mycmd.ExecuteNonQuery()
myConnection.close()
MessageBox.Show("Stored Procedure ‘MyStoredProc” & _
“successfully created.", " Creation“ & _ “Status", _
MessageBoxButtons.OK,MessageBoxIcon.Information)
Catch sqlExc As SqlException
MessageBox.Show(sqlExc.ToString, "SQL Exception Error!", _
MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Requirements
SummaryFrom this article, you found that how to open a new database connection using SQL Server database. Also, it focused on creating dynamic database, tables and stored procedure in SQL Server. The usage of exception handling mechanism really helps to track the custom My next article is going to explain how it will then queried to fill a Thank You You can contact me at here.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||