找回密码
 立即注册
搜索
查看: 254|回复: 3

★Asp.net连接SQL Server2000数据库例程★

[复制链接]

15

主题

0

回帖

20

积分

新手上路

积分
20
发表于 2004-2-20 14:59:50 | 显示全部楼层 |阅读模式
★Asp.net连接SQL Server2000数据库例程★


大家好,我是Asp.net的忠实爱好者,很想把这方面的经验和大家分享一下. 目前我选用的是时代互联(http://www.now.net.cn )的Asp.net+SQLServer2000空间(商务C).
欢迎大家一起进来讨论讨论!

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>

Asp.net+SQLServer2000这款时代互联的空间在使用的速度和稳定上我都是非常满意的!

Asp.net虚拟主机的服务提供商中,目前首推的是CNNIC的其中一家权威机构---时代互联(www.now.net.cn),他们在2003年便更新使用微软发布最新版本的ASP.net Web Matrix了.经笔者的使用测试,他提供的ASP.NET空间性能非常的稳定,版本也会定期的更新升级!

使用详解和例程: http://www.now.net.cn/support/host/(这里有更多更细的Asp.net的详细例程)
全球免费咨询电话:http://www.now.net.cn/customer/moreline.net(很方便的)
时代互联的Asp.net空间购买方法:http://www.now.net.cn/host


喜欢asp.net和需要建网站的朋友,可以去网站看看,相信会在asp.net的使用方面会有更深的认识。

我的QQ:108786612、199332496、108786611,MSN:[email protected]朋友们可以随时与我联系^_^

22

主题

328

回帖

467

积分

中级会员

积分
467
发表于 2004-2-21 13:24:52 | 显示全部楼层
有一个问题,在M$的quickstart中的数据库联接使用的是
SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
我每次都不能联接成功,可能是SQL需要设置或其他,大虾知道如何解决么?
回复

使用道具 举报

5

主题

24

回帖

41

积分

注册会员

积分
41
发表于 2004-2-23 09:16:15 | 显示全部楼层

是啊,我也期待有人回答

是啊,我也期待有人回答
回复

使用道具 举报

726

主题

7323

回帖

5966

积分

网站编辑

海盗船长

积分
5966
发表于 2004-2-25 15:19:02 | 显示全部楼层
最初由 arcke 发布
[B]有一个问题,在M$的quickstart中的数据库联接使用的是
SqlConnection("server=(local)\\NetSDK;database=pubs;Trusted_Connection=yes");
我每次都不能联接成功,可能是SQL需要设置或其他,大虾知道如何解决么? [/B]

其实asp.net和asp连接数据库的原理差不多,就是语法不一样而已,关于这样的例子大家找找看吧
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|海浩社区

GMT+8, 2025-9-22 13:04 , Processed in 0.085348 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表