C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > dataset数据集

c#的dataset离线数据集示例

作者:

这篇文章主要介绍了c#的dataset离线数据集示例,需要的朋友可以参考下
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);
                    }
                }
            }
        }
    }
}
您可能感兴趣的文章:
阅读全文