创建动态MSSQL数据库表

论坛 期权论坛 脚本     
niminba   2021-5-23 04:19   1506   0
以下是引用片段:
复制代码 代码如下:

  ImportsSystem.Data
  ImportsSystem.Data.SqlClient
  PublicClassForm1
  InheritsSystem.Windows.Forms.Form
  PrivateConnectionStringAsString="DataSource=.;InitialCatalog=;UserId=sa;Password=;"
  PrivatereaderAsSqlDataReader=Nothing
  PrivateconnAsSqlConnection=Nothing
  PrivatecmdAsSqlCommand=Nothing
  PrivateAlterTableBtnAsSystem.Windows.Forms.Button
  PrivatesqlAsString=Nothing
  PrivateCreateOthersBtnAsSystem.Windows.Forms.Button
  #Region"Windows窗体设计器生成的代码"
  '窗体重写处置以清理组件列表。
  ProtectedOverloadsOverridesSubDispose(ByValdisposingAsBoolean)
  IfdisposingThen
  IfNot(componentsIsNothing)Then
  components.Dispose()
  EndIf
  EndIf
  MyBase.Dispose(disposing)
  EndSub
  PublicSubNew()
  MyBase.New()
  InitializeComponent()
  EndSub
  PrivatecomponentsAsSystem.ComponentModel.IContainer
  FriendWithEventsDataGrid1AsSystem.Windows.Forms.DataGrid
  FriendWithEventsCreateDBBtnAsSystem.Windows.Forms.Button
  FriendWithEventsCreateTableBtnAsSystem.Windows.Forms.Button
  FriendWithEventsCreateSPBtnAsSystem.Windows.Forms.Button
  FriendWithEventsCreateViewBtnAsSystem.Windows.Forms.Button
  FriendWithEventsbtnAlterTableAsSystem.Windows.Forms.Button
  FriendWithEventsbtnCreateOthersAsSystem.Windows.Forms.Button
  FriendWithEventsbtnDropTableAsSystem.Windows.Forms.Button
  FriendWithEventsbtnViewDataAsSystem.Windows.Forms.Button
  FriendWithEventsbtnViewSPAsSystem.Windows.Forms.Button
  FriendWithEventsbtnViewViewAsSystem.Windows.Forms.Button
  PrivateSubInitializeComponent()


  
复制代码 代码如下:

Me.CreateDBBtn=NewSystem.Windows.Forms.Button()
  Me.CreateTableBtn=NewSystem.Windows.Forms.Button()
  Me.CreateSPBtn=NewSystem.Windows.Forms.Button()
  Me.CreateViewBtn=NewSystem.Windows.Forms.Button()
  Me.btnAlterTable=NewSystem.Windows.Forms.Button()
  Me.btnCreateOthers=NewSystem.Windows.Forms.Button()
  Me.btnDropTable=NewSystem.Windows.Forms.Button()
  Me.btnViewData=NewSystem.Windows.Forms.Button()
  Me.btnViewSP=NewSystem.Windows.Forms.Button()
  Me.btnViewView=NewSystem.Windows.Forms.Button()
  Me.DataGrid1=NewSystem.Windows.Forms.DataGrid()
  CType(Me.DataGrid1,System.ComponentModel.ISupportInitialize).BeginInit()
  Me.SuspendLayout()
  '
  'CreateDBBtn
  '
  Me.CreateDBBtn.Location=NewSystem.Drawing.Point(19,9)
  Me.CreateDBBtn.Name="CreateDBBtn"
  Me.CreateDBBtn.Size=NewSystem.Drawing.Size(104,23)
  Me.CreateDBBtn.TabIndex=0
  Me.CreateDBBtn.Text="创建数据库"
  '
  'CreateTableBtn
  '
  Me.CreateTableBtn.Location=NewSystem.Drawing.Point(139,9)
  Me.CreateTableBtn.Name="CreateTableBtn"
  Me.CreateTableBtn.TabIndex=1
  Me.CreateTableBtn.Text="创建表"
  '
  'CreateSPBtn
  '
  Me.CreateSPBtn.Location=NewSystem.Drawing.Point(230,9)
  Me.CreateSPBtn.Name="CreateSPBtn"
  Me.CreateSPBtn.Size=NewSystem.Drawing.Size(104,23)
  Me.CreateSPBtn.TabIndex=2
  Me.CreateSPBtn.Text="创建存储过程"
  '
  'CreateViewBtn
  '
  Me.CreateViewBtn.Location=NewSystem.Drawing.Point(350,9)
  Me.CreateViewBtn.Name="CreateViewBtn"
  Me.CreateViewBtn.TabIndex=3中国网管联盟www.bitscn.com
  Me.CreateViewBtn.Text="创建视图"
  '
  'btnAlterTable
  '
  Me.btnAlterTable.Location=NewSystem.Drawing.Point(441,9)
  Me.btnAlterTable.Name="btnAlterTable"
  Me.btnAlterTable.TabIndex=4
  Me.btnAlterTable.Text="修改表"

#p#
复制代码 代码如下:


  'btnCreateOthers
  '
  Me.btnCreateOthers.Location=NewSystem.Drawing.Point(17,43)
  Me.btnCreateOthers.Name="btnCreateOthers"
  Me.btnCreateOthers.Size=NewSystem.Drawing.Size(104,23)
  Me.btnCreateOthers.TabIndex=5
  Me.btnCreateOthers.Text="创建规则和索引"
  feedom.net
  '
  'btnDropTable
  '
  Me.btnDropTable.Location=NewSystem.Drawing.Point(138,43)
  Me.btnDropTable.Name="btnDropTable"
  Me.btnDropTable.TabIndex=6
  Me.btnDropTable.Text="删除表"
  '
  'btnViewData
  '
  Me.btnViewData.Location=NewSystem.Drawing.Point(351,43)
  Me.btnViewData.Name="btnViewData"
  Me.btnViewData.TabIndex=7
  Me.btnViewData.Text="查看数据"
  '
  'btnViewSP
  '
  Me.btnViewSP.Location=NewSystem.Drawing.Point(230,43)
  feedom.net
  Me.btnViewSP.Name="btnViewSP"
 HandlesbtnDropTable.Click
  DimsqlAsString="DROPTABLEMyTable"
  ExecuteSQLStmt(sql)
  EndSub


复制代码 代码如下:

  '浏览表数据
  PrivateSubbtnViewData_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
  HandlesbtnViewData.Click
  conn=NewSqlConnection(ConnectionString)
  Ifconn.State=ConnectionState.OpenThen
  conn.Close()
  EndIf
  ConnectionString="DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;"
  conn.ConnectionString=ConnectionString
  conn.Open()
  DimdaAsNewSqlDataAdapter("SELECT*FROMmyTable",conn)
  DimdsAsNewDataSet("myTable")
  da.Fill(ds,"myTable")
  DataGrid1.DataSource=ds.Tables("myTable").DefaultView
  EndSub
  '浏览存储过程
  PrivateSubbtnViewSP_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
  HandlesbtnViewSP.Click
  conn=NewSqlConnection(ConnectionString)
  Ifconn.State=ConnectionState.OpenThen
  conn.Close()
  EndIf
  ConnectionString="DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;"
  conn.ConnectionString=ConnectionString
  conn.Open()
  DimdaAsNewSqlDataAdapter("myProc",conn)
  DimdsAsNewDataSet("SP")
  da.Fill(ds,"SP")
  DataGrid1.DataSource=ds.DefaultViewManager
  EndSub
  '浏览视图
  PrivateSubbtnViewView_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_
  HandlesbtnViewView.Click
  conn=NewSqlConnection(ConnectionString)
  Ifconn.State=ConnectionState.OpenThen
  conn.Close()
  EndIf
  ConnectionString="DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;"
  conn.ConnectionString=ConnectionString
  conn.Open()
  DimdaAsNewSqlDataAdapter("SELECT*FROMmyView",conn)
  DimdsAsNewDataSet()
  da.Fill(ds)
  DataGrid1.DataSource=ds.DefaultViewManager
  EndSub
  PrivateSubExecuteSQLStmt(ByValsqlAsString)
  conn=NewSqlConnection(ConnectionString)
  '打开连接
  Ifconn.State=ConnectionState.OpenThen
  conn.Close()
  EndIf
  ConnectionString="DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;"
  conn.ConnectionString=ConnectionString
  conn.Open()
  cmd=NewSqlCommand(sql,conn)
  Try
  cmd.ExecuteNonQuery()
  CatchaeAsSqlException
  MessageBox.Show(ae.Message.ToString())
  EndTry
  EndSub
  EndClass
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1060120
帖子:212021
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP