Create an In-Memory Cache - CSharp Database

CSharp examples for Database:SQL Command

Description

Create an In-Memory Cache

Demo Code


using System;/*from   w  w w .j ava  2s  .c o m*/
using System.Data;
using System.Data.SqlClient;

class MainClass
    {
        static void Main(string[] args)
        {

            using (SqlConnection con = new SqlConnection())
            {
                con.ConnectionString = @"Data Source = .\sqlexpress;" + "Database = Northwind; Integrated Security=SSPI";

                con.Open();
                string query = "SELECT * from Region";
                DataSet dataset = new DataSet();
                SqlDataAdapter adapter = new SqlDataAdapter(query, con);
                SqlCommandBuilder commbuilder = new SqlCommandBuilder(adapter);
                adapter.Fill(dataset);
                DataTable table = dataset.Tables[0];
                foreach (DataColumn col in table.Columns)
                {
                    Console.WriteLine("Column: {0} Type: {1}", col.ColumnName, col.DataType);
                }
                foreach (DataRow row in table.Rows)
                {
                    Console.WriteLine("Data {0} {1}", row[0], row[1]);
                }

                DataRow newrow = table.NewRow();
                newrow["RegionID"] = 5;
                newrow["RegionDescription"] = "Central";
                table.Rows.Add(newrow);

                table.Rows[0]["RegionDescription"] = "North Eastern";

                Console.WriteLine("\nData in (modified) table");
                foreach (DataRow row in table.Rows)
                {
                    Console.WriteLine("Data {0} {1}", row[0], row[1]);
                }

                adapter.Update(dataset);
            }
        }
    }

Result


Related Tutorials