在没有数据集的情况下使用数据表
生活随笔
收集整理的這篇文章主要介紹了
在没有数据集的情况下使用数据表
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用數據適配器填充數據表
View Code using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient;namespace PopDataTable {class Program{static void Main(string[] args){string connString = @"server =.;integrated security =true;database =northwind";string sql = @"select productname,unitprice fromproducts where unitprice<20";SqlConnection conn = new SqlConnection(connString);try{conn.Open();SqlDataAdapter da = new SqlDataAdapter(sql, conn);DataTable dt = new DataTable();da.Fill(dt);foreach (DataRow row in dt.Rows){foreach (DataColumn col in dt.Columns)Console.WriteLine(row[col]);Console.WriteLine("".PadLeft(20, '='));}}catch (Exception e){Console.WriteLine("Error: " + e);}finally{conn.Close();}Console.ReadKey();}} }示例說明
本示例中沒有創建數據集
DataSet ds = new DataSet();
而是創建了一個數據表:
DataTable dt=new DataTable();
并且沒有填充數據集:
da.Fill(ds,"products");
而是填充了數據表
da.Fill(dt);
因為數據表只可以保存一個表,所以需要注意Fill方法沒有接受數據表名作為參數。由于不需要在數據集中查找特定的數據表,因此不需要如下代碼:
dataTable dt=ds.Tables["products"];
提示:除非確實需要在數據庥中組織數據表以定義它們之間的關系,否則使用一個或多個數據表而不是一個或多個數據集更容易編寫代碼,并且占用較少的運行資源。
總結
以上是生活随笔為你收集整理的在没有数据集的情况下使用数据表的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: WebCombo
- 下一篇: 如何解决padding标记在ie7、ie