Read the data from the database and output

Source: Internet
Author: User
Tags string format

  1. Using System;
  2. Using System.Collections.Generic;
  3. Using System.Linq;
  4. Using System.Text;
  5. Using System.Data.SqlClient;
  6. Namespace Login
  7. {
  8. Class Program
  9. {
  10. static void Main (string[] args)
  11. {
  12. //Create a new database connection
  13. using (SqlConnection conn = new SqlConnection (Getconnectstring ()))
  14. {
  15. Conn. Open (); //Open Database
  16. //console.writeline ("Database open successfully!");
  17. //CREATE DATABASE command
  18. SqlCommand cmd = conn. CreateCommand ();
  19. //Create query Statements
  20. Cmd.commandtext = "SELECT * from UserInfo";
  21. //Read data stream from the database into reader
  22. SqlDataReader reader = cmd. ExecuteReader ();
  23. //Read the next line of data from reader, if there is no data, reader. Read () returns flase
  24. While (reader. Read ())
  25. {
  26. //reader. GetOrdinal ("id") is the index of the column in which the ID is obtained .
  27. //reader. GetInt32 (int n) This is the data in the nth column returned in the format Int32
  28. //reader. GetString (int n) This is the data in column n is returned in string format
  29. int id = reader. GetInt32 (reader.  GetOrdinal ("id"));
  30. String name = Reader. GetString (reader.  GetOrdinal ("name"));
  31. string pwd = reader. GetString (reader.  GetOrdinal ("password"));
  32. int age = Reader. GetInt32 (reader.  GetOrdinal ("age"));
  33. string sex = reader. GetString (reader.  GetOrdinal ("Sex"));
  34. string phone = reader. GetString (reader.  GetOrdinal ("Phone"));
  35. string address = reader. GetString (reader.  GetOrdinal ("Address"));
  36. //Format output data
  37. Console.Write ("id:{0},name:{1},pwd:{2},age:{3},sex:{4},phone{5},address:{6}\n", ID, Name, PWD, age, Sex, Phone,  address);
  38. }
  39. }
  40. Console.readkey ();
  41. }
  42. //Get a database connection string
  43. static string getconnectstring ()
  44. {
  45. return "Data source= (local); Initial catalog=db1;integrated security=sspi;";
  46. }
  47. }
  48. }

Read data from the database and output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.