C # Native Connection database and how to manipulate the database __ database

Source: Internet
Author: User

Reprint please indicate the source:

http://blog.csdn.net/gane_cheng/article/details/52233151

Http://www.ganecheng.tech/blog/52233151.html (better browsing effect)

Recently wanted to use C # to connect the SQL Server database, a search of information on the web, found that most of the LINQ. But I just want to simply connect the database, read the data, use the release of this connection, do not want to complete a heap of ORM class files to the project, after consulting the relevant information, summarized the following methods.

First you need to introduce the namespace in which the database connection resides.

Using System.Data.SqlClient;
Using System.Data;

You can then implement it in your code.

Generally we connect a database, need to know four things, the database server's IP address, database name, database username and password.

sqlconnection conn = null;
SqlDataReader dr = null;
SqlCommand com = null;
    try {//Construct database connection string, use IP address, database name, username, password replace XXX string connstr= "server=x.x.x.x;database=xxx;user=xxx;pwd=xxx";
    conn = new SqlConnection (CONNSTR); Conn.

    Open ();
    Write SQL statement String sqltext = "SELECT * FROM table";
    com = new SqlCommand (SQLTEXT, conn); Dr = Com.
    ExecuteReader (); if (Dr.
    HasRows = = false) {return; } Dr.
    Read (); MessageBox.Show (Dr[0].
ToString ()); The catch (System.Exception ex) {return;} finally {if (Dr!= null) {//Shut down SqlDataReader Dr.
    Close (); } if (conn!= null) {//CLOSE database Conn.
    Close (); }
}

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.