程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> 教你創建動態MSSQL 數據庫表(4)

教你創建動態MSSQL 數據庫表(4)

編輯:關於SqlServer
 Me.CreateVIEwBtn, Me.CreateSPBtn, Me.CreateTableBtn, Me.CreateDBBtn})
  Me.Name = "Form1"
  Me.Text = "動態創建SQL Server數據庫、表、存儲過程等架構信息"
  CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
  Me.ResumeLayout(False)
  End Sub
  #End Region
  ' 創建數據庫
  Private Sub CreateDBBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles CreateDBBtn.Click
  conn = New SqlConnection(ConnectionString)
  ' 打開連接
  If conn.State <> ConnectionState.Open Then
  conn.Open()
  End If
  'MyDataBase為數據庫名稱
  Dim sql As String = "CREATE DATABASE MyDataBase ON PRIMARY (Name=MyDataBase_data, filename = " + _
  54com.cn
  "'D:\MyDataBase.mdf', size=3," + "maxsize=5, filegrowth=10%) log on" + "(name=MyDataBase_log, " + _
  "filename='D:\MyDataBase.ldf',size=3," + "maxsize=20,filegrowth=1)"
  cmd = New SqlCommand(sql, conn)
  Try
  cmd.ExecuteNonQuery()
  Catch ae As SqlException
  MessageBox.Show(ae.Message.ToString())
  End Try
  End Sub
  '創建表
  Private Sub CreateTableBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
  Handles CreateTableBtn.Click
  conn = New SqlConnection(ConnectionString)
  ' 打開連接
  If conn.State = ConnectionState.Open Then
  conn.Close()
  End If
  ConnectionString = "Data Source=.;Initial Catalog=MyDataBase;User Id=sa;PassWord=;"
  conn.ConnectionString = ConnectionString
  conn.Open()
  sql = "CREATE TABLE myTable" + "(myId INTEGER CONSTRAINT PKeyMyId PRIMARY KEY," + _
  "myName CHAR(50) NOT Null, myAddress CHAR(255), myValues FLOAT)"
  cmd = New SqlCommand(sql, conn)
  Try
  cmd.ExecuteNonQuery()
  ' 添加紀錄
  54com.cn
  sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _
  "VALUES (1001, _'【孟憲會之精彩世界】之一', 'http://XML.sz.luohuedu.Net/', 100 ) "
  cmd = New SqlCommand(sql, conn)
  cmd.ExecuteNonQuery()
  sql = "INSERT INTO myTable(myId, myName, myAddress, myValues) " + _
  "VALUES (1002, '【孟憲會之精彩世界】之二', 'http://www.erp800.com/net_lover/', 99) "
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved