分享

连接SQL Server数据库

 空城66 2014-10-25

连接SQL Server数据库: 
using System; 
using System.Data.SqlClient; 

public class TestADO 

public static void Main() 

SqlConnection conn = new SqlConnection("Data Source=localhost; Integrated Security=SSPI; Initial Catalog=pubs");

SqlCommand cmd = new SqlCommand("SELECT * FROM employees", conn); 
try 
{  
conn.Open(); 

SqlDataReader reader = cmd.ExecuteReader();  
while (reader.Read()) 

Console.WriteLine("First Name: {0}, Last Name: {1}", reader.GetString(0), reader.GetString(1)); 


reader.Close(); 
conn.Close(); 

catch(Exception e) 

Console.WriteLine("Exception Occured –>> {0}",e); 
}  

}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多