Advertisement
Ladies_Man

#DB Lab12 ADO.NET COMPLETE

Dec 31st, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 21.00 KB | None | 0 0
  1. --Лабораторная работа №12. Программирование клиентского приложения доступа к данным
  2.  
  3.  
  4. --1.Создать пользовательское приложение с использованием технологии ADO.NET для выполнения следующих операций:
  5.     --•просмотра содержимого таблиц;
  6.     --•вставки, удаления, изменения данных в этих таблицах.
  7. --2.Сравнить на практике использование для некоторой таблицы:
  8.     --•несвязного уровня ADO.NET;
  9.     --•связного уровня ADO.NET.
  10. --3.Для хранения строки подключения к источнику данных использовать конфигурационный файл приложения (app.config).
  11.  
  12.  
  13.  
  14. use master;
  15. go
  16. if DB_ID (N'lab12ado') is null
  17.     create database lab12ado
  18.         on (
  19.             NAME = lab12adodat,
  20.             FILENAME = 'C:\Users\anthony\Documents\DB_Labs\lab12ado\lab12adodat.mdf',
  21.             SIZE = 5, MAXSIZE = UNLIMITED, FILEGROWTH = 5 )
  22.         log on (
  23.             NAME = lab12adolog,
  24.             FILENAME = 'C:\Users\anthony\Documents\DB_Labs\lab12ado\lab12adolog.ldf',
  25.             SIZE = 5, MAXSIZE = 20, FILEGROWTH = 5 );
  26. go
  27.  
  28.  
  29. use lab12ado;
  30. go
  31. if OBJECT_ID(N'dbo.test', N'U') is not null
  32.     drop table dbo.test
  33. go
  34. create table dbo.test (
  35.     id          int PRIMARY KEY,
  36.     name        varchar(35),
  37.     email       varchar(254)
  38.     );
  39. go
  40.  
  41. if OBJECT_ID(N'dbo.books', N'U') is not null
  42.     drop table dbo.books;
  43. go
  44. create table dbo.books (
  45.     title           varchar(254),
  46.     year            int,
  47.     rating          int
  48.         CONSTRAINT DF_books_rating DEFAULT(0),
  49.     );
  50. go
  51.  
  52. insert into dbo.test values
  53.     (1337, 'anthony', 'anthony@mail.address'),
  54.     (2, 'me', 'me@mail.ru'),
  55.     (3, 'george martin', 'gmartin@7k.com'),
  56.     (1007, 'Q', 'Q@SIS.co.uk'),
  57.     (6000, 'sumail', 'su@mail'),
  58.     (8800, '555', '35@35');
  59.  
  60. insert dbo.books values
  61.     ('the lord of the rings', 1954, 10),
  62.     ('the hobbit, or there and back again', 1937, 8),
  63.     ('a game of thrones', 1996, 7),
  64.     ('a dance with dragons', 2011, 9),
  65.     ('1984', 1949, 6),
  66.     ('the martian', 2011, 8),
  67.     ('fahrenheit 451', 1953, 6),
  68.     ('the war of the worlds', 1897, 7),
  69.     ('the da vinci code', 2003, 6);
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76. //app.config
  77. <?xml version="1.0" encoding="utf-8" ?>
  78. <configuration>
  79.     <startup>
  80.         <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  81.     </startup>
  82.  
  83.     <connectionStrings>
  84.       <add name="AdoConnString"
  85.            providerName="System.Data.SqlClient"
  86.            connectionString="Server = (localdb)\u5;
  87.                Initial Catalog = Lab12ado;
  88.                Integrated Security = true"/>
  89.     </connectionStrings>
  90.  
  91. </configuration>
  92.  
  93.  
  94.  
  95. //Program.cs
  96. using System;
  97. using System.Collections.Generic;
  98. using System.Linq;
  99. using System.Text;
  100. using System.Threading.Tasks;
  101. using System.Data.SqlClient;
  102. using System.Configuration;
  103. using System.Data;
  104.  
  105. namespace ADOnet
  106. {
  107.     class Program
  108. {
  109.  
  110.         static int check_tablename(string table, SqlConnection connection) {
  111.             connection.Open();
  112.             DataTable dTable = connection.GetSchema("TABLES", new string[] { null, null, table });
  113.  
  114.             if (dTable.Rows.Count > 0) {
  115.                 connection.Close();
  116.                 return 1;
  117.             }
  118.             Console.WriteLine("table does not exist");
  119.             connection.Close();
  120.             return -1;
  121.         }
  122.  
  123.  
  124.         static int check_colname(string colname, string table, SqlConnection connection)
  125.         {
  126.             connection.Open();
  127.             DataTable dTable = connection.GetSchema("TABLES", new string[] { null, null, table });
  128.  
  129.             if (dTable.Rows.Count > 0) {
  130.  
  131.                 string q = string.Format("select * from {0}", table);
  132.                 SqlCommand command = new SqlCommand(q, connection);
  133.                 SqlDataReader reader = command.ExecuteReader();
  134.                 if (reader.Read()) {
  135.  
  136.                     int fieldCount = reader.FieldCount;
  137.                     string[] fields = new string[fieldCount];
  138.  
  139.                     for (int j = 0; j < fieldCount; j++) {
  140.                         fields[j] = reader.GetName(j);
  141.                     }
  142.  
  143.                     if (fields.Contains<String>(colname)) {
  144.                         reader.Close();
  145.                         connection.Close();
  146.                         return 1;
  147.                     }
  148.                 }
  149.                 reader.Close();
  150.             }
  151.            
  152.             Console.WriteLine("column does not exist");
  153.             connection.Close();
  154.             return -1;
  155.         }
  156.        
  157.  
  158.         static int run(SqlCommand command, SqlConnection connection) {
  159.             try {
  160.                 command.ExecuteNonQuery();
  161.  
  162.             } catch (SqlException e) {
  163.                 Console.WriteLine(e.Message);
  164.                 return -1;
  165.             }
  166.             return 0;
  167.         }
  168.  
  169.  
  170.         static void exec(SqlCommand command) {
  171.             try {
  172.  
  173.                 SqlDataReader reader = command.ExecuteReader();
  174.                 do {
  175.                     while (reader.Read()) {
  176.                         Object[] values = new Object[reader.FieldCount];
  177.                         int fieldCount = reader.GetValues(values);
  178.  
  179.                         for (int i = 0; i < fieldCount; i++) {
  180.                             Console.Write("\t\t{0}", values[i]);
  181.                         }
  182.                         Console.WriteLine();
  183.                     }
  184.  
  185.                 } while (reader.NextResult());
  186.  
  187.                 reader.Close();
  188.  
  189.  
  190.             } catch (Exception e) { Console.WriteLine(e.Message); }
  191.  
  192.             Console.WriteLine();
  193.         }
  194.  
  195.  
  196.         static void PrintTable(DataTable dt) {
  197.             DataTableReader dtReader = dt.CreateDataReader();
  198.  
  199.             while (dtReader.Read()) {
  200.                 for (int i = 0; i < dtReader.FieldCount; i++) {
  201.                     Console.Write("\t{0}", dtReader.GetValue(i).ToString());
  202.                 }
  203.                 Console.WriteLine();
  204.             }
  205.             dtReader.Close();
  206.         }
  207.  
  208. //---------------------------------------------------------
  209. //--------------------------MAIN---------------------------
  210. //---------------------------------------------------------
  211.  
  212.  
  213.         static void Main(string[] args)
  214.         {
  215.             try
  216.             {
  217.  
  218.  
  219.  
  220.                 SqlConnection connection;
  221.  
  222.                 try {
  223.                     string connectionString = ConfigurationManager.ConnectionStrings["AdoConnString"].ConnectionString;
  224.                     connection = new SqlConnection(connectionString);
  225.                 }
  226.                 catch (Exception e) { Console.WriteLine(e.Message); Console.ReadLine(); return; }
  227.  
  228.                 Console.WriteLine("Connection has been opened.");
  229.                 Console.WriteLine("Cвязный уровень");
  230.                 Console.WriteLine("type 'help' for help");
  231.  
  232.                 string com;
  233.  
  234.                 while (true)
  235.                 {
  236.                     string table, column, q;
  237.                     Console.Write(">");
  238.                     com = Console.ReadLine();
  239.  
  240.                     if (com.Equals(""))
  241.                         continue;
  242. //=========================================================
  243. //------------------------SELECT---------------------------
  244. //=========================================================
  245.                     if ('s' == com[0])
  246.                     {
  247.                         Console.Write("->tables | * | column:");
  248.                         string subcom = Console.ReadLine();
  249.  
  250.                         if (subcom.Equals(""))
  251.                             continue;
  252.  
  253.                         if ('t' == subcom[0])
  254.                         {
  255.                             q = "select * from INFORMATION_SCHEMA.TABLES";
  256.                             connection.Open();
  257.                             SqlCommand cmd = new SqlCommand(q, connection);
  258.  
  259.                             exec(cmd);
  260.                             connection.Close();
  261.                         }
  262.  
  263.                         if ('*' == subcom[0])
  264.                         {
  265.                             Console.Write("->tablename:");
  266.                             table = Console.ReadLine();
  267.                             if (-1 == check_tablename(table, connection))
  268.                                 continue;
  269.  
  270.                             connection.Open();
  271.                             q = string.Format("select * from {0}", table);
  272.                             SqlCommand cmd = new SqlCommand(q, connection);
  273.  
  274.                             exec(cmd);
  275.                             connection.Close();
  276.                         }
  277.  
  278.                         if ('c' == subcom[0])
  279.                         {
  280.                             Console.Write("->tablename:");
  281.                             table = Console.ReadLine();
  282.                             if (-1 == check_tablename(table, connection))
  283.                                 continue;
  284.  
  285.                             Console.Write("-->column name:");
  286.                             column = Console.ReadLine();
  287.                             if (-1 == check_colname(column, table, connection))
  288.                                 continue;
  289.  
  290.                             connection.Open();
  291.                             q = string.Format("select {0} from {1}", column, table);
  292.                             SqlCommand cmd = new SqlCommand(q, connection);
  293.  
  294.                             exec(cmd);
  295.                             connection.Close();
  296.                         }
  297.  
  298.                         if (subcom.Equals("q"))
  299.                             continue;
  300.                     }
  301.  
  302.  
  303. //=========================================================
  304. //------------------------INSERT---------------------------
  305. //=========================================================
  306.                     if ('i' == com[0])
  307.                     {
  308.                         Console.Write("->tablename:");
  309.                         table = Console.ReadLine();
  310.                         if (-1 == check_tablename(table, connection))
  311.                             continue;
  312.  
  313.                         connection.Open();
  314.                         q = string.Format("select * from {0}", table);
  315.                         SqlCommand command = new SqlCommand(q, connection);
  316.                         SqlDataReader reader = command.ExecuteReader();
  317.  
  318.                         //get column names as result of simple <select*> query
  319.                         if (reader.Read())
  320.                         {
  321.  
  322.                             int fieldCount = reader.FieldCount;
  323.  
  324.                             //form query string
  325.                             q = string.Format("insert into {0} (", table);
  326.                             for (int j = 0; j < fieldCount; j++)
  327.                             {
  328.                                 q += reader.GetName(j) + ", ";
  329.                             }
  330.                             q = q.Remove(q.Length - 2);
  331.                             q += ") values (";
  332.                             for (int j = 0; j < fieldCount; j++)
  333.                             {
  334.                                 q += "@" + reader.GetName(j) + ", ";
  335.                             }
  336.                             q = q.Remove(q.Length - 2);
  337.                             q += ")";
  338.                             Console.WriteLine("Q:" + q);
  339.  
  340.  
  341.                             SqlCommand cmd = new SqlCommand(q, connection);
  342.  
  343.                             //get parameters and add to command
  344.                             for (int j = 0; j < fieldCount; j++) {
  345.                                 Console.Write("-->{0}[{1}]:", reader.GetName(j), reader.GetDataTypeName(j));
  346.                                 Object value = Console.ReadLine();
  347.  
  348.                                 cmd.Parameters.AddWithValue("@" + reader.GetName(j), value);
  349.                             }
  350.  
  351.                             reader.Close();
  352.  
  353.                             if (0 == run(cmd, connection))
  354.                                 Console.WriteLine("entry has been inserted");
  355.  
  356.                         } else {
  357.                             reader.Close();
  358.                         }
  359.                         connection.Close();
  360.                     }
  361.  
  362.  
  363. //=========================================================
  364. //------------------------DELETE---------------------------
  365. //=========================================================
  366.                     if ('d' == com[0])
  367.                     {
  368.                         Console.Write("->tablename:");
  369.                         table = Console.ReadLine();
  370.                         if (-1 == check_tablename(table, connection))
  371.                             continue;
  372.  
  373.                         connection.Open();
  374.                         q = string.Format("select * from {0}", table);
  375.                         SqlCommand command = new SqlCommand(q, connection);
  376.                         SqlDataReader reader = command.ExecuteReader();
  377.  
  378.                         if (reader.Read())
  379.                         {
  380.  
  381.                             Object[] del_values = new Object[reader.FieldCount];//array of values to delete
  382.                             int[] field_indices = new int[reader.FieldCount];//array of their indices in table
  383.                             int delete_col_num = 0, fieldCount = reader.FieldCount;
  384.  
  385.                             q = string.Format("delete {0} where ", table);
  386.  
  387.                             for (int j = 0; j < fieldCount; j++)
  388.                             {
  389.                                 Console.Write("-->del by {0}? Y/N:", reader.GetName(j));
  390.                                 string t_com = Console.ReadLine();
  391.  
  392.                                 if ('n' == t_com[0] || 'N' == t_com[0]) {
  393.                                     continue;
  394.                                 }
  395.                                 else {
  396.                                     Console.Write("-->{0}[{1}] value:", reader.GetName(j), reader.GetDataTypeName(j));
  397.                                     del_values[delete_col_num] = Console.ReadLine();
  398.  
  399.                                     field_indices[delete_col_num] = j;
  400.                                     q += reader.GetName(j) + " = @" + reader.GetName(j) + " and ";
  401.                                     delete_col_num++;
  402.                                 }
  403.                             }
  404.  
  405.                             if (0 == delete_col_num)
  406.                             {
  407.                                 Console.WriteLine("delete parameters are missing");
  408.                                 reader.Close();
  409.                                 connection.Close();
  410.                                 continue;
  411.                             }
  412.                             q = q.Remove(q.Length - 5);
  413.                             Console.WriteLine("Q:" + q);
  414.  
  415.  
  416.  
  417.                             SqlCommand cmd = new SqlCommand(q, connection);
  418.  
  419.                             for (int k = 0; k < delete_col_num; k++) {
  420.                                 cmd.Parameters.AddWithValue("@" + reader.GetName(field_indices[k]), del_values[k]);
  421.                             }
  422.                             reader.Close();
  423.  
  424.                             if (0 == run(cmd, connection))
  425.                                 Console.WriteLine("entry(-ies) has been deleted");
  426.  
  427.                         } else {
  428.                             reader.Close();
  429.                         }
  430.                         connection.Close();
  431.                     }
  432.  
  433.  
  434. //=========================================================
  435. //------------------------UPDATE---------------------------
  436. //=========================================================
  437.                     if ('u' == com[0])
  438.                     {
  439.                         Console.Write("->tablename:");
  440.                         table = Console.ReadLine();
  441.                         if (-1 == check_tablename(table, connection))
  442.                             continue;
  443.  
  444.                         connection.Open();
  445.                         q = string.Format("select * from {0}", table);
  446.                         SqlCommand command = new SqlCommand(q, connection);
  447.                         SqlDataReader reader = command.ExecuteReader();
  448.  
  449.                         if (reader.Read())
  450.                         {
  451.                             Object[] old_values = new Object[reader.FieldCount];
  452.                             int[] old_field_indices = new int[reader.FieldCount];//array of their indices in table
  453.                             Object[] new_values = new Object[reader.FieldCount];
  454.                             int[] new_field_indices = new int[reader.FieldCount];//array of their indices in table
  455.                             int new_col_num = 0, old_col_num = 0, fieldCount = reader.FieldCount;
  456.  
  457.                             q = string.Format("update {0} set ", table);
  458.  
  459.                             for (int j = 0; j < fieldCount; j++) {
  460.                                 Console.Write("-->modify {0}? Y/N:", reader.GetName(j));
  461.                                 string t_com = Console.ReadLine();
  462.  
  463.                                 if ('n' == t_com[0] || 'N' == t_com[0]) {
  464.                                     continue;
  465.                                 }
  466.                                 else {
  467.                                     Console.Write("-->{0}[{1}] value:", reader.GetName(j), reader.GetDataTypeName(j));
  468.                                     new_values[new_col_num] = Console.ReadLine();
  469.                                     new_field_indices[new_col_num] = j;
  470.                                     q += reader.GetName(j) + " = @new" + reader.GetName(j) + ", ";
  471.                                     new_col_num++;
  472.                                 }
  473.                             }
  474.                             q = q.Remove(q.Length - 2);
  475.  
  476.                             q += " where ";
  477.                             for (int j = 0; j < fieldCount; j++) {
  478.                                 Console.Write("-->by {0}? Y/N:", reader.GetName(j));
  479.                                 string t_com = Console.ReadLine();
  480.  
  481.                                 if ('n' == t_com[0] || 'N' == t_com[0]) {
  482.                                     continue;
  483.                                 }
  484.                                 else {
  485.                                     Console.Write("-->{0}[{1}] value:", reader.GetName(j), reader.GetDataTypeName(j));
  486.                                     old_values[old_col_num] = Console.ReadLine();
  487.                                     old_field_indices[old_col_num] = j;
  488.                                     q += reader.GetName(j) + " = @old" + reader.GetName(j) + " and ";
  489.                                     old_col_num++;
  490.                                 }
  491.                             }
  492.                             q = q.Remove(q.Length - 5);
  493.                             Console.WriteLine("Q:" + q);
  494.  
  495.                             if (0 == new_col_num || 0 == old_col_num)
  496.                             {
  497.                                 Console.WriteLine("delete or update parameters are missing");
  498.                                 reader.Close();
  499.                                 connection.Close();
  500.                                 continue;
  501.                             }
  502.  
  503.  
  504.  
  505.                             SqlCommand cmd = new SqlCommand(q, connection);
  506.  
  507.                             for (int k = 0; k < new_col_num; k++) {
  508.                                 cmd.Parameters.AddWithValue("@new" + reader.GetName(new_field_indices[k]), new_values[k]);
  509.                             }
  510.                             for (int k = 0; k < old_col_num; k++) {
  511.                                 cmd.Parameters.AddWithValue("@old" + reader.GetName(old_field_indices[k]), old_values[k]);
  512.                             }
  513.  
  514.                             reader.Close();
  515.  
  516.                             if (0 == run(cmd, connection))
  517.                                 Console.WriteLine("entry(-ies) has been updated");
  518.  
  519.                         } else {
  520.                             reader.Close();
  521.                         }
  522.                         connection.Close();
  523.                     }
  524.  
  525.  
  526. //=========================================================
  527. //------------------------EXPLICIT-------------------------
  528. //=========================================================
  529.                     if ('e' == com[0])
  530.                     {
  531.                         connection.Open();
  532.                        
  533.                         Console.Write("->");
  534.                         q = Console.ReadLine();
  535.                         SqlCommand cmd = new SqlCommand(q, connection);
  536.  
  537.                         exec(cmd);
  538.  
  539.                         connection.Close();
  540.                     }
  541.  
  542.                     if ('h' == com[0])
  543.                     {
  544.                         Console.WriteLine("Available commands:");
  545.                         Console.Write("\tselect\n\t\ttables\n\t\t*\n\t\t\t<table_name>\n\t\tcolumn\n\t\t\t<table_name>\n\t\t\t\t<column_name>\n");
  546.                         Console.Write("\tinsert\n\t\t<table_name>\n\t\t\t<value_string>\n");
  547.                         Console.Write("\tdelete\n\t\t<table_name>\n\t\t\t<by-fields>\n");
  548.                         Console.Write("\tupdate\n\t\t<table_name>\n\t\t\t<fields to modify>\n\t\t\t\t<by-fields>\n");
  549.                         Console.WriteLine("\texplicit\n\t\t<explicit_query>\n");
  550.                         Console.WriteLine("\tПерейти на Несвязный уроень / Выйти = [q]");
  551.                     }
  552.  
  553.                     if (com.Equals("q"))
  554.                         break;
  555.  
  556.                 }
  557.  
  558.                 Console.Write("Выйти = [q]\nПерейти на Несвзный уровень = [Any_key]\nПерейти?:");
  559.                 com = Console.ReadLine();
  560.  
  561.                 if (com.Equals("q") && !com.Equals(""))
  562.                 {
  563.                     Console.WriteLine("Closing connection. App will shut down soon.");
  564.                     if (null != connection && connection.State == ConnectionState.Open)
  565.                     {
  566.                         connection.Close();
  567.                     }
  568.                    
  569.                     return;
  570.                 }
  571.                 Console.Write("\n\nНесвязный уровень\n");
  572.                 Console.WriteLine("type 'help' for help");
  573.  
  574.  
  575.  
  576.  
  577.  
  578.                 //несвязный уровень
  579.  
  580.                 DataSet cds = new DataSet("cds");
  581.  
  582.                 SqlDataAdapter adapter = new SqlDataAdapter("select * from test", connection);
  583.                 SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
  584.  
  585.                 adapter.Fill(cds, "test");
  586.  
  587.                 DataTable dt = cds.Tables["test"];
  588.                
  589.                 //table must have primary key for delete/update queries
  590.                 try {
  591.                     DataColumn[] primary_key = new DataColumn[1];
  592.                     primary_key[0] = cds.Tables["test"].Columns["id"];
  593.                     dt.PrimaryKey = primary_key;
  594.                 }
  595.                 catch (Exception e) { Console.WriteLine(e.Message); }
  596.                
  597.  
  598.  
  599.  
  600.                 while (true)
  601.                 {
  602.  
  603.                     Console.Write(">");
  604.                     com = Console.ReadLine();
  605.  
  606.                     if (com.Equals(""))
  607.                         continue;
  608. //=========================================================
  609. //------------------------SELECT---------------------------
  610. //=========================================================
  611.                     if ('s' == com[0])
  612.                     {
  613.                         Console.WriteLine("select * from test");
  614.                         PrintTable(cds.Tables["test"]);
  615.                     }
  616.  
  617. //=========================================================
  618. //------------------------INSERT---------------------------
  619. //=========================================================
  620.                     if ('i' == com[0])
  621.                     {
  622.                         Console.Write("->id:");
  623.                         int tid;
  624.                         try
  625.                         {
  626.                             tid = Convert.ToInt32(Console.ReadLine());
  627.                         }
  628.                         catch (Exception e) { Console.WriteLine(e.Message); continue; }
  629.  
  630.                         Console.Write("->name:");
  631.                         string tname = Console.ReadLine();
  632.                         Console.Write("->email:");
  633.                         string tmail = Console.ReadLine();
  634.  
  635.                         DataRow row = dt.NewRow();
  636.                         row[0] = tid;
  637.                         row[1] = tname;
  638.                         row[2] = tmail;
  639.  
  640.                         try { dt.Rows.Add(row); }
  641.                         catch (Exception e) { Console.WriteLine(e.Message); continue; }
  642.  
  643.                         Console.WriteLine("row has been added");
  644.                     }
  645.  
  646. //=========================================================
  647. //------------------------DELETE---------------------------
  648. //=========================================================
  649.                     if ('d' == com[0])
  650.                     {
  651.                         Console.Write("->by PrimaryKey id:");
  652.                         DataRow row = null;
  653.  
  654.                         try
  655.                         {
  656.                             int tid = Convert.ToInt32(Console.ReadLine());
  657.                             row = dt.Rows.Find(tid);
  658.                         }
  659.                         catch (Exception e) { Console.WriteLine(e.Message); continue; }
  660.  
  661.                         try { row.Delete(); }
  662.                         catch (Exception e) { Console.WriteLine(e.Message); continue; }
  663.  
  664.                         Console.WriteLine("row has been deleted");
  665.                     }
  666.  
  667. //=========================================================
  668. //------------------------UPDATE---------------------------
  669. //=========================================================
  670.                     if ('u' == com[0])
  671.                     {
  672.  
  673.                         Console.Write("->new email:");
  674.                         string newmail = Console.ReadLine();
  675.  
  676.                         int tid, indx;
  677.                         DataRow row = null;
  678.  
  679.                         Console.Write("->by id:");
  680.                         try
  681.                         {
  682.                             tid = Convert.ToInt32(Console.ReadLine());
  683.                             row = dt.Rows.Find(tid);
  684.                             indx = dt.Rows.IndexOf(row);
  685.                         }
  686.                         catch (Exception e) { Console.WriteLine(e.Message); continue; }
  687.  
  688.                         try { dt.Rows[indx]["email"] = newmail; }
  689.                         catch (Exception e) { Console.WriteLine(e.Message); continue; }
  690.  
  691.                         Console.WriteLine("row has been updated");
  692.                         continue;
  693.                     }
  694.  
  695. //=========================================================
  696. //---------------------ACCEPT-CHANGES----------------------
  697. //=========================================================
  698.                     if ('a' == com[0])
  699.                     {
  700.                         adapter.Update(cds, dt.TableName);
  701.                         Console.WriteLine("changes have been accepted");
  702.                     }
  703.  
  704.  
  705.                     if ('h' == com[0])
  706.                     {
  707.                         Console.WriteLine("Available commands:");
  708.                         Console.Write("\tselect\n\t\t* from 'test'\n");
  709.                         Console.Write("\tinsert\n\t\tinto 'test'\n\t\t\t<value_string>\n");
  710.                         Console.Write("\tdelete\n\t\t'test' by\n\t\t\t<primary_key>\n");
  711.                         Console.Write("\tupdate\n\t\t'test' set 'emai'\n\t\t\t<new_email>\n\t\t\t\tby\n\t\t\t\t\t<primary_key>\n");
  712.                         Console.WriteLine("\tAccept changes to table = [a]\n");
  713.                         Console.Write("\tExit = [q]\n");
  714.                     }
  715.  
  716.                     if (com.Equals("q"))
  717.                         break;
  718.                 }
  719.  
  720.                 Console.WriteLine("Closing connection. App will shut down soon.");
  721.                 connection.Close();
  722.  
  723.         } catch(Exception e) {
  724.             Console.WriteLine(e.Message);
  725.             Console.ReadLine();
  726.             return;
  727.         }
  728.    
  729.         }
  730.  
  731.     }
  732.  
  733. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement