下面是利用sql语句创建数据库、表、存储过程、视图、索引、规则、修改表、查看数据等的方法。所要增加的控件如下:
imports system.data
imports system.data.sqlclient
public class form1
inherits system.windows.forms.form
private connectionstring as string = "data source=.;initial catalog=;user id=sa;password=;"
private reader as sqldatareader = nothing
private conn as sqlconnection = nothing
private cmd as sqlcommand = nothing
private altertablebtn as system.windows.forms.button
private sql as string = nothing
private createothersbtn as system.windows.forms.button
#region " windows 窗体设计器生成的代码 "
'窗体重写处置以清理组件列表。
protected overloads overrides sub dispose(byval disposing as boolean)
if disposing then
if not (components is nothing) then
components.dispose()
end if
end if
mybase.dispose(disposing)
end sub
public sub new()
mybase.new()
initializecomponent()
end sub
private components as system.componentmodel.icontainer
friend withevents datagrid1 as system.windows.forms.datagrid
friend withevents createdbbtn as system.windows.forms.button
friend withevents createtablebtn as system.windows.forms.button
friend withevents createspbtn as system.windows.forms.button
friend withevents createviewbtn as system.windows.forms.button
friend withevents btnaltertable as system.windows.forms.button
friend withevents btncreateothers as system.windows.forms.button
friend withevents btndroptable as system.windows.forms.button
friend withevents btnviewdata as system.windows.forms.button
friend withevents btnviewsp as system.windows.forms.button
friend withevents btnviewview as system.windows.forms.button
<system.diagnostics.debuggerstepthrough()> private sub initializecomponent()
me.createdbbtn = new system.windows.forms.button()
me.createtablebtn = new system.windows.forms.button()
me.createspbtn = new system.windows.forms.button()
me.createviewbtn = new system.windows.forms.button()
me.btnaltertable = new system.windows.forms.button()
me.btncreateothers = new system.windows.forms.button()
me.btndroptable = new system.windows.forms.button()
me.btnviewdata = new system.windows.forms.button()
me.btnviewsp = new system.windows.forms.button()
me.btnviewview = new system.windows.forms.button()
me.datagrid1 = new system.windows.forms.datagrid()
ctype(me.datagrid1, system.componentmodel.isupportinitialize).begininit()
me.suspendlayout()
'
'createdbbtn
'
me.createdbbtn.location = new system.drawing.point(19, 9)
me.createdbbtn.name = "createdbbtn"
me.createdbbtn.size = new system.drawing.size(104, 23)
me.createdbbtn.tabindex = 0
me.createdbbtn.text = "创建数据库"
'
'createtablebtn
'
me.createtablebtn.location = new system.drawing.point(139, 9)
me.createtablebtn.name = "createtablebtn"
me.createtablebtn.tabindex = 1
me.createtablebtn.text = "创建表"
'
'createspbtn
'
me.createspbtn.location = new system.drawing.point(230, 9)
me.createspbtn.name = "createspbtn"
me.createspbtn.size = new system.drawing.size(104, 23)
me.createspbtn.tabindex = 2
me.createspbtn.text = "创建存储过程"
'
'createviewbtn
'
me.createviewbtn.location = new system.drawing.point(350, 9)
me.createviewbtn.name = "createviewbtn"
me.createviewbtn.tabindex = 3
me.createviewbtn.text = "创建视图"
'
'btnaltertable
'
me.btnaltertable.location = new system.drawing.point(441, 9)
me.btnaltertable.name = "btnaltertable"
me.btnaltertable.tabindex = 4
me.btnaltertable.text = "修改表"
'
'btncreateothers
'
me.btncreateothers.location = new system.drawing.point(17, 43)
me.btncreateothers.name = "btncreateothers"
me.btncreateothers.size = new system.drawing.size(104, 23)
me.btncreateothers.tabindex = 5
me.btncreateothers.text = "创建规则和索引"
'
'btndroptable
'
me.btndroptable.location = new system.drawing.point(138, 43)
me.btndroptable.name = "btndroptable"
me.btndroptable.tabindex = 6
me.btndroptable.text = "删除表"
'
'btnviewdata
'
me.btnviewdata.location = new system.drawing.point(351, 43)
me.btnviewdata.name = "btnviewdata"
me.btnviewdata.tabindex = 7
me.btnviewdata.text = "查看数据"
'
'btnviewsp
'
me.btnviewsp.location = new system.drawing.point(230, 43)
me.btnviewsp.name = "btnviewsp"
me.btnviewsp.size = new system.drawing.size(104, 23)
me.btnviewsp.tabindex = 8
me.btnviewsp.text = "查看存储过程"
'
'btnviewview
'
me.btnviewview.location = new system.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 = new system.drawing.point(20, 76)
me.datagrid1.name = "datagrid1"
me.datagrid1.size = new system.drawing.size(500, 183)
me.datagrid1.tabindex = 10
'
'form1
'
me.autoscalebasesize = new system.drawing.size(5, 13)
me.clientsize = new system.drawing.size(538, 281)
me.controls.addrange(new system.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 = "动态创建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 = " + _
"'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()
' 添加纪录
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) "
cmd = new sqlcommand(sql, conn)
cmd.executenonquery()
sql = "insert into mytable(myid, myname, myaddress, myvalues) " + _
"values (1003, '【孟宪会之精彩世界】之三', 'http://xml.sz.luohuedu.net/', 99) "
cmd = new sqlcommand(sql, conn)
cmd.executenonquery()
sql = "insert into mytable(myid, myname, myaddress, myvalues) " + _
"values (1004, '【孟宪会之精彩世界】之四', 'http://www.erp800.com/net_lover/', 100) "
cmd = new sqlcommand(sql, conn)
cmd.executenonquery()
catch ae as sqlexception
messagebox.show(ae.message.tostring())
end try
end sub
'创建存储过程
private sub createspbtn_click(byval sender as system.object, byval e as system.eventargs) _
handles createspbtn.click
sql = "create procedure myproc as" + " select myname, myaddress from mytable go"
executesqlstmt(sql)
end sub
'创建视图
private sub createviewbtn_click(byval sender as system.object, byval e as system.eventargs) _
handles createviewbtn.click
sql = "create view myview as select myname from mytable"
executesqlstmt(sql)
end sub
'修改表
private sub btnaltertable_click(byval sender as system.object, byval e as system.eventargs) _
handles btnaltertable.click
sql = "alter table mytable add newcol datetime not null default (getdate())"
executesqlstmt(sql)
end sub
'创建规则和索引
private sub btncreateothers_click(byval sender as system.object, byval e as system.eventargs) _
handles btncreateothers.click
sql = "create unique index " + "myidx on mytable(myname)"
executesqlstmt(sql)
sql = "create rule myrule " + "as @myvalues >= 90 and @myvalues < 9999"
executesqlstmt(sql)
end sub
'删除表
private sub btndroptable_click(byval sender as system.object, byval e as system.eventargs) _
handles btndroptable.click
dim sql as string = "drop table mytable"
executesqlstmt(sql)
end sub
'浏览表数据
private sub btnviewdata_click(byval sender as system.object, byval e as system.eventargs) _
handles btnviewdata.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()
dim da as new sqldataadapter("select * from mytable", conn)
dim ds as new dataset("mytable")
da.fill(ds, "mytable")
datagrid1.datasource = ds.tables("mytable").defaultview
end sub
'浏览存储过程
private sub btnviewsp_click(byval sender as system.object, byval e as system.eventargs) _
handles btnviewsp.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()
dim da as new sqldataadapter("myproc", conn)
dim ds as new dataset("sp")
da.fill(ds, "sp")
datagrid1.datasource = ds.defaultviewmanager
end sub
'浏览视图
private sub btnviewview_click(byval sender as system.object, byval e as system.eventargs) _
handles btnviewview.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()
dim da as new sqldataadapter("select * from myview", conn)
dim ds as new dataset()
da.fill(ds)
datagrid1.datasource = ds.defaultviewmanager
end sub
private sub executesqlstmt(byval sql as string)
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()
cmd = new sqlcommand(sql, conn)
try
cmd.executenonquery()
catch ae as sqlexception
messagebox.show(ae.message.tostring())
end try
end sub
end class
新闻热点
疑难解答