Data retrieval method from data sources

Source: Internet
Author: User
I am a beginner at C #, and I feel a little scared to get data from the data bank. Although I was a little scared, I still tried to learn it! Today, I made a small program to export data from data sources to the console.

Using System. Data. sqlclient;
Static   Void Main ( String [] ARGs)
{
# Region Connected data centers
Sqlconnection dataconnection =   New Sqlconnection ();
String Username =   " SA " ;
String Password =   " Sh2_123 " ;
Dataconnection. connectionstring =   " User ID = "   + Username +   " ; Password = "   + Password + " ; Initial catalog = northwind; "   +   " Data Source = ccm02 \ sqlexpress " ;
Dataconnection. open ();
# Endregion

# Region Begin row sentence
Sqlcommand datacommand =   New Sqlcommand ();
Datacommand. Connection = Dataconnection;
Datacommand. commandtext =   " Select orderid, orderdate, shippeddate, shipname, shipaddress, shipcity, shipcountry from orders where customerid = 'blonp' " ;
Sqldatareader datareader = Datacommand. executereader ();
# Endregion

# Region Obtain information
While (Datareader. Read ())
{
# Region Define the change to the nearest bit
Int Orderid = Datareader. getint32 ( 0 );
Datetime orderdate = Datareader. getdatetime ( 1 );
Datetime shipdate = Datareader. getdatetime ( 2 );
String Shipname = Datareader. getstring ( 3 );
String Shipaddress = Datareader. getstring ( 4 );
String Shipcity = Datareader. getstring ( 5 );
String Shipcountry = Datareader. getstring ( 6 );
# Endregion

Console. writeline (
" Order {0} \ n placed {1} \ n shipped {2} \ n "   +
" To address {3} \ n {4} \ n {5} \ n {6} \ n " ,
Orderid, orderdate, shipdate, shipname, shipaddress, shipcity, shipcountry );
}
# Endregion
}

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.