ado.net数据操作全接触二(query,Parameters)

原创 2005年03月02日 10:50:00
5.1使用SQLDataReader进行数据库查询
1: <%@ Import Namespace="System.Data" %>
2: <%@ Import NameSpace="System.Data.SqlClient" %>
3:
4: <%
5: Dim myConnection As SqlConnection
6: Dim myCommand As SqlCommand
7: Dim myDataReader As SqlDataReader
8:
9: myConnection = New SqlConnection( "server=localhost;uid=sa;database=Pubs" )
10: myConnection.Open()
11: myCommand = New SqlCommand( "Select * from Authors", myConnection )
12: myDataReader = myCommand.ExecuteReader()
13: While myDataReader.Read()
14: Response.Write( myDataReader.Item( "au_lname" ) )
15: End While
16: myDataReader.Close()
17: myConnection.Close()
18: %>
19:
5.2在C#中使用SqlDataReader 进行数据库查询

1: <%@ Page Language="C#" %>
2: <%@ Import Namespace="System.Data" %>
3: <%@ Import NameSpace="System.Data.SqlClient" %>
4:
5: <%
6: SqlDataReader myDataReader;
7: SqlConnection myConnection = new http://aspfree.com/chapters/sams/graphics/ccc.gifSqlConnection( "server=localhost;uid=sa;database=Pubs" );
8: myConnection.Open();
9: SqlCommand myCommand = new SqlCommand( "Select * from http://aspfree.com/chapters/sams/graphics/ccc.gifAuthors", myConnection );
10: myDataReader = myCommand.ExecuteReader();
11: while ( myDataReader.Read() )
12: {
13: Response.Write( myDataReader[ "au_lname" ].ToString() );
14: }
15: myDataReader.Close();
16: myConnection.Close();
17: %>
18:
5.3使用OleDbDataReader进行数据库查询
1: <%@ Import Namespace="System.Data" %>
2: <%@ Import NameSpace="System.Data.OleDb" %>
3:
4: <%
5: Dim myConnection As OleDbConnection
6: Dim myCommand As OleDbCommand
7: Dim myDataReader As OleDbDataReader
8:
9: myConnection = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA http://aspfree.com/chapters/sams/graphics/ccc.gifSource=c:/authors.mdb" )
10: myConnection.Open()
11: myCommand = New OleDbCommand( "Select * from Authors", myConnection )
12: myDataReader = myCommand.ExecuteReader()
13: While myDataReader.Read
14: Response.Write( myDataReader.Item( "author" ) )
15: End While
16: myDataReader.Close()
17: myConnection.Close
18: %>
19:
5.5使用sql Parameters
1: <%@ Import Namespace="System.Data" %>
2: <%@ Import NameSpace="System.Data.SqlClient" %>
3:
4: <%
5: Dim myConnection As SqlConnection
6: Dim myCommand As SqlCommand
7: Dim FirstName As String = "Robert"
8: Dim LastName As String = "Johnson"
9:
10: myConnection = New SqlConnection( "server=localhost;uid=sa;pwd=secret;database=myData" )
11: myConnection.Open()
12: myCommand = New SQLCommand( "Insert Authors ( FirstName, LastName ) http://aspfree.com/chapters/sams/graphics/ccc.gifValues ( @FirstName, @LastName )", myConnection )
13:
14: myCommand.Parameters.Add( New SqlParameter( "@FirstName", http://aspfree.com/chapters/sams/graphics/ccc.gifSqlDbType.Varchar, 30 ))
15: myCommand.Parameters( "@FirstName" ).Value = FirstName
16:
17: myCommand.Parameters.Add( New SqlParameter( "@LastName", http://aspfree.com/chapters/sams/graphics/ccc.gifSqlDbType.Varchar, 30 ))
18: myCommand.Parameters( "@LastName" ).Value = LastName
19:
20: myCommand.ExecuteNonQuery()
21: myConnection.Close()
22: %>
23: Record Inserted!
5.6使用sql Parameters(access)
1: <%@ Import Namespace="System.Data" %>
2: <%@ Import NameSpace="System.Data.OleDb" %>
3:
4: <%
5: Dim myConnection As OleDbConnection
6: Dim myCommand As OleDbCommand
7: Dim FirstName As String = "Robert"
8: Dim LastName As String = "Johnson"
9:
10: myConnection = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;http://aspfree.com/chapters/sams/graphics/ccc.gifDATA Source=c:/author2.mdb" )
11: myConnection.Open()
12: myCommand = New OleDbCommand( "Insert INTO Authors ( FirstName, LastName ) http://aspfree.com/chapters/sams/graphics/ccc.gifValues ( @FirstName, @LastName )", myConnection )
13:
14: myCommand.Parameters.Add( New OleDbParameter( "@FirstName", http://aspfree.com/chapters/sams/graphics/ccc.gifOleDbType.Varchar, 30 ))
15: myCommand.Parameters( "@FirstName" ).Value = FirstName
16:
17: myCommand.Parameters.Add( New OleDbParameter( "@LastName", http://aspfree.com/chapters/sams/graphics/ccc.gifOleDbType.Varchar, 30 ))
18: myCommand.Parameters( "@LastName" ).Value = LastName
19:
20: myCommand.ExecuteNonQuery()
21: myConnection.Close()
22: %>
23: Record Inserted!
24:

相关文章推荐

ADO.Net Connectivity using Stored Procedure and Output parameters

原文:http://www.codeproject.com/Tips/990005/ADO-Net-Connectivity-using-Stored-Procedure-and-Ou Create ...
  • Joyhen
  • Joyhen
  • 2015年05月12日 01:24
  • 839

ADO.NET数据操作常见错误总结

1-数据库服务器无法连接问题凡是出现以上问题时,使用如下方法。1检查SQLServer服务是否打开:2检查连接字符串中的Server对应的服务器名称是否正确(注意默认实例,命名实例的名称)3如果示连接...

ADO.NET基本数据操作(增删改查)

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D...

C#ADO.NET数据操作汇总

C#ADO.NET操作数据代码汇总 一.用SqlConnection连接SQL Server 1..加入命名空间 using System.Data.SqlClient; 2.连接数据库 S...

ADO.NET中数据操作浅述

上次我们

Ado.Net操作Excel文件数据常见问题及解决

经常需要在数据库与Execl之间互导数据。net时代,ADO.NET可以使用使用Microsoft.Jet.OleDb访问访问Excel,网上已经有很多类似的资源,最典型也是最简单的可能如下:(asp...

ADO.NET数据操作

  • 2008年04月28日 01:46
  • 200KB
  • 下载

ADO.NET数据操作 PDF

  • 2007年06月12日 15:16
  • 160KB
  • 下载

ADO.NET操作数据库(二)

核心内容:SqlHelper的封装、配置文件的使用、事件监视器、ado.net数据库连接池、可变参数、异常捕获、SQL漏洞注释攻击、使用带参数的sql语句、本文中出现的类的用法。 一:关于SqlHel...

ADO.NET数据操作 Demo

  • 2007年06月12日 15:17
  • 65KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:ado.net数据操作全接触二(query,Parameters)
举报原因:
原因补充:

(最多只允许输入30个字)