ASP.NET入门教程 ASP.NET操作数据库(2)

发布时间:2007年08月19日      浏览次数:923 次
通过一个Command对象,我们才可以对数据库进行操作
Dim myConnection As SQLConnection = New SQLConnection("server=localhost;uid=sa;
pwd=;database=pubs")
Dim myCommand As SQLCommand = New SQLCommand("select * from Authors", myConnection)
myConnection.Open()
Dim dr As New SQLDataReader
myCommand.Execute(dr)
...
myConnection.Close()
或者 这样做
Dim myConnection As New SQLConnection("server=localhost;uid=sa;pwd=;database=pubs")
Dim mycommand As New SQLCommand( _
"UPDATE Authors SET phone='(800) 555-5555' WHERE au_id = '123-45-6789'", _
myConnection)
myCommand.ActiveConnection.Open()
myCommand.ExecuteNonQuery()
myCommand.ActiveConnection.Close()
  这些都是SQLCommand的标准用法,下面列出了Command的所有属性和相关方法。
  ActiveConnection 取得或设置联结Connections
  CommandText 执行的SQL语句或储存过程(StoredProcedure)名
  CommandTimeout 超时
  CommandType Command操作的类型(StoredProcedure,Text,TableDirect)三种,默认Text
  Parameters 操作储存过程时使用
  Execute() 执行SQL语句或储存过程
  ExecuteNonQuery() 同上,但无返回,或者说,只返回记录的数量
  注意: 和ASP一样,在运行完以后一定要注意关闭Connection,否则会很耗服务器资源的。
  5.4 数据的显示
  在这节的讲解前,我们先建立一个数据库,名字叫 aspnet 然后里面有一张表user 结构如下:
uid username Email
1 User1 Mail1
2 User2 Mail2
3 User3 Mail3
  SQL 语句 Select * From User
  数据库语句 server=localhost;uid=sa;pwd=;database=aspnet
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!