2010年12月7日 星期二

無連線使用ADO.NET

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace sql运用
{
class Program
{
static void Main(string[] args)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Northwind;Integrated Security=True");
con.Open();

DataSet dataset = new DataSet();

SqlDataAdapter adapter1 = new SqlDataAdapter("select * From Customers ",con);
adapter1.Fill(dataset, "customers"); //建立一个customers表。并把它放入dataset中
SqlDataAdapter adapter2 = new SqlDataAdapter("select * From Orders ", con);
adapter2.Fill(dataset, "orders");

foreach (DataRow row in dataset.Tables["customers"].Rows)
{
Console.WriteLine(row[0] + " " + row[1] + " " + row[2]);
}
Console.Read();
foreach (DataRow row in dataset.Tables["orders"].Rows)
{
Console.WriteLine(row[0] + " " + row[1] + " " + row[2]);
}

Console .Read();
Console.Read();
}
}
}

沒有留言:

張貼留言