Sample dataset offline dataset for c

  • 2020-06-15 10:07:21
  • OfStack

c# DataSet Offline data set instance

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 Offline data set 
{
    /// <summary>
    /// Window1.xaml  Interaction logic of 
    /// </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(); It's not implemented, it's implemented new the 1 a adapter To accept the cmd . 

                    //SqlDataAdapter is 1 Give us a hand SqlCommand The results of the query are populated to DataSet In the class 
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);//SqlDataAdapter Need to be 1 A parameter 

                    //DataSet Equivalent to local 1 A complex set ( List<int> ) 
                    DataSet dataset = new DataSet();//DataSet Is the data set 
                    adapter.Fill(dataset);// perform cmd And the SqlCommand The query results are populated to DataSet

                    //DataTable It's a table of data in memory 
                    DataTable table = dataset.Tables[0];// Because it's in the database 1 A table T_Student , so it's [0].
                    DataRowCollection rows = table.Rows;//DataRowCollection is DataTable The collection , Here, rows The row in which the result is queried 
                    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)
        {
            // using ConfigurationManager.ConnectionStrings  attribute , Can only read app.config Configuration information. 
            string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString;
            // Add the project root directory 1 a " Application configuration files ", The name is App.config
            //App.config Add nodes, give add since 1 a name
            // Project add pair System.configuration A reference to the ( To add a development package ,System.Data is ADO.NET The development kit )
            // You can use System.configuration In the ConfigurationManager class 
            //asp.net So theta is going to be theta 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);
                    }
                }
            }
        }
    }
}

Related articles: