程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> ASP編程 >> ASP技巧 >> ASP.NET連SQL7接口源代碼

ASP.NET連SQL7接口源代碼

編輯:ASP技巧

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.

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.

SqlClIEnt
[Visual Basic]
Imports System
Imports System.Data
Imports System.Data.SqlClIEnt
Imports Microsoft.VisualBasic

Public Class Sample

  Public Shared Sub Main()
    Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;" & _
                                                       "User Id=sa;PassWord=pwd;Initial Catalog=northwind")

    Dim catCMD As SqlCommand = nwindConn.CreateCommand()
    catCMD.CommandText = "SELECT CategoryID, CategoryName FROM CategorIEs"

    nwindConn.Open()

    Dim myReader As SqlDataReader = catCMD.ExecuteReader()

    Do While myReader.Read()
      Console.WriteLine(vbTab & "{0}" & vbTab & "{1}", myReader.GetInt32(0), myReader.GetString(1))
    Loop

    myReader.Close()
    nwindConn.Close()
  End Sub
End Class
[C#]
using System;
using System.Data;
using System.Data.SqlClIEnt;

class Sample
{
  public static void Main()
  {
    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;User Id=sa;PassWord=pwd;Initial Catalog=northwind");

    SqlCommand catCMD = nwindConn.CreateCommand();
    catCMD.CommandText = "SELECT CategoryID, CategoryName FROM CategorIEs";

    nwindConn.Open();

    SqlDataReader myReader = catCMD.ExecuteReader();

    while (myReader.Read())
    {
      Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
    }

    myReader.Close();
    nwindConn.Close();
  }
}
OleDb
[Visual Basic]
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports Microsoft.VisualBasic

Public Class Sample

  Public Shared Sub Main()
    Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" & _
                                                           "User Id=sa;PassWord=pwd;Initial Catalog=northwind")

    Dim catCMD As OleDbCommand = nwindConn.CreateCommand()
    catCMD.CommandText = "SELECT CategoryID, CategoryName FROM CategorIEs"

    nwindConn.Open()

    Dim myReader As OleDbDataReader = catCMD.ExecuteReader()

    Do While myReader.Read()
      Console.WriteLine(vbTab & "{0}" & vbTab & "{1}", myReader.GetInt32(0), myReader.GetString(1))
    Loop

    myReader.Close()
    nwindConn.Close()
  End Sub
End Class
[C#]
using System;
using System.Data;
using System.Data.OleDb;

class Sample
{
  public static void Main()
  {
    OleDbConnection nwindConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;User Id=sa;PassWord=pwd;Initial Catalog=northwind");

    OleDbCommand catCMD = nwindConn.CreateCommand();
    catCMD.CommandText = "SELECT CategoryID, CategoryName FROM CategorIEs";

    nwindConn.Open();

    OleDbDataReader myReader = catCMD.ExecuteReader();

    while (myReader.Read())
    {
      Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));
    }

    myReader.Close();
    nwindConn.Close();
  }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved