首页 > 编程 > .NET > 正文

asp.net连接sql数据库实例基础教程

2024-07-10 13:05:38
字体:
来源:转载
供稿:网友

以下代码演示了如何使用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>


国内最大的酷站演示中心!
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表