下面是利用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 sqlmand = 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 (ponents is nothing) then
ponents.dispose()
end if
end if
mybase.dispose(disposing)
end sub
public sub new()
initializeponent()
end sub
private ponents as system.ponentmodel.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 initializeponent()
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.ponentmodel.isupportinitialize).begininit() me.suspendlayout()
createdbbtn
< = "创立数据库"
createtablebtn
< = "创立表"
createspbtn
< = "创立存储过程"
createviewbtn
< = "创立视图"
btnaltertable
me.btnaltertable.location = new system.drawing.point(441, 9) me.btnaltertable.name = "btnaltertable"
me.btnaltertable.tabindex = 4
= "修改表"
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
= "创立规那么和索引" btndroptable
me.btndroptable.location = new system.drawing.point(138, 43) me.btndroptable.name = "btndroptable"
me.btndroptable.tabindex = 6
= "删除表"
btnviewdata
me.btnviewdata.location = new system.drawing.point(351, 43) me.btnviewdata.name = "btnviewdata"
me.btnviewdata.tabindex = 7
= "查看数据"
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
= "查看存储过程"
btnviewview
me.btnviewview.location = new system.drawing.point(443, 43) me.btnviewview.name = "btnviewview"
me.btnviewview.tabindex = 9
= "查看视图"
datagrid1
me.datagrid1.datamember = ""
me.datagrid1.headerforecolor = system.ltext
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)
< = "动态创立sql server数据库、表、存储过程等架构信息"
ctype(me.datagrid1, system.ponentmodel.isupportinitialize).endinit()
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 sqlmand(sql, conn)
try
catch ae as sqlexception
messagebox.string())
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=;" tionstring = connectionstring
conn.open()
sql = "create table mytable" + "(myid integer constraint pkeymyid primary key," + _ "myname char(50) not null, myaddress char(255), myvalues float)"
asp数据
cmd = new sqlmand(sql, conn)
try
添加纪录
sql = "insert into mytable(myid, myname, myaddress, myvalues) " + _
"values (1001, _【孟宪会之精彩世界】之一, xml.sz.luohuedu/, 100 ) "
cmd = new sqlmand(sql, conn)
sql = "insert into mytable(myid, myname, myaddress, myvalues) " + _
"values (1002, 【孟宪会之精彩世界】之二, .erp800./net_lover/, 99) "
cmd = new sqlmand(sql, conn)
sql = "insert into mytable(myid, myname, myaddress, myvalues) " + _
"values (1003, 【孟宪会之精彩世界】之三, xml.sz.luohuedu/, 99) "
cmd = new sqlmand(sql, conn)
sql = "insert into mytable(myid, myname, myaddress, myvalues) " + _
"values (1004, 【孟宪会之精彩世界】之四, .erp800./net_lover/, 100) "
cmd = new sqlmand(sql, conn)
catch ae as sqlexception
messagebox.string())
end try
end sub

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。