如何使用oledbConnection连接到SQL Server

时间:2012-08-16 08:08:29

标签: c# sql-server

我正在尝试使用OledbConnection连接到SQL Server,但显示错误

  

服务器不存在或拒绝访问

我的代码是

using (OleDbConnection conn = new OleDbConnection(ConnectionString))
{
   try
   {
      // test the connection with an open attempt
      conn.Open();
      this.Dispose();
   }
   catch (Exception ex)
   {
      // inform the user if the connection was not saved
      MessageBox.Show(ex.Message, "Connection Test");
   }
}

1 个答案:

答案 0 :(得分:1)

以下是可用 connection string for MSSQL Server 的列表。如果您使用 System.Date.SqlClient 命名空间,那就更好了。

Using (SqlConnection connection = new SqlConnection(connectionString))
{
    try
    {
         connection.Open()
         // do something here
    }
    catch (SqlException ex)
    {
    }
    finally
    {
         connection.Close()
    } 
}