-
C#中连接odbc数据库
using<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>;
using<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.IO;
using<Atitle=systemhref="http://www.alimama.com/membersvc/buyadzone/buy_ad_zone.htm?adzoneid=892989"target=_blank>system</A>.Data.SqlClient;
namespaceConsoleSqlConnection
{
///<summary>
///Class1的摘要说明。
///</summary>
classSqlConnectionClass
{
///<summary>
///应用程序的主入口点。
///</summary>
[STAThread]
staticvoidMain(string[]args)
{
stringstrCon=@"server=(local);IntegratedSecurity=true;database=Northwind;uid=sa;pwd=sa";
SqlConnectionMyCon=newSqlConnection(strCon);
//打开SQLServer数据库
try
{
MyCon.Open();
//浏览数据库
stringstrSQL=@"select*fromCustomers";
SqlCommandMyCommand=newSqlCommand(strSQL,MyCon);
//将检索结果放入SqlDataReader中
SqlDataReaderMyDataReader=MyCommand.ExecuteReader();
Console.WriteLine("显示数据库中的数据");
while(MyDataReader.Read())
{
Console.WriteLine("用户名称:{0}公司名称:{1}所在城市:{2}",MyDataReader["CustomerID"].ToString().PadRight(10),MyDataReader["CompanyName"].ToString().PadRight(25),MyDataReader["City"].ToString());
}
MyDataReader.Close();
}
catch(Exceptionex)
{
Console.WriteLine("{0}",ex.ToString());
}
finally
{
//使用完毕,关闭数据库
MyCon.Close();
}
Console.ReadLine();
}
}
}