server| Create | stored Procedure | dynamic | data | database
The following are ways to create databases, tables, stored procedures, views, indexes, rules, modify tables, view data, and so on, using SQL statements. The controls you want to add are as follows:
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Inherits System.Windows.Forms.Form
Private ConnectionString as String = "Data source=.;i Nitial 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 the Windows forms Designer"
' Form overrides disposition to clean up the component list.
Protected Overloads Overrides Sub Dispose (ByVal disposing as Boolean)
If disposing Then
If not (components are 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 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 Procedures"
'
' Btnviewview
'
Me.btnViewView.Location = New System.Drawing.Point (443, 43)
Me.btnViewView.Name = "Btnviewview"
Me.btnViewView.TabIndex = 9
Me.btnViewView.Text = "View View"
'
' DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point (20, 76)
Me.DataGrid1.Name = "DataGrid1"
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 creating schema information such as SQL Server databases, tables, 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 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 connection
If Conn. State = ConnectionState.Open Then
Conn. Close ()
End If
ConnectionString = "Data source=.;i Nitial catalog=mydatabase; User Id=sa; password=; "
Conn. ConnectionString = ConnectionString
Conn. Open ()
sql = "CREATE TABLE myTable" + "(myId INTEGER CONSTRAINT Pkeymyid PRIMARY KEY," + _
"MyName char () 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, _," one of Mengxian's Wonderful Worlds ", ' http://xml.sz.luohuedu.net/', 100)"
cmd = New SqlCommand (SQL, conn)
Cmd. ExecuteNonQuery ()
sql = "INSERT into myTable (MyId, MyName, myAddress, myvalues)" + _
"VALUES (1002, '" Mengxian The Wonderful World "bis", ' http://www.erp800.com/net_lover/', 99) "
cmd = New SqlCommand (SQL, conn)
Cmd. ExecuteNonQuery ()
sql = "INSERT into myTable (MyId, MyName, myAddress, myvalues)" + _
"VALUES (1003, '" Mengxian of the Wonderful World "III", ' http://xml.sz.luohuedu.net/', 99) "
cmd = New SqlCommand (SQL, conn)
Cmd. ExecuteNonQuery ()
sql = "INSERT into myTable (MyId, MyName, myAddress, myvalues)" + _
"VALUES (1004, '" Mengxian the Wonderful World of four ", ' 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 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 rules and indexes
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 >= and @myValues < 9999"
EXECUTESQLSTMT (SQL)
End Sub
' Delete 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=.;i Nitial 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
' Browse 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=.;i Nitial 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
' Browse 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=.;i Nitial 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 connection
If Conn. State = ConnectionState.Open Then
Conn. Close ()
End If
ConnectionString = "Data source=.;i Nitial 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