创建动态MSSQL数据库表

前端之家收集整理的这篇文章主要介绍了创建动态MSSQL数据库表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

以下是引用片段:
<div class="codetitle"><a style="CURSOR: pointer" data="37229" class="copybut" id="copybut37229" onclick="doCopy('code37229')"> 代码如下:

<div class="codebody" id="code37229">
  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()
  <div class="codetitle"><a style="CURSOR: pointer" data="28943" class="copybut" id="copybut28943" onclick="doCopy('code28943')"> 代码如下:
<div class="codebody" id="code28943">
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#
<div class="codetitle"><a style="CURSOR: pointer" data="6917" class="copybut" id="copybut6917" onclick="doCopy('code6917')"> 代码如下:
<div class="codebody" id="code6917">  '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"
  Me.btnViewSP.Size=NewSystem.Drawing.Size(104,23)
  Me.btnViewSP.TabIndex=8
  Me.btnViewSP.Text="查看存储过程"
  '
  'btnViewView
  '
  Me.btnViewView.Location=NewSystem.Drawing.Point(443,43)
  Me.btnViewView.Name="btnViewView"
  Me.btnViewView.TabIndex=9
  Me.btnViewView.Text="查看视图"
  '
  'DataGrid1
  '
  Me.DataGrid1.DataMember=""
  Me.DataGrid1.HeaderForeColor=System.Drawing.SystemColors.ControlText
  Me.DataGrid1.Location=NewSystem.Drawing.Point(20,76)54com.cn
  Me.DataGrid1.Name="DataGrid1"
  Me.DataGrid1.Size=NewSystem.Drawing.Size(500,183)
  Me.DataGrid1.TabIndex=10
  '
  'Form1
  '
  Me.AutoScaleBaseSize=NewSystem.Drawing.Size(5,13)
  Me.ClientSize=NewSystem.Drawing.Size(538,281)
  Me.Controls.AddRange(NewSystem.Windows.Forms.Control(){Me.DataGrid1,Me.btnViewView,
  Me.btnViewSP,Me.btnViewData,Me.btnDropTable,Me.btnCreateOthers,Me.btnAlterTable,


<div class="codetitle"><a style="CURSOR: pointer" data="46438" class="copybut" id="copybut46438" onclick="doCopy('code46438')"> 代码如下:
<div class="codebody" id="code46438">  Me.CreateViewBtn,Me.CreateSPBtn,Me.CreateTableBtn,Me.CreateDBBtn})
  Me.Name="Form1"
  Me.Text="动态创建sqlServer数据库、表、存储过程等架构信息"
  CType(Me.DataGrid1,System.ComponentModel.ISupportInitialize).EndInit()
  Me.ResumeLayout(False)
  EndSub
  #EndRegion
  '创建数据库
  PrivateSubCreateDBBtnClick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)
  HandlesCreateDBBtn.Click
  conn=NewsqlConnection(ConnectionString)
  '打开连接
  Ifconn.State<>ConnectionState.OpenThen
  conn.Open()
  EndIf
  'MyDataBase为数据库名称
  DimsqlAsString="CREATEDATABASEMyDataBaSEONPRIMARY(Name=MyDataBasedata,filename="+
  54com.cn
  "'D:\MyDataBase.mdf',size=3,"+"maxsize=5,filegrowth=10%)logon"+"(name=MyDataBaselog,"+
  "filename='D:\MyDataBase.ldf',"+"maxsize=20,filegrowth=1)"
  cmd=NewsqlCommand(sql,conn)
  Try
  cmd.ExecuteNonQuery()
  CatchaeAssqlException
  MessageBox.Show(ae.Message.ToString())
  EndTry
  EndSub
  '创建表
  PrivateSubCreateTableBtnClick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)
  HandlesCreateTableBtn.Click
  conn=NewsqlConnection(ConnectionString)
  '打开连接
  Ifconn.State=ConnectionState.OpenThen
  conn.Close()
  EndIf
  ConnectionString="DataSource=.;InitialCatalog=MyDataBase;UserId=sa;Password=;"
  conn.ConnectionString=ConnectionString
  conn.Open()
  sql="CREATETABLEmyTable"+"(myIdINTEGERCONSTRAINTPKeyMyIdPRIMARYKEY,"+_
  "myNameCHAR(50)NOTNull,myAddressCHAR(255),myValuesFLOAT)"
  cmd=NewsqlCommand(sql,conn)
  Try
  cmd.ExecuteNonQuery()
  '添加纪录
  54com.cn
  sql="INSERTINTOmyTable(myId,myName,myAddress,myValues)"+
  "VALUES(1001,
'【孟宪会之精彩世界】之一','http://xml.sz.luohuedu.net/',100)"
  cmd=NewsqlCommand(sql,conn)
  cmd.ExecuteNonQuery()
  sql="INSERTINTOmyTable(myId,myValues)"+_
  "VALUES(1002,'【孟宪会之精彩世界】之二','http://www.erp800.com/net_lover/',99)"

#p#
<div class="codetitle"><a style="CURSOR: pointer" data="27875" class="copybut" id="copybut27875" onclick="doCopy('code27875')"> 代码如下:<div class="codebody" id="code27875">  cmd=NewsqlCommand(sql,myValues)"+_
  "VALUES(1003,'【孟宪会之精彩世界】之三',99)"
  cmd=NewsqlCommand(sql,myValues)"+_
  "VALUES(1004,'【孟宪会之精彩世界】之四',conn)
  cmd.ExecuteNonQuery()
  CatchaeAssqlException
  MessageBox.Show(ae.Message.ToString())
  EndTry
  EndSub
  '创建存储过程
  PrivateSubCreateSPBtnClick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)
  HandlesCreateSPBtn.Click
  sql="CREATEPROCEDUREmyProcAS"+"SELECTmyName,myAddressFROMmyTableGO"
  ExecutesqlStmt(sql)
  EndSub
  '创建视图
  PrivateSubCreateViewBtnClick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)
  HandlesCreateViewBtn.Click
  sql="CREATEVIEWmyViewASSELECTmyNameFROMmyTable"
  ExecutesqlStmt(sql)
  EndSub
  '修改
  PrivateSubbtnAlterTableClick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)
  54com.cn
  HandlesbtnAlterTable.Click
  sql="ALTERTABLEMyTableADDnewColdatetimeNOTNULLDEFAULT(getdate())"
  ExecutesqlStmt(sql)
  EndSub
  '创建规则和索引
  PrivateSubbtnCreateOthersClick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)
  HandlesbtnCreateOthers.Click
  sql="CREATEUNIQUEINDEX"+"myIdxONmyTable(myName)"
  ExecutesqlStmt(sql)
  sql="CREATERULEmyRule"+"AS@myValues>=90AND@myValues<9999"
  ExecutesqlStmt(sql)
  EndSub
  '删除
  PrivateSubbtnDropTableClick(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)
  HandlesbtnDropTable.Click
  DimsqlAsString="DROPTABLEMyTable"
  ExecutesqlStmt(sql)
  EndSub
<div class="codetitle"><a style="CURSOR: pointer" data="10121" class="copybut" id="copybut10121" onclick="doCopy('code10121')"> 代码如下:<div class="codebody" id="code10121">
  '浏览表数据
  PrivateSubbtnViewDataClick(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("SELECTFROMmyTable",conn)
  DimdsAsNewDataSet("myTable")
  da.Fill(ds,"myTable")
  DataGrid1.DataSource=ds.Tables("myTable").DefaultView
  EndSub
  '浏览存储过程
  PrivateSubbtnViewSPClick(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
  '浏览视图
  PrivateSubbtnViewViewClick(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

原文链接:https://www.f2er.com/mssql/64381.html
动态动态动态数据库数据库

猜你在找的MsSQL相关文章