ado.net資料操作全接觸二(query,Parameters)

來源:互聯網
上載者:User
ado|資料 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 ccc.gifSqlConnection( "server=localhost;uid=sa;database=Pubs" );
8: myConnection.Open();
9: SqlCommand myCommand = new SqlCommand( "Select * from 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 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 ) ccc.gifValues ( @FirstName, @LastName )", myConnection )
13:
14: myCommand.Parameters.Add( New SqlParameter( "@FirstName", ccc.gifSqlDbType.Varchar, 30 ))
15: myCommand.Parameters( "@FirstName" ).Value = FirstName
16:
17: myCommand.Parameters.Add( New SqlParameter( "@LastName", 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;ccc.gifDATA Source=c:\author2.mdb" )
11: myConnection.Open()
12: myCommand = New OleDbCommand( "Insert INTO Authors ( FirstName, LastName ) ccc.gifValues ( @FirstName, @LastName )", myConnection )
13:
14: myCommand.Parameters.Add( New OleDbParameter( "@FirstName", ccc.gifOleDbType.Varchar, 30 ))
15: myCommand.Parameters( "@FirstName" ).Value = FirstName
16:
17: myCommand.Parameters.Add( New OleDbParameter( "@LastName", ccc.gifOleDbType.Varchar, 30 ))
18: myCommand.Parameters( "@LastName" ).Value = LastName
19:
20: myCommand.ExecuteNonQuery()
21: myConnection.Close()
22: %>
23: Record Inserted!
24:
 


相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。