导航

上海网站建设

专业的网站建设,上海网站建设中心,我们有最专业的团对与最优秀的网站设计人才,选择我们就是选择成功!

« 用MSSQLReportingServices生成报表sql语句查询结果合并unionall用法 »

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

DIV class=guanggao><SPAN id=contentAdv></SPAN></DIV><P>下面是利用SQL语句创建数据库、表、存储过程、视图、索引、规则、修改表、查看数据等的方法。所要增加的控件如下:</P>
<P align=left></P>
<TABLE style="BORDER-RIGHT: rgb(204,204,204) 1px dotted; TABLE-LAYOUT: fixed; BORDER-TOP: rgb(204,204,204) 1px dotted; BORDER-LEFT: rgb(204,204,204) 1px dotted; BORDER-BOTTOM: rgb(204,204,204) 1px dotted" cellSpacing=0 cellPadding=6 width="95%" align=center border=0>
<TBODY>
<TR>
<TD bgColor=#f3f3f3><FONT style="FONT-WEIGHT: bold; COLOR: rgb(153,0,0)">以下是引用片段:</FONT><BR>Imports System.Data <BR>Imports System.Data.SqlClient <BR><BR>Public Class Form1 <BR>  Inherits System.Windows.Forms.Form <BR>  Private ConnectionString As String = "Data Source=.;Initial Catalog=;User Id=sa;Password=;" <BR>  Private reader As SqlDataReader = Nothing <BR>  Private conn As SqlConnection = Nothing <BR>  Private cmd As SqlCommand = Nothing <BR>  Private AlterTableBtn As System.Windows.Forms.Button <BR>  Private sql As String = Nothing <BR>  Private CreateOthersBtn As System.Windows.Forms.Button <BR><BR>#Region " Windows 窗体设计器生成的代码 " <BR>  '窗体重写处置以清理组件列表。 <BR>  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean) <BR>    If disposing Then <BR>      If Not (components Is Nothing) Then <BR>        components.Dispose() <BR>      End If <BR>    End If <BR>    MyBase.Dispose(disposing) <BR>  End Sub <BR>  Public Sub New() <BR>    MyBase.New() <BR>    InitializeComponent() <BR>  End Sub <BR>  Private components As System.ComponentModel.IContainer <BR>  Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid <BR>  Friend WithEvents CreateDBBtn As System.Windows.Forms.Button <BR>  Friend WithEvents CreateTableBtn As System.Windows.Forms.Button <BR>  Friend WithEvents CreateSPBtn As System.Windows.Forms.Button <BR>  Friend WithEvents CreateViewBtn As System.Windows.Forms.Button <BR>  Friend WithEvents btnAlterTable As System.Windows.Forms.Button <BR>  Friend WithEvents btnCreateOthers As System.Windows.Forms.Button <BR>  Friend WithEvents btnDropTable As System.Windows.Forms.Button <BR>  Friend WithEvents btnViewData As System.Windows.Forms.Button <BR>  Friend WithEvents btnViewSP As System.Windows.Forms.Button <BR>  Friend WithEvents btnViewView As System.Windows.Forms.Button <BR>  <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() <BR>    Me.CreateDBBtn = New System.Windows.Forms.Button() <BR>    Me.CreateTableBtn = New System.Windows.Forms.Button() <BR>    Me.CreateSPBtn = New System.Windows.Forms.Button() <BR>    Me.CreateViewBtn = New System.Windows.Forms.Button() <BR>    Me.btnAlterTable = New System.Windows.Forms.Button() <BR>    Me.btnCreateOthers = New System.Windows.Forms.Button() <BR>    Me.btnDropTable = New System.Windows.Forms.Button() <BR>    Me.btnViewData = New System.Windows.Forms.Button() <BR>    Me.btnViewSP = New System.Windows.Forms.Button() <BR>    Me.btnViewView = New System.Windows.Forms.Button() <BR>    Me.DataGrid1 = New System.Windows.Forms.DataGrid() <BR>    CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit() <BR>    Me.SuspendLayout() <BR>    ' <BR>    'CreateDBBtn <BR>    ' <BR>    Me.CreateDBBtn.Location = New System.Drawing.Point(19, 9) <BR>    Me.CreateDBBtn.Name = "CreateDBBtn" <BR>    Me.CreateDBBtn.Size = New System.Drawing.Size(104, 23) <BR>    Me.CreateDBBtn.TabIndex = 0 <BR>    Me.CreateDBBtn.Text = "创建数据库" <BR>    ' <BR>    'CreateTableBtn <BR>    ' <BR>    Me.CreateTableBtn.Location = New System.Drawing.Point(139, 9) <BR>    Me.CreateTableBtn.Name = "CreateTableBtn" <BR>    Me.CreateTableBtn.TabIndex = 1 <BR>    Me.CreateTableBtn.Text = "创建表" <BR>    ' <BR>    'CreateSPBtn <BR>    ' <BR>    Me.CreateSPBtn.Location = New System.Drawing.Point(230, 9) <BR>    Me.CreateSPBtn.Name = "CreateSPBtn" <BR>    Me.CreateSPBtn.Size = New System.Drawing.Size(104, 23) <BR>    Me.CreateSPBtn.TabIndex = 2 <BR>    Me.CreateSPBtn.Text = "创建存储过程" <BR>    ' <BR>    'CreateViewBtn <BR>    ' <BR>    Me.CreateViewBtn.Location = New System.Drawing.Point(350, 9) <BR>    Me.CreateViewBtn.Name = "CreateViewBtn" <BR>    Me.CreateViewBtn.TabIndex = 3 <BR>    Me.CreateViewBtn.Text = "创建视图" <BR>    ' <BR>    'btnAlterTable <BR>    ' <BR>    Me.btnAlterTable.Location = New System.Drawing.Point(441, 9) <BR>    Me.btnAlterTable.Name = "btnAlterTable" <BR>    Me.btnAlterTable.TabIndex = 4 <BR>    Me.btnAlterTable.Text = "修改表" <BR>    ' <BR>    'btnCreateOthers <BR>    ' <BR>    Me.btnCreateOthers.Location = New System.Drawing.Point(17, 43) <BR>    Me.btnCreateOthers.Name = "btnCreateOthers" <BR>    Me.btnCreateOthers.Size = New System.Drawing.Size(104, 23) <BR>    Me.btnCreateOthers.TabIndex = 5 <BR>    Me.btnCreateOthers.Text = "创建规则和索引" <BR>    ' <BR>    'btnDropTable <BR>    ' <BR>    Me.btnDropTable.Location = New System.Drawing.Point(138, 43) <BR>    Me.btnDropTable.Name = "btnDropTable" <BR>    Me.btnDropTable.TabIndex = 6 <BR>    Me.btnDropTable.Text = "删除表" <BR>    ' <BR>    'btnViewData <BR>    ' <BR>    Me.btnViewData.Location = New System.Drawing.Point(351, 43) <BR>    Me.btnViewData.Name = "btnViewData" <BR>    Me.btnViewData.TabIndex = 7 <BR>    Me.btnViewData.Text = "查看数据" <BR>    ' <BR>    'btnViewSP <BR>    ' <BR>    Me.btnViewSP.Location = New System.Drawing.Point(230, 43) <BR>    Me.btnViewSP.Name = "btnViewSP" <BR>    Me.btnViewSP.Size = New System.Drawing.Size(104, 23) <BR>    Me.btnViewSP.TabIndex = 8 <BR>    Me.btnViewSP.Text = "查看存储过程" <BR>    ' <BR>    'btnViewView <BR>    ' <BR>    Me.btnViewView.Location = New System.Drawing.Point(443, 43) <BR>    Me.btnViewView.Name = "btnViewView" <BR>    Me.btnViewView.TabIndex = 9 <BR>    Me.btnViewView.Text = "查看视图" <BR>    ' <BR>    'DataGrid1 <BR>    ' <BR>    Me.DataGrid1.DataMember = "" <BR>    Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText <BR>    Me.DataGrid1.Location = New System.Drawing.Point(20, 76) <BR>    Me.DataGrid1.Name = "DataGrid1" <BR>    Me.DataGrid1.Size = New System.Drawing.Size(500, 183) <BR>    Me.DataGrid1.TabIndex = 10 <BR>    ' <BR>    'Form1 <BR>    ' <BR>    Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) <BR>    Me.ClientSize = New System.Drawing.Size(538, 281) <BR>    Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.DataGrid1, Me.btnViewView, _ <BR>              Me.btnViewSP, Me.btnViewData, Me.btnDropTable, Me.btnCreateOthers, Me.btnAlterTable, _ <BR>              Me.CreateViewBtn, Me.CreateSPBtn, Me.CreateTableBtn, Me.CreateDBBtn}) <BR>    Me.Name = "Form1" <BR>    Me.Text = "动态创建SQL Server数据库、表、存储过程等架构信息" <BR>    CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit() <BR>    Me.ResumeLayout(False) <BR><BR>  End Sub <BR><BR>#End Region <BR><BR>  ' 创建数据库 <BR>  Private Sub CreateDBBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles CreateDBBtn.Click <BR>    conn = New SqlConnection(ConnectionString) <BR>    ' 打开连接 <BR>    If conn.State <> ConnectionState.Open Then <BR>      conn.Open() <BR>    End If <BR>    'MyDataBase为数据库名称 <BR>    Dim sql As String = "CREATE DATABASE MyDataBase ON PRIMARY (Name=MyDataBase_data, filename = " + _ <BR>        "'D:\MyDataBase.mdf', size=3," + "maxsize=5, filegrowth=10%) log on" + "(name=MyDataBase_log, " + _ <BR>        "filename='D:\MyDataBase.ldf',size=3," + "maxsize=20,filegrowth=1)" <BR>    cmd = New SqlCommand(sql, conn) <BR>    Try <BR>      cmd.ExecuteNonQuery() <BR>    Catch ae As SqlException <BR>      MessageBox.Show(ae.Message.ToString()) <BR>    End Try <BR>  End Sub <BR>  '创建表 <BR>  Private Sub CreateTableBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles CreateTableBtn.Click <BR>    conn = New SqlConnection(ConnectionString) <BR>    ' 打开连接 <BR>    If conn.State = ConnectionState.Open Then <BR>      conn.Close() <BR>    End If <BR>    ConnectionString = "Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;" <BR>    conn.ConnectionString = ConnectionString <BR>    conn.Open() <BR>    sql = "CREATE TABLE myTable" + "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," + _ <BR>      "myName CHAR(50) NOT Null, myAddress CHAR(255), myValues FLOAT)" <BR>    cmd = New SqlCommand(sql, conn) <BR>    Try <BR>      cmd.ExecuteNonQuery() <BR>      ' 添加纪录 <BR>      sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _ <BR>        "VALUES (1001, _'【孟宪会之精彩世界】之一', 'http://xml.sz.luohuedu.net/', 100 ) " <BR>      cmd = New SqlCommand(sql, conn) <BR>      cmd.ExecuteNonQuery() <BR>      sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _ <BR>        "VALUES (1002, '【孟宪会之精彩世界】之二', 'http://www.erp800.com/net_lover/', 99) " <BR>      cmd = New SqlCommand(sql, conn) <BR>      cmd.ExecuteNonQuery() <BR>      sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _ <BR>        "VALUES (1003, '【孟宪会之精彩世界】之三', 'http://xml.sz.luohuedu.net/', 99) " <BR>      cmd = New SqlCommand(sql, conn) <BR>      cmd.ExecuteNonQuery() <BR>      sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _ <BR>        "VALUES (1004, '【孟宪会之精彩世界】之四', 'http://www.erp800.com/net_lover/', 100) " <BR>      cmd = New SqlCommand(sql, conn) <BR>      cmd.ExecuteNonQuery() <BR>    Catch ae As SqlException <BR>      MessageBox.Show(ae.Message.ToString()) <BR>    End Try <BR><BR>  End Sub <BR>  '创建存储过程 <BR>  Private Sub CreateSPBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles CreateSPBtn.Click <BR>    sql = "CREATE PROCEDURE myProc AS" + " SELECT myName, myAddress FROM myTable GO" <BR>    ExecuteSQLStmt(sql) <BR>  End Sub <BR>  '创建视图 <BR>  Private Sub CreateViewBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles CreateViewBtn.Click <BR>    sql = "CREATE VIEW myView AS SELECT myName FROM myTable" <BR>    ExecuteSQLStmt(sql) <BR><BR>  End Sub <BR>  '修改表 <BR>  Private Sub btnAlterTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles btnAlterTable.Click <BR>    sql = "ALTER TABLE MyTable ADD newCol datetime NOT NULL DEFAULT (getdate())" <BR>    ExecuteSQLStmt(sql) <BR>  End Sub <BR>  '创建规则和索引 <BR>  Private Sub btnCreateOthers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles btnCreateOthers.Click <BR>    sql = "CREATE UNIQUE INDEX " + "myIdx ON myTable(myName)" <BR>    ExecuteSQLStmt(sql) <BR><BR>    sql = "CREATE RULE myRule " + "AS @myValues >= 90 AND @myValues < 9999" <BR>    ExecuteSQLStmt(sql) <BR>  End Sub <BR><BR>  '删除表 <BR>  Private Sub btnDropTable_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles btnDropTable.Click <BR>    Dim sql As String = "DROP TABLE MyTable" <BR>    ExecuteSQLStmt(sql) <BR>  End Sub <BR>  '浏览表数据 <BR>  Private Sub btnViewData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles btnViewData.Click <BR>    conn = New SqlConnection(ConnectionString) <BR>    If conn.State = ConnectionState.Open Then <BR>      conn.Close() <BR>    End If <BR>    ConnectionString = "Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;" <BR>    conn.ConnectionString = ConnectionString <BR>    conn.Open() <BR>    Dim da As New SqlDataAdapter("SELECT * FROM myTable", conn) <BR>    Dim ds As New DataSet("myTable") <BR>    da.Fill(ds, "myTable") <BR>    DataGrid1.DataSource = ds.Tables("myTable").DefaultView <BR>  End Sub <BR>  '浏览存储过程 <BR>  Private Sub btnViewSP_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles btnViewSP.Click <BR>    conn = New SqlConnection(ConnectionString) <BR>    If conn.State = ConnectionState.Open Then <BR>      conn.Close() <BR>    End If <BR>    ConnectionString = "Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;" <BR>    conn.ConnectionString = ConnectionString <BR>    conn.Open() <BR>    Dim da As New SqlDataAdapter("myProc", conn) <BR>    Dim ds As New DataSet("SP") <BR>    da.Fill(ds, "SP") <BR>    DataGrid1.DataSource = ds.DefaultViewManager <BR>  End Sub <BR>  '浏览视图 <BR>  Private Sub btnViewView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _ <BR>    Handles btnViewView.Click <BR>    conn = New SqlConnection(ConnectionString) <BR>    If conn.State = ConnectionState.Open Then <BR>      conn.Close() <BR>    End If <BR>    ConnectionString = "Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;" <BR>    conn.ConnectionString = ConnectionString <BR>    conn.Open() <BR>    Dim da As New SqlDataAdapter("SELECT * FROM myView", conn) <BR>    Dim ds As New DataSet() <BR>    da.Fill(ds) <BR>    DataGrid1.DataSource = ds.DefaultViewManager <BR>  End Sub <BR><BR>  Private Sub ExecuteSQLStmt(ByVal sql As String) <BR>    conn = New SqlConnection(ConnectionString) <BR>    ' 打开连接 <BR>    If conn.State = ConnectionState.Open Then <BR>      conn.Close() <BR>    End If <BR>    ConnectionString = "Data Source=.;Initial Catalog=MyDataBase;User Id=sa;Password=;" <BR>    conn.ConnectionString = ConnectionString <BR>    conn.Open() <BR>    cmd = New SqlCommand(sql, conn) <BR>    Try <BR>      cmd.ExecuteNonQuery() <BR>    Catch ae As SqlException <BR>      MessageBox.Show(ae.Message.ToString()) <BR>    End Try <BR>  End Sub <BR>End Class</TD></TR></TBODY></TABLE><script language='javascript' src='http://www.iTbulo.com/gg/200703/79.js'></script></td>
</tr>
</table><!--c-pip文章正文中间240*200--><SPAN id=span_ad3>
<SCRIPT language=JavaScript>
document.write("<script src=\"http://www.iTbulo.com/gg/200703/78.js\"><\/script>");</SCRIPT>
</SPAN>
<SCRIPT>
document.getElementById("contentAdv").innerHTML=document.getElementById("span_ad3").innerHTML;
document.getElementById("span_ad3").innerHTML="";
</SCRIPT>
</td>
<td width="1" valign="top" bgcolor="#A8A8A8"></td>
<td width="173" valign="top" bgcolor="F3F3F3">
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td height="21" align="center" valign="middle" bgcolor="DFDFDF"><strong>文章搜索</strong></td>
</tr>
<tr>
<td height="2" bgcolor="A3A3A3"></td>
</tr>
<form method="Get" name="SearchForm" action="/Search.asp" target="_blank"><TR>
<TD height="40" align=center><input name="Field" type="hidden" id="Field" value="Title"><INPUT name=Keyword id="Keyword" size=16> <INPUT type=submit value=搜索 name=submit onClick="if(this.form.Keyword.value=='') {alert('请输入关键字');return false;}"></TD></TR></FORM>
<tr>
<td height="21" align="center" valign="middle" bgcolor="DFDFDF"><strong>

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

Powered By Z-Blog .Templete from Google黑板报

Copyright 上海网站建设 www.shwebc.com. Some Rights Reserved.