C #. Net simple example for reading sqlserver2000 Database

Source: Internet
Author: User
I started to learn C # by myself. I learned how to connect to and query databases yesterday and Code Put it here for fear of forgetting it ~

Components used:
System. Data. sqlclient. sqlconnection
System. Data. sqlclient. sqldataadapter
System. Data. sqlclient. sqlcommand
System. Data. Dataset
Note:
Sqlconnection
Sqlcommand execution Query
Sqldataadapter uses sqlcommand to obtain the result. The result is stored in dataset.

Configuration code: (automatically generated by the system or manually written)
...
This. sqlconnection1.connectionstring = ""; // write the connection string
...
This. sqldataadapter1.selectcommand = This. sqlselectcommand1; // specify the Association
...
This. sqlselectcommand1.commandtext = "select [user], password from m_system"; // query statement
...
The configuration part is better for the system to generate by itself (at least for beginners like me, I am dizzy)

Execute some code:
Try // catch exceptions
{
Sqlconnection1.open (); // you must first connect
DS = new dataset (); // instantiate a dataset
Sqldataadapter1.fill (DS, "m_system"); // query
Textbox1.text = Ds. Tables ["m_system"]. Rows [0] [0]. tostring (); // obtain the result
Textbox2.text = Ds. Tables ["m_system"]. Rows [0] [1]. tostring ();
}
Catch (exception exp)
{
MessageBox. Show (this, exp. tostring ());
}
The table name must be specified in the preceding tables. The rows two-dimensional array specifies the column and column coordinates. I only query one row and two columns.
It looks simple to execute a small amount of code.

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.