zoukankan      html  css  js  c++  java
  • ado.net数据库操作(2)

    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!
    
    
    
    

        

  • 相关阅读:
    Android程序反编译的方法(转)
    VMware的Easy Install安装
    Oracle ORA12514:TNS:监听程序当前无法识别连接描述符中请求的服务
    Java Eclipse 如何导入外部Jar包
    Oracle IMP00010: 不是有效的导出文件,标题验证失败
    怎样取消Windows 2003 server 意外关机提示
    没有文件扩展“.vbs”的脚本引擎的解决方案
    Oracle init.ora常用配置详解
    eclipse断点调试 出现Source not found
    VMware Workstation 8
  • 原文地址:https://www.cnblogs.com/fmy-hmfy/p/3812661.html
Copyright © 2011-2022 走看看