欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#的dataset离线数据集示例

程序员文章站 2023-12-19 13:50:16
c# dataset离线数据集实例复制代码 代码如下:using system;using system.collections.generic;using system....
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);
                    }
                }
            }
        }
    }
}

上一篇:

下一篇: