首页 > 编程 > .NET > 正文

Asp.net如何连接SQL Server2000数据库

2024-07-10 13:07:03
字体:
来源:转载
供稿:网友
大家好,以下是有关asp.net连接sql server2000数据库的例程,
     在这里和大家分享一下:
    
     asp.net连接sql server2000数据库例程详解:
     <%@ 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 添加纪录
     mycommand = new sqlcommand( "insert into [test] (name,sex) values( '黄志文','男' )",
    
     myconnection )
     mycommand.executenonquery()
     la3.text="new record inserted!"
    
     '3 更新数据
     mycommand = new sqlcommand( "update [test] set name='smith' where name='李明'", myconnection )
     mycommand.executenonquery()
     la4.text="record updated!"
    
     '4 删除数据
     mycommand = new sqlcommand( "delete from [test] where name='smith'", myconnection )
     mycommand.executenonquery()
     la5.text="record deleted!"
    
     '5 用datagrid显示数据
     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>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表