C #/SQL Server database connection problems

Source: Internet
Author: User

Recently connected to the database, it summarizes the // This is the insert statement SqlConnection conn = new SqlConnection ("server = MRGAO-PC \ SQLEXPRESS; database = jiaohua; uid = sa; pwd = *********** "); // you can change the connection keyword to your own try {conn. open (); // Open the database string ins = "insert into tb_jiaohua (dtem, dhumidity, dt_humidity) values ('" + tem + "', '" + humidity + "', '"+ t_humidity +"') "; // Insert the data keyword SqlCommand cmd = new SqlCommand (ins, conn); cmd. executeNonQuery (); // execute insert conn. close ();// Close database} catch (Exception ex) {Console. WriteLine ("unable to connect to database! "); Console. writeLine (ex. message);} // The method used to connect to the database is the same, but the insert and query statements may be different, because the insert operation table, but the Query Needs to be displayed. // I think the insert, modify, and delete methods may be the same, but the statement is changed and not verified. Try it by yourself. // This method is intuitive, but you need to connect to the database every time you execute the statement (that is, the first two or three sentences cannot be left blank ). // I used the database connection today and tried it. This method is okay and I have verified it. // The following is the query statement, also verified, the correct SqlConnection conn = new SqlConnection ("server = MRGAO-PC \ SQLEXPRESS; database = jiaohua; uid = sa; pwd = ************ "); try {conn. open (); Console. writeLine ("successfully connected to the database! "); SqlCommand cmd1 = new SqlCommand (" select dtem from tb_jiaohua ", conn); SqlDataReader dr = bytes 1.executereader (); while (dr. read () {Console. write (dr [0]); // the program in the console is not in the form, but in the same principle, dr [0] is equivalent to the first data variable found, it can be displayed in the form control.} Dr. Close (); conn. Close ();} catch (Exception ex) {Console. WriteLine ("cannot connect to the database! "); Console. WriteLine (ex. Message );}

Related Article

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.