Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. var con = new SqlConnection("Data Source=local;Initial Catalog=Test;Integrated Security=True");
  2.  
  3. using (con)
  4. using (var command = new SqlCommand("SELECT col1,col2 FROM some table", con))
  5. {
  6. con.Open();
  7. command.ExecuteNonQuery();
  8. }
  9.  
  10. public class ClassName
  11. {
  12. public string Col1 { get; set; }
  13. public int Col2 { get; set; }
  14. }
  15.  
  16. ClassName[] allRecords = null;
  17. string sql = @"SELECT col1,col2
  18. FROM some table";
  19. using (var command = new SqlCommand(sql, con))
  20. {
  21. con.Open();
  22. using (var reader = command.ExecuteReader())
  23. {
  24. var list = new List<ClassName>();
  25. while (reader.Read())
  26. list.Add(new ClassName { Col1 = reader.GetString(0), Col2 = reader.GetInt32(1) });
  27. allRecords = list.ToArray();
  28. }
  29. }
  30.  
  31. DataTable dt = new DataTable();
  32. using (var con = new SqlConnection("Data Source=local;Initial Catalog=Test;Integrated Security=True"))
  33. {
  34. using (var command = new SqlCommand("SELECT col1,col2" +
  35. {
  36. con.Open();
  37. using (SqlDataReader dr = command.ExecuteReader())
  38. {
  39. dt.Load(dr);
  40. }
  41. }
  42. }
  43.  
  44. SqlDataAdapter da = new SqlDataAdapter(command);
  45. da.Fill(dt);
  46.  
  47. foreach (DataRow dr in dt.Rows)
  48. {
  49. if (dr.Field<string>("col1") == "yourvalue") //your condition
  50. {
  51. }
  52. }
  53.  
  54. try
  55. {
  56. SqlCommand comm = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;",connection);
  57. connection.Open();
  58.  
  59. SqlDataReader reader = comm.ExecuteReader();
  60. List<string> str = new List<string>();
  61. int i=0;
  62. while (reader.Read())
  63. {
  64. str.Add( reader.GetValue(0).ToString() );
  65. }
  66. reader.Close();
  67. }
  68. catch (Exception)
  69. {
  70. throw;
  71. }
  72. finally
  73. {
  74. connection.Close();
  75. }
  76.  
  77. public void PrintSql_Array()
  78. {
  79. int[] numbers = new int[4];
  80. string[] names = new string[4];
  81. string[] secondNames = new string[4];
  82. int[] ages = new int[4];
  83.  
  84. int cont = 0;
  85.  
  86. string cs = @"Server=ADMINSQLEXPRESS; Database=dbYourBase; User id=sa; password=youpass";
  87. using (SqlConnection con = new SqlConnection(cs))
  88. {
  89. using (SqlCommand cmd = new SqlCommand())
  90. {
  91. cmd.Connection = con;
  92. cmd.CommandType = CommandType.Text;
  93. cmd.CommandText = "SELECT * FROM tbl_Datos";
  94. con.Open();
  95.  
  96. SqlDataAdapter da = new SqlDataAdapter(cmd);
  97. DataTable dt = new DataTable();
  98. da.Fill(dt);
  99.  
  100. foreach (DataRow row in dt.Rows)
  101. {
  102. numbers[cont] = row.Field<int>(0);
  103. names[cont] = row.Field<string>(1);
  104. secondNames[cont] = row.Field<string>(2);
  105. ages[cont] = row.Field<int>(3);
  106.  
  107. cont++;
  108. }
  109.  
  110. for (int i = 0; i < numbers.Length; i++)
  111. {
  112. Console.WriteLine("{0} | {1} {2} {3}", numbers[i], names[i], secondNames[i], ages[i]);
  113. }
  114.  
  115. con.Close();
  116. }
  117. }
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement