OdbcConnection open()不起作用

时间:2015-03-21 03:43:51

标签: c# visual-studio-2012

protected OdbcConnection conectarBD()
{
    String StringDeConexion = "Data Source=PABLOZN\\SQLEXPRESS;Initial Catalog=Proyecto2;Integrated Security=True";

    try
    {
        OdbcConnection conexion = new OdbcConnection(StringDeConexion);
        conexion.Open();
        return conexion;
    }
    catch (Exception ex)
    {
        Label3.Text = ex.StackTrace.ToString();
        return null;
    }
}

问题是,当我浏览我的网站时,Label会在第18行显示此例外

  

en System.Data.Odbc.OdbcConnection.HandleError(OdbcHandle hrHandle,   RetCode retcode)en   System.Data.Odbc.OdbcConnectionHandle..ctor(OdbcConnection连接,   OdbcConnectionString constr,OdbcEnvironmentHandle environmentHandle)   en System.Data.Odbc.OdbcConnectionOpen..ctor(OdbcConnection   outerConnection,OdbcConnectionString connectionOptions)zh   System.Data.Odbc.OdbcConnectionFactory.CreateConnection(DbConnectionOptions   options,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,   DbConnectionPool池,DbConnection owningObject)en   System.Data.ProviderBase.DbConnectionFactory.CreateConnection(DbConnectionOptions   options,DbConnectionPoolKey poolKey,Object poolGroupProviderInfo,   DbConnectionPool池,DbConnection owningConnection,   DbConnectionOptions userOptions)zh   System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(的DbConnection   owningConnection,DbConnectionPoolGroup poolGroup,DbConnectionOptions   userOptions)en   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(的DbConnection   owningConnection,TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) en System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1次重试,DbConnectionOptions userOptions)zh   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(的DbConnection   outerConnection,DbConnectionFactory connectionFactory,   TaskCompletionSource`1重试,DbConnectionOptions userOptions)en   System.Data.ProviderBase.DbConnectionInternal.OpenConnection(的DbConnection   outerConnection,DbConnectionFactory connectionFactory)en   System.Data.Odbc.OdbcConnection.Open()zh_cn index.conectarBD()en   c:.. \ Documents \ Visual Studio   2012 \ WebSites \ Proyecto \ index.aspx.cs:línea18

1 个答案:

答案 0 :(得分:0)

验证用户是否有权访问数据库。如何使用可信连接,验证当前用户是否可以访问数据库。如果您的应用程序是Web,请确保NETWORKSERVICE可以连接到数据库。

您还可以尝试在Windows ODBC管理器中通过DSN配置连接字符串。