主题:[原创]注意连接数据库
1、SqlConnection conn=new SqlConnection ("Data Source=LSJWQ\\LSJWQ;User Id=LSJWQ;Password=LSJWQ;database=pubs");
2、SqlConnection conn=new SqlConnection ("Data Source=LSJWQ\\LSJWQ;UId=LSJWQ;Password=LSJWQ;database=pubs");
3、SqlConnection conn=new SqlConnection ("Data Source=LSJWQ\\LSJWQ;UId=LSJWQ;Password=LSJWQ;Initial Catalog=pubs");
4、SqlConnection conn=new SqlConnection ("server=LSJWQ\\LSJWQ;UId=LSJWQ;Password=LSJWQ;Initial Catalog=pubs");
5、SqlConnection conn=new SqlConnection ("server=LSJWQ\\LSJWQ;UId=LSJWQ;Password=LSJWQ;Integrated Security=SSPI;Initial Catalog=pubs");
6、SqlConnection conn=new SqlConnection ("server=LSJWQ\\LSJWQ;UId=LSJWQ;Integrated Security=SSPI;Initial Catalog=pubs");
以上几种连接数据库都可以,有些字段是等价的。
SqlCommand cmd=new SqlCommand ("select * from jobs",conn);
conn.Open ();
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read ())
{
Console.WriteLine ("\t{0}",dr.GetValue(0));
}
2、SqlConnection conn=new SqlConnection ("Data Source=LSJWQ\\LSJWQ;UId=LSJWQ;Password=LSJWQ;database=pubs");
3、SqlConnection conn=new SqlConnection ("Data Source=LSJWQ\\LSJWQ;UId=LSJWQ;Password=LSJWQ;Initial Catalog=pubs");
4、SqlConnection conn=new SqlConnection ("server=LSJWQ\\LSJWQ;UId=LSJWQ;Password=LSJWQ;Initial Catalog=pubs");
5、SqlConnection conn=new SqlConnection ("server=LSJWQ\\LSJWQ;UId=LSJWQ;Password=LSJWQ;Integrated Security=SSPI;Initial Catalog=pubs");
6、SqlConnection conn=new SqlConnection ("server=LSJWQ\\LSJWQ;UId=LSJWQ;Integrated Security=SSPI;Initial Catalog=pubs");
以上几种连接数据库都可以,有些字段是等价的。
SqlCommand cmd=new SqlCommand ("select * from jobs",conn);
conn.Open ();
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read ())
{
Console.WriteLine ("\t{0}",dr.GetValue(0));
}