加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

创建动态MSSQL数据库表

发布时间:2020-12-12 09:36:47 所属栏目: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 Publ

<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)"

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

推荐文章
    热点阅读