C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

Ado.net各种provider的连接直接读取,填充dataset示例(四)

直接读取

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;" & _
                                                       "Integrated Security=SSPI;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;Integrated Security=SSPI;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;" & _
                                                           "Integrated Security=SSPI;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;Integrated Security=SSPI;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();
  }
}

Odbc

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

Public Class Sample

  Public Shared Sub Main() 
    Dim nwindConn As OdbcConnection = New OdbcConnection("Driver={SQL Server};Server=localhost;" & _
                                                         "Trusted_Connection=yes;Database=northwind")

    Dim catCMD As OdbcCommand = new OdbcCommand("SELECT CategoryID, CategoryName FROM Categories", nwindConn)

    nwindConn.Open()

    Dim myReader As OdbcDataReader = 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.Odbc;

class Sample
{
  public static void Main() 
  {
    OdbcConnection nwindConn = new OdbcConnection("Driver={SQL Server};Server=localhost;" +
                                                  "Trusted_Connection=yes;Database=northwind");

    OdbcCommand catCMD = new OdbcCommand("SELECT CategoryID, CategoryName FROM Categories", nwindConn);

    nwindConn.Open();

    OdbcDataReader myReader = catCMD.ExecuteReader();

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

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

OracleClient

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

Class Sample

  Public Shared Sub Main() 

    Dim oraConn As OracleConnection = New OracleConnection("Data Source=MyOracleServer;Integrated Security=yes;")

    Dim oraCMD As OracleCommand = New OracleCommand("SELECT CUSTOMER_ID, NAME FROM DEMO.CUSTOMER", oraConn)

    oraConn.Open()

    Dim myReader As OracleDataReader = oraCMD.ExecuteReader()

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

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

class Sample
{
  public static void Main() 
  {
    OracleConnection oraConn = new OracleConnection("Data Source=MyOracleServer;Integrated Security=yes;");

    OracleCommand oraCMD = new OracleCommand("SELECT CUSTOMER_ID, NAME FROM DEMO.CUSTOMER", oraConn);

    oraConn.Open();

    OracleDataReader myReader = oraCMD.ExecuteReader();

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

    myReader.Close();
    oraConn.Close();
  }
}

填充dataset:

SqlClient

[Visual Basic]
Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")

Dim selectCMD As SqlCommand = New SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn)
selectCMD.CommandTimeout = 30

Dim custDA As SqlDataAdapter = New SqlDataAdapter
custDA.SelectCommand = selectCMD

nwindConn.Open()

Dim custDS As DataSet = New DataSet
custDA.Fill(custDS, "Customers")

nwindConn.Close()
[C#]
SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
selectCMD.CommandTimeout = 30;

SqlDataAdapter custDA = new SqlDataAdapter();
custDA.SelectCommand = selectCMD;

nwindConn.Open();

DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");

nwindConn.Close();

OleDb

[Visual Basic]
Dim nwindConn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" & _
                                                       "Integrated Security=SSPI;Initial Catalog=northwind")

Dim selectCMD As OleDbCommand = New OleDbCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn)
selectCMD.CommandTimeout = 30

Dim custDA As OleDbDataAdapter = New OleDbDataAdapter
custDA.SelectCommand = selectCMD

Dim custDS As DataSet = New DataSet
custDA.Fill(custDS, "Customers")
[C#]
OleDbConnection nwindConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" +
                                                "Integrated Security=SSPI;Initial Catalog=northwind");

OleDbCommand selectCMD = new OleDbCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
selectCMD.CommandTimeout = 30;

OleDbDataAdapter custDA = new OleDbDataAdapter();
custDA.SelectCommand = selectCMD;

DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");

Odbc

[Visual Basic]
Dim nwindConn As OdbcConnection = New OdbcConnection("Driver={SQL Server};Server=localhost;" & _
                                                     "Trusted_Connection=yes;Database=northwind")

Dim selectCMD As OdbcCommand = New OdbcCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn)
selectCMD.CommandTimeout = 30

Dim custDA As OdbcDataAdapter = New OdbcDataAdapter
custDA.SelectCommand = selectCMD

nwindConn.Open()

Dim custDS As DataSet = New DataSet
custDA.Fill(custDS, "Customers")

nwindConn.Close()
[C#]
OdbcConnection nwindConn = new OdbcConnection("Driver={SQL Server};Server=localhost;" +
                                              "Trusted_Connection=yes;Database=northwind");

OdbcCommand selectCMD = new OdbcCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
selectCMD.CommandTimeout = 30;

OdbcDataAdapter custDA = new OdbcDataAdapter();
custDA.SelectCommand = selectCMD;

nwindConn.Open();

DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");

nwindConn.Close();



从多个 DataAdapter 填充 DataSet
[C#] SqlConnection custConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind;"); SqlDataAdapter custDA = new SqlDataAdapter("SELECT * FROM Customers", custConn); OleDbConnection orderConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=c:\\Program Files\\Microsoft Office\\Office\\Samples\\northwind.mdb;"); OleDbDataAdapter orderDA = new OleDbDataAdapter("SELECT * FROM Orders", orderConn); custConn.Open(); orderConn.Open(); DataSet custDS = new DataSet(); custDA.Fill(custDS, "Customers"); orderDA.Fill(custDS, "Orders"); custConn.Close(); orderConn.Close(); DataRelation custOrderRel = custDS.Relations.Add("CustOrders", custDS.Tables["Customers"].Columns["CustomerID"], custDS.Tables["Orders"].Columns["CustomerID"]); foreach (DataRow pRow in custDS.Tables["Customers"].Rows) { Console.WriteLine(pRow["CustomerID"]); foreach (DataRow cRow in pRow.GetChildRows(custOrderRel)) Console.WriteLine("\t" + cRow["OrderID"]); }

posted on 2005-11-24 10:33 梦在天涯 阅读(1340) 评论(0)  编辑 收藏 引用 所属分类: C#/.NET


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   博问   Chat2DB   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1797174
  • 排名 - 5

最新评论

阅读排行榜