Visual Studio中访问SQL数据库的代码

string strCon = "Data Source=.;Initial Catalog=Northwind;uid=sa;pwd=Daniel";
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand cmd = con.CreateCommand())
{
string strSql = "select count(*) from Customers";
cmd.CommandText = strSql;
con.Open();
int intCount = Int32.Parse(cmd.ExecuteScalar().ToString()); //ExecuteScaler();
Console.WriteLine(intCount);

cmd.CommandText = "select * from Customers";
string strS = cmd.ExecuteScalar().ToString(); //ExecuteScalar() 返回结果集的首行首列
Console.WriteLine(strS); //ALFKI 返回结果集的首行首列

}
}
Console.ReadKey();

 

posted @ 2015-12-17 17:35  WingFly+  阅读(925)  评论(0编辑  收藏  举报