VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#教程之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);
                    }
                }
            }
        }
    }
}

相关教程