Advertisement
Guest User

Untitled

a guest
Apr 26th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Data.SqlClient;
  7. using System.Data;
  8.  
  9. namespace ConsoleApp
  10. {
  11. class Program
  12. {
  13.  
  14.  
  15. static void Main(string[] args)
  16. {
  17.  
  18. string conStr = @"Data Source = server1; Initial Catalog = User; Integrated Security = false; User = sa; Password = ghbvf;";
  19.  
  20. SqlConnection sql = new SqlConnection(conStr);
  21. sql.Open();
  22.  
  23. //SqlCommand cmd = new SqlCommand("create table Users(Id int identity not null, Name varchar(50) not null, Age int not null)",sql);
  24. //cmd.ExecuteNonQuery();
  25.  
  26. SqlCommand cmd3 = new SqlCommand("insert Users values('Flint',30);", sql);
  27. cmd3.ExecuteNonQuery();
  28.  
  29.  
  30. SqlCommand cmd1 = new SqlCommand("select * from Users", sql);
  31. SqlDataReader dtread = cmd1.ExecuteReader();
  32.  
  33.  
  34. DataTable table = new DataTable();
  35. table = dtread.GetSchemaTable();
  36.  
  37.  
  38. while (dtread.Read())
  39. {
  40. table.Rows.Add(dtread.GetValue(1));
  41. table.Rows.Add(dtread.GetValue(0));
  42.  
  43. }
  44.  
  45. foreach (DataRow item in table.Rows)
  46. {
  47. for(int i=0; i<item.ItemArray.Length;i++)
  48. {
  49. Console.WriteLine(item.ItemArray[i]);
  50.  
  51. }
  52. for (int i = 0; i < item.ItemArray.Length; i++)
  53. {
  54. Console.WriteLine(item.ItemArray[i]);
  55. }
  56.  
  57. }
  58.  
  59.  
  60. DataTable tb = new DataTable();
  61. tb = dtread.GetSchemaTable();
  62.  
  63.  
  64. Console.ReadLine();
  65.  
  66.  
  67. }
  68. }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement