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