• 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!
    
    
    
    

        

  • 相关阅读:
    Activity生命周期 onCreate onResume onStop onPause (转)
    职场链接
    memset中的sizeof
    c文件操作 (转)
    串口编程 tcflush()函数 (转)
    tcflush 功能(转)
    串口开发(转)
    ios github网址
    做移动端视频通话软件,大致看了下现有的开源软件(转)
    PJSIP框架
  • 原文地址:https://www.cnblogs.com/fmy-hmfy/p/3812661.html
Copyright © 2020-2023  润新知