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

创建动态SQL Server数据库表的简单示例

发布时间:2020-12-12 07:51:32 所属栏目:MsSql教程 来源:网络整理
导读:感兴趣的小伙伴,下面一起跟随编程之家 jb51.cc的小编两巴掌来看看吧! 以下是引用片段: 代码如下: ImportsSystem.Data ImportsSystem.Data.SqlClient PublicClassForm1 InheritsSystem.Windows.Forms.Form PrivateConnectionStringAsString=DataSource=.;I

代码如下:



  '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,_ 
 

代码如下:



  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 
  '创建数据库 
  PrivateSubCreateDBBtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)_ 
  HandlesCreateDBBtn.Click 
  conn=NewSqlConnection(ConnectionString) 
  '打开连接 
  Ifconn.State<>ConnectionState.OpenThen 
  conn.Open() 
  EndIf 
  'MyDataBase为数据库名称 
  DimsqlAsString="CREATEDATABASEMyDataBaSEONPRIMARY(Name=MyDataBase_data,filename="+_ 
  54com.cn 
  "'D:MyDataBase.mdf',size=3,"+"maxsize=5,filegrowth=10%)logon"+"(name=MyDataBase_log,"+_ 
  "filename='D:MyDataBase.ldf',"+"maxsize=20,filegrowth=1)" 
  cmd=NewSqlCommand(sql,conn) 
  Try 
  cmd.ExecuteNonQuery() 
  CatchaeAsSqlException 
  MessageBox.Show(ae.Message.ToString()) 
  EndTry 
  EndSub 
  '创建表 
  PrivateSubCreateTableBtn_Click(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)" 

(编辑:李大同)

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

推荐文章
    热点阅读