以下代碼演示了如何使用asp.net連接sql server2000數據庫并操作的代碼實例, 和asp.net初學者分享一下.
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> | 
新聞熱點
疑難解答
圖片精選