0

我正在尝试使用 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");
   }
}
4

1 回答 1

1

以下是MSSQL Server的可用连接字符串列表。如果您使用System.Date.SqlClient命名空间会更好。

Using (SqlConnection connection = new SqlConnection(connectionString))
{
    try
    {
         connection.Open()
         // do something here
    }
    catch (SqlException ex)
    {
    }
    finally
    {
         connection.Close()
    } 
}
于 2012-08-16T08:14:54.810 回答