国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#的dataset離線數據集示例

2020-01-24 02:43:47
字體:
來源:轉載
供稿:網友
c# DataSet離線數據集實例
復制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
using System.Data; using System.Configuration;

namespace _03.DataSet離線數據集
{
    /// <summary>
    /// Window1.xaml 的交互邏輯
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }

        private void btnDS_Click(object sender, RoutedEventArgs e)
        {
            using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from T_Student where age<@age";
                    cmd.Parameters.Add(new SqlParameter("@age", 60));
                    //cmd.ExecuteReader();并沒有執行,而是new了一個adapter來接受cmd。

                    //SqlDataAdapter是一個幫我們把SqlCommand的查詢結果填充到DataSet中的類
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);//SqlDataAdapter需要一個參數

                    //DataSet相當于本地的一個復雜集合(List<int>)
                    DataSet dataset = new DataSet();//DataSet是數據集
                    adapter.Fill(dataset);//執行cmd并且把SqlCommand查詢結果填充到DataSet

                    //DataTable是內存中的數據表
                    DataTable table = dataset.Tables[0];//因為數據庫中就一個表T_Student,所以就是[0].
                    DataRowCollection rows = table.Rows;//DataRowCollection是DataTable行的集合,這里的rows指查詢結果的行
                    for (int i = 0; i < rows.Count; i++)
                    {
                        DataRow row = rows[i];
                        int age = (int)row["Age"];
                        string name=(string)row["Name"];
                        MessageBox.Show(name+","+age);
                    }
                }
            }
        }

        private void btnDSS_Click(object sender, RoutedEventArgs e)
        {
            //采用ConfigurationManager.ConnectionStrings 屬性,只能讀取到app.config的配置信息。
            string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
            //項目根目錄添加一個"應用程序配置文件",名字是App.config
            //App.config加節點,給add起一個name
            //項目添加對System.configuration的引用(理解為添加開發包,System.Data就是ADO.NET的開發包)
            //就能使用System.configuration里的ConfigurationManager類
            //asp.net里就變成了Web.config

            MessageBox.Show(connStr);
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = "select * from T_Student where age<@age";
                    cmd.Parameters.Add(new SqlParameter("@age",21));

                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataset = new DataSet();
                    adapter.Fill(dataset);

                    DataTable table=dataset.Tables[0];
                    DataRowCollection rows = table.Rows;
                    for(int i=0;i<rows.Count;i++)
                    {
                        DataRow row=rows[i];
                        string hobbit=(string)row["Hobbit"];
                        MessageBox.Show(hobbit);
                    }
                }
            }
        }
    }
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 梧州市| 台江县| 宁安市| 峨山| 黎城县| 天等县| 周宁县| 彭州市| 伽师县| 白城市| 红桥区| 买车| 巴彦淖尔市| 乌兰浩特市| 荥经县| 宁阳县| 呼和浩特市| 临海市| 肃宁县| 江油市| 额济纳旗| 邓州市| 水富县| 安国市| 即墨市| 保德县| 宜春市| 黔西县| 莱芜市| 时尚| 洛川县| 沐川县| 巫溪县| 昆山市| 崇信县| 上林县| 石河子市| 峡江县| 城口县| 策勒县| 宁蒗|