Example of how C # connects to a SQL server2008 database share

Source: Internet
Author: User
This article mainly describes the C # connection to the SQL Server2008 database instance code, the need for friends can refer to the following

Not much nonsense to say, directly to everyone to paste the code, the specific code is as follows:


Namespace myfirstapp{class Program {static void Main (string[] args) {SqlConnection conn = null;      SqlCommand comm = null;      SqlDataReader sdreader = null;        try {string constr = "Server=192.168.1.110;uid=sa;pwd=woaifr0828;database=shudiansuo";        conn = new SqlConnection (CONSTR); Conn.        Open (); String sql = "SELECT * FROM dbo."        Member ";        Comm = new SqlCommand (SQL); Comm.        Connection = conn; Sdreader = Comm.        ExecuteReader (); Sdreader.        Read (); Console.WriteLine ("Database connection succeeded!        ");        Console.WriteLine ("Contents of the Student table"); while (Sdreader.          Read ()) {Console.WriteLine ("Sdreader[\" id\ "]=" + sdreader["id"]);          Console.WriteLine ("Sdreader[\" name\ "]=" + sdreader["membername"]);        Console.WriteLine ("Sdreader[\" sex\ "]=" + sdreader["Phonenum"]); } sdreader.        Close (); Conn.      Close (); } catch (SqlException e) {Console.WriteLine ("A SqlException was thrOwn ");        Console.WriteLine ("number =" + E.number);        Console.WriteLine ("Message =" + e.message);      Console.WriteLine ("stacktrace:\n" + e.stacktrace);    } console.readkey (); }  }}

summary

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.