Asp.net如何連接SQL Server2000數(shù)據(jù)庫
2024-07-10 13:07:03
供稿:網(wǎng)友
大家好,以下是有關(guān)asp.net連接sql server2000數(shù)據(jù)庫的例程,
在這里和大家分享一下:
asp.net連接sql server2000數(shù)據(jù)庫例程詳解:
<%@ import namespace="system.data" %>
<%@ import namespace="system.data.sqlclient" %>
<script laguage="vb" runat="server">
sub page_load(sender as object,e as eventargs)
dim myconnection as sqlconnection
dim mycommand as sqlcommand
dim ds as dataset
'1.connect to sql server
myconnection = new sqlconnection( "server=localhost;database=pubs;uid=ueytjdf;pwd=doekdf" )
myconnection.open()
la1.text="connection opened!"
'2.create a table
mycommand = new sqlcommand( "create table [test] ([id] [int] identity (1, 1) not null ,[name]
[char] (10) collate chinese_prc_ci_as null ,[sex] [char] (10) collate chinese_prc_ci_as null
)", myconnection )
mycommand.executenonquery()
la2.text="new table created!"
'2 添加紀(jì)錄
mycommand = new sqlcommand( "insert into [test] (name,sex) values( '黃志文','男' )",
myconnection )
mycommand.executenonquery()
la3.text="new record inserted!"
'3 更新數(shù)據(jù)
mycommand = new sqlcommand( "update [test] set name='smith' where name='李明'", myconnection )
mycommand.executenonquery()
la4.text="record updated!"
'4 刪除數(shù)據(jù)
mycommand = new sqlcommand( "delete from [test] where name='smith'", myconnection )
mycommand.executenonquery()
la5.text="record deleted!"
'5 用datagrid顯示數(shù)據(jù)
mycommand = new sqlcommand( "select * from [test]", myconnection )
mydatagrid.datasource=mycommand.executereader()
mydatagrid.databind()
end sub
</script>
<html>
<body>
<asp:label id="la1" runat="server" /><br>
<asp:label id="la2" runat="server" /><br>
<asp:label id="la3" runat="server" /><br>
<asp:label id="la4" runat="server" /><br>
<asp:label id="la5" runat="server" /><br>
<asp:datagrid id="mydatagrid" runat="server"
bordercolor="black"
borderwidth="1"
gridlines="both"
cellpadding="3"
cellspacing="0"
font-name="verdana"
font-size="10pt"
headerstyle-backcolor="#aaaadd"
alternatingitemstyle-backcolor="#eeeeee"
>
</asp:datagrid>
</body>
</html>