jin_geping 发表于 2006-7-30 10:32

[分享]下面是利用SQL语句创建数据库、表、存储过程、视图、索引、规则、修改表、查

<P>下面是利用SQL语句创建数据库、表、存储过程、视图、索引、规则、修改表、查看数据等的方法。所要增加的控件如下:</P>
<P><BR>Imports System.Data<BR>Imports System.Data.SqlClient</P>
<P>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</P>
<P>#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>  &lt;System.Diagnostics.DebuggerStepThrough()&gt; 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)</P>
<P>  End Sub</P>
<P>#End Region</P>
<P>  ' 创建数据库<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 &lt;&gt; 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</P>
<P>  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)</P>
<P>  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)</P>
<P>    sql = "CREATE RULE myRule " + "AS @myValues &gt;= 90 AND @myValues &lt; 9999"<BR>    ExecuteSQLStmt(sql)<BR>  End Sub</P>
<P>  '删除表<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</P>
<P>  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</P>
<P> <BR> <BR>[1楼] 作者:vga 发表时间:2005/07/19 07:11 回复  修改  删除  来源   <BR>CREATE PROCEDURE Get_Image_ID(@PicTableName char(15), @Package_NO int,@TyperID int,<BR>@Typer_Order int,@Pic_ID int output)</P>

<P>AS</P>

<P>begin<BR>    declare @str varchar(1000)<BR>    declare @ConditionStr varchar(500)<BR>    declare @AssignStr varchar(500)<BR>    declare @typerField char(10)</P>

<P>    if @Typer_Order = 1 <BR>    begin<BR>       set @AssignStr = '[工作状态]=1, [一打员]=@TyperID'<BR>       set @ConditionStr = 'where ([工作状态]=0) and ([一打员] is null) and ([包号<a href="mailto:=@Package_NO])'" target="_blank" >=@Package_NO])'</A><BR>    end<BR>    else<BR>    begin<BR>       set @AssignStr = ' [工作状态]=3, [二打员]=@TyperID'<BR>       set @ConditionStr = ' where  ([工作状态]=2)  and  ([二打员] is null) and ([包号<a href="mailto:=@Package_NO])'" target="_blank" >=@Package_NO])'</A><BR>   end</P>

<P>    set @str = 'update  [' +  @PicTableName + '] set ' + @AssignStr + ' ' + @ConditionStr<BR>    <BR>    exec (@str)</P>

<P>    if  @@rowcount = 1 -- 成功分配图像<BR>    begin<BR>    end <BR>    else               -- 分配图像失败   <BR>    begin<BR>    end<BR>end</P>

<P>go<BR> <BR> <BR></P>


页: [1]

编程论坛