ASP。NET连SQL7接口源代码?

发表于:2007-06-30来源:作者:点击数: 标签:
The following example shows what a simple ADO.NET application that connects to the Northwind database and returns a list of Categories would look like. The example writes the output to the console, or command prompt.br br The following exam
The following example shows what a simple ADO.NET application that connects to the Northwind database and returns a list of Categories would look like. The example writes the output to the console, or command prompt.<br>
<br>
The following example shows what a simple ADO.NET application that connects to the Northwind database and returns a list of Categories. The example writes the output to the console, or command prompt.<br>
<br>
SqlClient<br>
[Visual Basic]<br>
Imports System<br>
Imports System.Data<br>
Imports System.Data.SqlClient<br>
Imports Microsoft.VisualBasic<br>
<br>
Public Class Sample<br>
<br>
&nbsp;&nbsp;Public Shared Sub Main() <br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim nwindConn As SqlConnection = New SqlConnection(&quot;Data Source=localhost;&quot; & _<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;User Id=sa;Password=pwd;Initial Catalog=northwind&quot;)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim catCMD As SqlCommand = nwindConn.CreateCommand()<br>
&nbsp;&nbsp;&nbsp;&nbsp;catCMD.CommandText = &quot;SELECT CategoryID, CategoryName FROM Categories&quot;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;nwindConn.Open()<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim myReader As SqlDataReader = catCMD.ExecuteReader()<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Do While myReader.Read()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(vbTab & &quot;{0}&quot; & vbTab & &quot;{1}&quot;, myReader.GetInt32(0), myReader.GetString(1))<br>
&nbsp;&nbsp;&nbsp;&nbsp;Loop<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;myReader.Close()<br>
&nbsp;&nbsp;&nbsp;&nbsp;nwindConn.Close()<br>
&nbsp;&nbsp;End Sub<br>
End Class<br>
[C#]<br>
using System;<br>
using System.Data;<br>
using System.Data.SqlClient;<br>
<br>
class Sample<br>
{<br>
&nbsp;&nbsp;public static void Main() <br>
&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;SqlConnection nwindConn = new SqlConnection(&quot;Data Source=localhost;User Id=sa;Password=pwd;Initial Catalog=northwind&quot;);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;SqlCommand catCMD = nwindConn.CreateCommand();<br>
&nbsp;&nbsp;&nbsp;&nbsp;catCMD.CommandText = &quot;SELECT CategoryID, CategoryName FROM Categories&quot;;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;nwindConn.Open();<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;SqlDataReader myReader = catCMD.ExecuteReader();<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;while (myReader.Read())<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(&quot;\t{0}\t{1}&quot;, myReader.GetInt32(0), myReader.GetString(1));<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;myReader.Close();<br>
&nbsp;&nbsp;&nbsp;&nbsp;nwindConn.Close();<br>
&nbsp;&nbsp;}<br>
}<br>
OleDb<br>
[Visual Basic]<br>
Imports System<br>
Imports System.Data<br>
Imports System.Data.OleDb<br>
Imports Microsoft.VisualBasic<br>
<br>
Public Class Sample<br>
<br>
&nbsp;&nbsp;Public Shared Sub Main() <br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim nwindConn As OleDbConnection = New OleDbConnection(&quot;Provider=SQLOLEDB;Data Source=localhost;&quot; & _<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;User Id=sa;Password=pwd;Initial Catalog=northwind&quot;)<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim catCMD As OleDbCommand = nwindConn.CreateCommand()<br>
&nbsp;&nbsp;&nbsp;&nbsp;catCMD.CommandText = &quot;SELECT CategoryID, CategoryName FROM Categories&quot;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;nwindConn.Open()<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Dim myReader As OleDbDataReader = catCMD.ExecuteReader()<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;Do While myReader.Read()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(vbTab & &quot;{0}&quot; & vbTab & &quot;{1}&quot;, myReader.GetInt32(0), myReader.GetString(1))<br>
&nbsp;&nbsp;&nbsp;&nbsp;Loop<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;myReader.Close()<br>
&nbsp;&nbsp;&nbsp;&nbsp;nwindConn.Close()<br>
&nbsp;&nbsp;End Sub<br>
End Class<br>
[C#]<br>
using System;<br>
using System.Data;<br>
using System.Data.OleDb;<br>
<br>
class Sample<br>
{<br>
&nbsp;&nbsp;public static void Main() <br>
&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;OleDbConnection nwindConn = new OleDbConnection(&quot;Provider=SQLOLEDB;Data Source=localhost;User Id=sa;Password=pwd;Initial Catalog=northwind&quot;);<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;OleDbCommand catCMD = nwindConn.CreateCommand();<br>
&nbsp;&nbsp;&nbsp;&nbsp;catCMD.CommandText = &quot;SELECT CategoryID, CategoryName FROM Categories&quot;;<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;nwindConn.Open();<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;OleDbDataReader myReader = catCMD.ExecuteReader();<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;while (myReader.Read())<br>
&nbsp;&nbsp;&nbsp;&nbsp;{<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Console.WriteLine(&quot;\t{0}\t{1}&quot;, myReader.GetInt32(0), myReader.GetString(1));<br>
&nbsp;&nbsp;&nbsp;&nbsp;}<br>
<br>
&nbsp;&nbsp;&nbsp;&nbsp;myReader.Close();<br>
&nbsp;&nbsp;&nbsp;&nbsp;nwindConn.Close();<br>
&nbsp;&nbsp;}<br>
}<br>

原文转自:http://www.ltesting.net