Dynamically create SQL Server databases, tables, and stored procedures

Source: Internet
Author: User
Tags how to use sql

The following describes how to use SQL statements to create databases, tables, stored procedures, views, indexes, rules, modify tables, and view data. The following controls are added:

Imports system. Data
Imports system. Data. sqlclient

Public class form1
Inherits system. Windows. Forms. Form
Private connectionstring as string = "Data Source =.; initial catalog =; user id = sa; Password = ;"
Private reader as sqldatareader = nothing
Private conn as sqlconnection = nothing
Private cmd as sqlcommand = nothing
Private altertablebtn as system. Windows. Forms. Button
Private SQL as string = nothing
Private createothersbtn as system. Windows. Forms. Button

# Region "code generated by Windows Form Designer"
'Form rewriting disposal to clear the component list.
Protected overloads overrides sub dispose (byval disposing as Boolean)
If disposing then
If not (components is nothing) then
Components. Dispose ()
End if
End if
Mybase. Dispose (disposing)
End sub
Public sub new ()
Mybase. New ()
Initializecomponent ()
End sub
Private components as system. componentmodel. icontainer
Friend withevents datagrid1 as system. Windows. Forms. DataGrid
Friend withevents createdbbtn as system. Windows. Forms. Button
Friend withevents createtablebtn as system. Windows. Forms. Button
Friend withevents createspbtn as system. Windows. Forms. Button
Friend withevents createviewbtn as system. Windows. Forms. Button
Friend withevents btnaltertable as system. Windows. Forms. Button
Friend withevents btncreateothers as system. Windows. Forms. Button
Friend withevents btndroptable as system. Windows. Forms. Button
Friend withevents btnviewdata as system. Windows. Forms. Button
Friend withevents btnviewsp as system. Windows. Forms. Button
Friend withevents btnviewview as system. Windows. Forms. Button
<System. Diagnostics. debuggerstepthrough ()> private sub initializecomponent ()
Me. createdbbtn = new system. Windows. Forms. Button ()
Me. createtablebtn = new system. Windows. Forms. Button ()
Me. createspbtn = new system. Windows. Forms. Button ()
Me. createviewbtn = new system. Windows. Forms. Button ()
Me. btnaltertable = new system. Windows. Forms. Button ()
Me. btncreateothers = new system. Windows. Forms. Button ()
Me. btndroptable = new system. Windows. Forms. Button ()
Me. btnviewdata = new system. Windows. Forms. Button ()
Me. btnviewsp = new system. Windows. Forms. Button ()
Me. btnviewview = new system. Windows. Forms. Button ()
Me. datagrid1 = new system. Windows. Forms. DataGrid ()
Ctype (Me. datagrid1, system. componentmodel. isupportinitialize). begininit ()
Me. suspendlayout ()
'
'Createdbbtn
'
Me. createdbbtn. Location = new system. Drawing. Point (19, 9)
Me. createdbbtn. Name = "createdbbtn"
Me. createdbbtn. size = new system. Drawing. Size (104, 23)
Me. createdbbtn. tabindex = 0
Me. createdbbtn. Text = "create database"
'
'Createtablebtn
'
Me. createtablebtn. Location = new system. Drawing. Point (139, 9)
Me. createtablebtn. Name = "createtablebtn"
Me. createtablebtn. tabindex = 1
Me. createtablebtn. Text = "create table"
'
'Createspbtn
'
Me. createspbtn. Location = new system. Drawing. Point (230, 9)
Me. createspbtn. Name = "createspbtn"
Me. createspbtn. size = new system. Drawing. Size (104, 23)
Me. createspbtn. tabindex = 2
Me. createspbtn. Text = "Create a stored procedure"
'
'Createviewbtn
'
Me. createviewbtn. Location = new system. Drawing. Point (350, 9)
Me. createviewbtn. Name = "createviewbtn"
Me. createviewbtn. tabindex = 3
Me. createviewbtn. Text = "create View"
'
'Btnaltertable
'
Me. btnaltertable. Location = new system. Drawing. Point (441, 9)
Me. btnaltertable. Name = "btnaltertable"
Me. btnaltertable. tabindex = 4
Me. btnaltertable. Text = "Modify Table"
'
'Btncreateothers
'
Me. btncreateothers. Location = new system. Drawing. Point (17, 43)
Me. btncreateothers. Name = "btncreateothers"
Me. btncreateothers. size = new system. Drawing. Size (104, 23)
Me. btncreateothers. tabindex = 5
Me. btncreateothers. Text = "create rules and indexes"
'
'Btndroptable
'
Me. btndroptable. Location = new system. Drawing. Point (138, 43)
Me. btndroptable. Name = "btndroptable"
Me. btndroptable. tabindex = 6
Me. btndroptable. Text = "Delete table"
'
'Btnviewdata
'
Me. btnviewdata. Location = new system. Drawing. Point (351, 43)
Me. btnviewdata. Name = "btnviewdata"
Me. btnviewdata. tabindex = 7
Me. btnviewdata. Text = "View data"
'
'Btnviewsp
'
Me. btnviewsp. Location = new system. Drawing. Point (230, 43)
Me. btnviewsp. Name = "btnviewsp"
Me. btnviewsp. size = new system. Drawing. Size (104, 23)
Me. btnviewsp. tabindex = 8
Me. btnviewsp. Text = "view stored procedure"
'
'Btnviewview
'
Me. btnviewview. Location = new system. Drawing. Point (443, 43)
Me. btnviewview. Name = "btnviewview"
Me. btnviewview. tabindex = 9
Me. btnviewview. Text = "view"
'
'Create1'
'
Me. datagrid1.datamember = ""
Me. datagrid1.headerforecolor = system. Drawing. systemcolors. controltext
Me. datagrid1.location = new system. Drawing. Point (20, 76)
Me. Maid = "maid"
Me. datagrid1.size = new system. Drawing. Size (500,183)
Me. datagrid1.tabindex = 10
'
'Form1
'
Me. autoscalebasesize = new system. Drawing. Size (5, 13)
Me. clientsize = new system. Drawing. Size (538,281)
Me. Controls. addrange (new system. Windows. Forms. Control () {me. datagrid1, me. btnviewview ,_
Me. btnviewsp, me. btnviewdata, me. btndroptable, me. btncreateothers, me. btnaltertable ,_
Me. createviewbtn, me. createspbtn, me. createtablebtn, me. createdbbtn })
Me. Name = "form1"
Me. Text = "dynamically create architecture information such as SQL Server databases, tables, and stored procedures"
Ctype (Me. datagrid1, system. componentmodel. isupportinitialize). endinit ()
Me. resumelayout (false)

End sub

# End Region

'Create a database
Private sub createdbbtn_click (byval sender as system. Object, byval e as system. eventargs )_
Handles createdbbtn. Click
Conn = new sqlconnection (connectionstring)
'Open the connection
If conn. State <> connectionstate. Open then
Conn. open ()
End if
'Mydatabase is the database name
Dim SQL as string = "CREATE DATABASE mydatabase on primary (name = mydatabase_data, filename =" + _
"'D:/mydatabase. MDF ', size = 3," + "maxsize = 5, filegrowth = 10%) log on" + "(name = mydatabase_log," + _
"Filename = 'd:/mydatabase. ldf', size = 3," + "maxsize = 20, filegrowth = 1 )"
Cmd = new sqlcommand (SQL, Conn)
Try
Cmd. executenonquery ()
Catch AE as sqlexception
MessageBox. Show (AE. Message. tostring ())
End try
End sub
'Create a table
Private sub createtablebtn_click (byval sender as system. Object, byval e as system. eventargs )_
Handles createtablebtn. Click
Conn = new sqlconnection (connectionstring)
'Open the connection
If conn. State = connectionstate. Open then
Conn. Close ()
End if
Connectionstring = "Data Source =.; initial catalog = mydatabase; user id = sa; Password = ;"
Conn. connectionstring = connectionstring
Conn. open ()
SQL = "CREATE TABLE mytable" + "(myid integer constraint pkeymyid primary key," + _
"Myname char (50) not null, myaddress char (255), myvalues float )"
Cmd = new sqlcommand (SQL, Conn)
Try
Cmd. executenonquery ()
'Add record
SQL = "insert into mytable (myid, myname, myaddress, myvalues)" + _
"Values (1001, _ '[wonderful world of the Meng xianhui Conference]', 'HTTP: // xml.sz.luohuedu.net/', 100 )"
Cmd = new sqlcommand (SQL, Conn)
Cmd. executenonquery ()
SQL = "insert into mytable (myid, myname, myaddress, myvalues)" + _
"Values (1002, '', 'HTTP: // www.erp800.com/net_lover/', 99 )"
Cmd = new sqlcommand (SQL, Conn)
Cmd. executenonquery ()
SQL = "insert into mytable (myid, myname, myaddress, myvalues)" + _
"Values (1003, 'the wonderful world of the Meng xianhui ', 'HTTP: // xml.sz.luohuedu.net/', 99 )"
Cmd = new sqlcommand (SQL, Conn)
Cmd. executenonquery ()
SQL = "insert into mytable (myid, myname, myaddress, myvalues)" + _
"Values (1004, '4th of the wonderful world of the Meng xianhui ', 'HTTP: // www.erp800.com/net_lover/', 100 )"
Cmd = new sqlcommand (SQL, Conn)
Cmd. executenonquery ()
Catch AE as sqlexception
MessageBox. Show (AE. Message. tostring ())
End try

End sub
'Create a stored procedure
Private sub createspbtn_click (byval sender as system. Object, byval e as system. eventargs )_
Handles createspbtn. Click
SQL = "create procedure myproc as" + "select myname, myaddress from mytable go"
Executesqlstmt (SQL)
End sub
'Create a view
Private sub createviewbtn_click (byval sender as system. Object, byval e as system. eventargs )_
Handles createviewbtn. Click
SQL = "create view myview as select myname from mytable"
Executesqlstmt (SQL)

End sub
'Modify the table
Private sub btnaltertable_click (byval sender as system. Object, byval e as system. eventargs )_
Handles btnaltertable. Click
SQL = "ALTER TABLE mytable add newcol datetime not null default (getdate ())"
Executesqlstmt (SQL)
End sub
'Create a rule and an index
Private sub btncreateothers_click (byval sender as system. Object, byval e as system. eventargs )_
Handles btncreateothers. Click
SQL = "create unique index" + "myidx on mytable (myname )"
Executesqlstmt (SQL)

SQL = "create rule myrule" + "as @ myvalues> = 90 and @ myvalues <9999"
Executesqlstmt (SQL)
End sub

'Delete a table
Private sub btndroptable_click (byval sender as system. Object, byval e as system. eventargs )_
Handles btndroptable. Click
Dim SQL as string = "Drop table mytable"
Executesqlstmt (SQL)
End sub
'Browse table data
Private sub btnviewdata_click (byval sender as system. Object, byval e as system. eventargs )_
Handles btnviewdata. Click
Conn = new sqlconnection (connectionstring)
If conn. State = connectionstate. Open then
Conn. Close ()
End if
Connectionstring = "Data Source =.; initial catalog = mydatabase; user id = sa; Password = ;"
Conn. connectionstring = connectionstring
Conn. open ()
Dim da as new sqldataadapter ("select * From mytable", Conn)
Dim ds as new dataset ("mytable ")
Da. Fill (DS, "mytable ")
Datagrid1.datasource = Ds. Tables ("mytable"). defaultview
End sub
'Browsing stored procedures
Private sub btnviewsp_click (byval sender as system. Object, byval e as system. eventargs )_
Handles btnviewsp. Click
Conn = new sqlconnection (connectionstring)
If conn. State = connectionstate. Open then
Conn. Close ()
End if
Connectionstring = "Data Source =.; initial catalog = mydatabase; user id = sa; Password = ;"
Conn. connectionstring = connectionstring
Conn. open ()
Dim da as new sqldataadapter ("myproc", Conn)
Dim ds as new dataset ("SP ")
Da. Fill (DS, "SP ")
Datagrid1.datasource = Ds. defaultviewmanager
End sub
'Browsing view'
Private sub btnviewview_click (byval sender as system. Object, byval e as system. eventargs )_
Handles btnviewview. Click
Conn = new sqlconnection (connectionstring)
If conn. State = connectionstate. Open then
Conn. Close ()
End if
Connectionstring = "Data Source =.; initial catalog = mydatabase; user id = sa; Password = ;"
Conn. connectionstring = connectionstring
Conn. open ()
Dim da as new sqldataadapter ("select * From myview", Conn)
Dim ds as new dataset ()
Da. Fill (DS)
Datagrid1.datasource = Ds. defaultviewmanager
End sub

Private sub executesqlstmt (byval SQL as string)
Conn = new sqlconnection (connectionstring)
'Open the connection
If conn. State = connectionstate. Open then
Conn. Close ()
End if
Connectionstring = "Data Source =.; initial catalog = mydatabase; user id = sa; Password = ;"
Conn. connectionstring = connectionstring
Conn. open ()
Cmd = new sqlcommand (SQL, Conn)
Try
Cmd. executenonquery ()
Catch AE as sqlexception
MessageBox. Show (AE. Message. tostring ())
End try
End sub
End Class

From Mencius Chapter E column Co., http://blog.csdn.net/net_lover/archive/2003/08/18/6942.aspx.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.