c#的dataset离线数据集示例

所属分类: 软件编程 / C#教程 阅读数: 63
收藏 0 赞 0 分享
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);
                    }
                }
            }
        }
    }
}
更多精彩内容其他人还在看

C# SendInput 模拟鼠标操作的实现方法

C# SendInput 模拟鼠标操作的实现方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中 paint()与Onpaint()的区别

paint是事件onpaint方法onpaint方法是调用paint事件的,用哪一个,效果是一样,就看那一个方便了内部是这样实现的:
收藏 0 赞 0 分享

c#中GetType()与Typeof()的区别

c#中GetType()与Typeof()的区别,需要的朋友可以参考一下
收藏 0 赞 0 分享

将字符串转换成System.Drawing.Color类型的方法

将字符串转换成System.Drawing.Color类型的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

C# 抓取网页内容的方法

C# 抓取网页内容的方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于C#后台调用跨域MVC服务及带Cookie验证的实现

本篇文章介绍了,基于C#后台调用跨域MVC服务及带Cookie验证的实现。需要的朋友参考下
收藏 0 赞 0 分享

使用C#获取远程图片 Form用户名与密码Authorization认证的实现

本篇文章介绍了,使用C#获取远程图片 Form用户名与密码Authorization认证的实现。需要的朋友参考下
收藏 0 赞 0 分享

Winform跨线程操作的简单方法

线程间操作无效:从不是创建控件“label1”的线程访问它
收藏 0 赞 0 分享

C# WINFORM 强制让窗体获得焦点的方法代码

C# WINFORM 强制让窗体获得焦点的方法代码,需要的朋友可以参考一下
收藏 0 赞 0 分享

C#中方括号[]的语法及作用介绍

C#中方括号[]可用于数组,索引、属性,更重要的是用于外部DLL类库的引用。
收藏 0 赞 0 分享
查看更多