您的位置:首页 > 数据库

C#学习笔记——连接sqlserver数据库

2015-12-13 10:11 302 查看
记录下自己的学习路线,备忘下。☺

1)SqlConnection

static void Main(string[] args)
{
string connection =
"server=.;database=db_test;Trusted_connection=true;";
SqlConnection sc = new SqlConnection();
sc.ConnectionString = connection;
try
{
sc.Open();
Console.WriteLine("already open the database");
}
catch(Exception ex)
{
Console.WriteLine("error!!! {0}", ex.Message);
}
finally
{
sc.Close();
Console.WriteLine("already close the database");
}
System.Console.ReadLine();

}


2)udl连接方法

static void Main(string[] args)
{
OleDbConnection connection = new OleDbConnection("File Name=d://test.udl");
try
{
connection.Open();
if (connection.State == System.Data.ConnectionState.Open)
Console.WriteLine("Connection open successfully!");
else
Console.WriteLine("Connection could not be established");
}
catch(Exception ex)
{
Console.WriteLine(ex.Message.ToString());
}
finally
{
connection.Close();
}
Console.ReadLine();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: