前事不忘,后事之师,不忘国耻!

 注册  找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2066|回复: 0

动态创建MSSQL数据库表存储过程

[复制链接]

动态创建MSSQL数据库表存储过程

[复制链接]
ehxz

主题

0

回帖

7159

积分

管理员

积分
7159
2007-6-21 16:36:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册

×
  下面是利用SQL语句创建数据库、表、存储过程、视图、索引、规则、修改表、查看数据等的方法。所要增加的控件如下:
以下是引用片段:
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 " Windows 窗体设计器生成的代码 "
'窗体重写处置以清理组件列表。
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()
  &#39;
  &#39;CreateDBBtn
  &#39;
  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 = "创建数据库"
  &#39;
  &#39;CreateTableBtn
  &#39;
  Me.CreateTableBtn.Location = New System.Drawing.Point(139, 9)
  Me.CreateTableBtn.Name = "CreateTableBtn"
  Me.CreateTableBtn.TabIndex = 1
  Me.CreateTableBtn.Text = "创建表"
  &#39;
  &#39;CreateSPBtn
  &#39;
  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 = "创建存储过程"
  &#39;
  &#39;CreateViewBtn
  &#39;
  Me.CreateViewBtn.Location = New System.Drawing.Point(350, 9)
  Me.CreateViewBtn.Name = "CreateViewBtn"
  Me.CreateViewBtn.TabIndex = 3
  Me.CreateViewBtn.Text = "创建视图"
  &#39;
  &#39;btnAlterTable
  &#39;
  Me.btnAlterTable.Location = New System.Drawing.Point(441, 9)
  Me.btnAlterTable.Name = "btnAlterTable"
  Me.btnAlterTable.TabIndex = 4
  Me.btnAlterTable.Text = "修改表"
  &#39;
  &#39;btnCreateOthers
  &#39;
  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 = "创建规则和索引"
  &#39;
  &#39;btnDropTable
  &#39;
  Me.btnDropTable.Location = New System.Drawing.Point(138, 43)
  Me.btnDropTable.Name = "btnDropTable"
  Me.btnDropTable.TabIndex = 6
  Me.btnDropTable.Text = "删除表"
  &#39;
  &#39;btnViewData
  &#39;
  Me.btnViewData.Location = New System.Drawing.Point(351, 43)
  Me.btnViewData.Name = "btnViewData"
  Me.btnViewData.TabIndex = 7
  Me.btnViewData.Text = "查看数据"
  &#39;
  &#39;btnViewSP
  &#39;
  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 = "查看存储过程"
  &#39;
  &#39;btnViewView
  &#39;
  Me.btnViewView.Location = New System.Drawing.Point(443, 43)
  Me.btnViewView.Name = "btnViewView"
  Me.btnViewView.TabIndex = 9
  Me.btnViewView.Text = "查看视图"
  &#39;
  &#39;DataGrid1
  &#39;
  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
  &#39;
  &#39;Form1
  &#39;
  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 = "动态创建SQL Server数据库、表、存储过程等架构信息"
  CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
  Me.ResumeLayout(False)

End Sub

#End Region

&#39; 创建数据库
Private Sub CreateDBBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles CreateDBBtn.Click
  conn = New SqlConnection(ConnectionString)
  &#39; 打开连接
  If conn.State <> ConnectionState.Open Then
   conn.Open()
  End If
  &#39;MyDataBase为数据库名称
  Dim sql As String = "CREATE DATABASE MyDataBase ON PRIMARY (Name=MyDataBase_data, filename = " + _
    "&#39;D:\MyDataBase.mdf&#39;, size=3," + "maxsize=5, filegrowth=10%) log on" + "(name=MyDataBase_log, " + _
    "filename=&#39;D:\MyDataBase.ldf&#39;,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
&#39;创建表
Private Sub CreateTableBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles CreateTableBtn.Click
  conn = New SqlConnection(ConnectionString)
  &#39; 打开连接
  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()
   &#39; 添加纪录
   sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _
    "VALUES (1001, _&#39;【孟宪会之精彩世界】之一&#39;, &#39;http://xml.sz.luohuedu.net/&#39;, 100 ) "
   cmd = New SqlCommand(sql, conn)
   cmd.ExecuteNonQuery()
   sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _
    "VALUES (1002, &#39;【孟宪会之精彩世界】之二&#39;, &#39;http://www.erp800.com/net_lover/&#39;, 99) "
   cmd = New SqlCommand(sql, conn)
   cmd.ExecuteNonQuery()
   sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _
    "VALUES (1003, &#39;【孟宪会之精彩世界】之三&#39;, &#39;http://xml.sz.luohuedu.net/&#39;, 99) "
   cmd = New SqlCommand(sql, conn)
   cmd.ExecuteNonQuery()
   sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _
    "VALUES (1004, &#39;【孟宪会之精彩世界】之四&#39;, &#39;http://www.erp800.com/net_lover/&#39;, 100) "
   cmd = New SqlCommand(sql, conn)
   cmd.ExecuteNonQuery()
  Catch ae As SqlException
   MessageBox.Show(ae.Message.ToString())
  End Try

End Sub
&#39;创建存储过程
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
&#39;创建视图
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
&#39;修改表
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
&#39;创建规则和索引
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

&#39;删除表
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
&#39;浏览表数据
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
&#39;浏览存储过程
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
&#39;浏览视图
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)
  &#39; 打开连接
  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

免责申明1、欢迎访问本站,本文内容及相关资源来源于网络,版权归版权方所有!本站原创内容版权归本站所有,请勿转载!
2、本文内容仅代表作者观点,不代表本站立场,作者自负,本站资源仅供学习研究,请勿非法使用,否则后果自负!请下载后24小时内删除!
3、本文内容,包括但不限于源码、文字、图片等,仅供参考。本站不对其安全性,正确性等作出保证。但本站会尽量审核会员发表的内容。
4、如本帖侵犯到任何版权问题,请立即告知本站 ,本站将及时删除并致以最深的歉意!客服邮箱:admin@dbabbs.com
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|Archiver|小黑屋|DBA论坛中国 ( 鲁ICP备20017503号-2 )

GMT+8, 2024-5-10 01:30 , Processed in 0.061157 second(s), 10 queries , MemCached On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表