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