[C#] C# 连接SqlServer数据库代码片段 →→→→→进入此内容的聊天室

来自 , 2020-10-17, 写在 C#, 查看 114 次.
URL http://www.code666.cn/view/30f607dc
  1. using System;
  2. using System.Data.SqlClient;
  3. public class TestADO
  4. {
  5.     public static void Main()
  6.     {
  7.         SqlConnection conn = new SqlConnection(Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs);
  8.         SqlCommand  cmd = new SqlCommand(SELECT * FROM employees, conn);
  9.         try
  10.         {        
  11.             conn.Open();
  12.             SqlDataReader reader = cmd.ExecuteReader();            
  13.             while (reader.Read())
  14.             {
  15.                 Console.WriteLine(First Name: {0}, Last Name: {1}, reader.GetString(0), reader.GetString(1));
  16.             }
  17.        
  18.             reader.Close();
  19.             conn.Close();
  20.         }
  21.         catch(Exception e)
  22.         {
  23.             Console.WriteLine(Exception Occured -->> {0},e);
  24.         }        
  25.     }
  26. }
  27. //csharp/4118

回复 "C# 连接SqlServer数据库代码片段"

这儿你可以回复上面这条便签

captcha