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!
    
    
    
    

        

  • 相关阅读:
    Mac os下安装pycurl
    Mac os 10.9下面配置JAVA_HOME
    同步,异步,阻塞,非阻塞
    Python处理XML
    Apriori算法在购物篮分析中的运用
    Python抓取双色球数据
    为什么这么多Python框架
    Caffe + Ubuntu 14.04 64bit + CUDA 6.5 配置说明
    ubuntu 开机自动挂载分区
    VIM 配置随手记
  • 原文地址:https://www.cnblogs.com/fmy-hmfy/p/3812661.html
Copyright © 2011-2022 走看看