为什么此代码会导致错误?

我一直在寻找关于如何连接到我的本地SQL数据库的小时。 我不敢相信这很难。 无论如何,我认为我更喜欢这个代码。

<%
    Dim myConnection as System.Data.SqlClient.SqlConnection
    Dim myCommand as System.Data.SqlClient.SqlCommand

    myConnection = New System.Data.SqlClient.SqlConnection("Data Source=localhost;Initial Catalog=dbtest;Integrated Security=True")
    myConnection.Open()
%>

但是,它仍然不起作用。 我不知道从哪里开始,因为当页面加载时我也会收到这条消息:

运行时错误

说明:服务器上发生应用程序错误。 此应用程序的当前自定义错误设置可防止远程查看应用程序错误的细节(出于安全原因)。 但是,它可以通过本地服务器上运行的浏览器来查看。

详细信息:要使此特定错误消息的详细信息可在远程计算机上查看,请在位于当前Web应用程序根目录中的“web.config”配置文件中创建一个标记。 这个标签应该将其“模式”属性设置为“关”。

我创建了一个web.config文件,并没有改变任何东西。 我很迷茫。 任何帮助表示赞赏!

堆栈跟踪:[SqlException(0x80131904):无法打开登录请求的数据库“dbtest”。 登录失败。 用户'IIS APPPOOL DefaultAppPool'登录失败。]]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)+6244425 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)+245
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior,SqlCommand cmdHandler,SqlDataReader dataStream,BulkCopySimpleResultSet bulkCopyHandler,TdsParserStateObject stateObj)+2811
System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)+53
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,String newPassword,Boolean ignoreSniOpenTimeout,Int64 timerExpire,SqlConnection owningObject)+248
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host,String newPassword,Boolean redirectedUserInstance,SqlConnection owningObject,SqlConnectionString connectionOptions,Int64 timerStart)+6260362 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject,SqlConnectionString connectionOptions,String newPassword,Boolean redirectedUserInstance)+6260328 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity,SqlConnectionString connectionOptions,Object providerInfo,String newPassword,SqlConnection owningObject,Boolean redirectedUserInstance)+354
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项,对象poolGroupProviderInfo,DbConnectionPool池,DbConnection拥有连接)+703
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection,DbConnectionPool pool,DbConnectionOptions options)+54 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)+6261592
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection拥有对象)+81
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection拥有对象)+1657
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection拥有连接)+88
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory)+6265031
System.Data.SqlClient.SqlConnection.Open()+258 ASP.index_aspx .__ Render__control1(HtmlTextWriter __w,Control parameterContainer)+71 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer,ICollection children)+115
System.Web.UI.Page.Render(HtmlTextWriter作家)+38
System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint)+4240


它在这里

无法打开登录请求的数据库“dbtest”。 登录失败。 用户'IIS APPPOOL DefaultAppPool'登录失败

App池标识帐户在SQL Server中设置,但未在数据库dbtest中设置。 如果您无法连接到SQL Server实例,则会得到不同的错误消息,但是您正在连接以获取上述错误消息。

所以,在SQL中:

USE dbtest
GO
CREATE USER [IIS APPPOOLDefaultAppPool] FROM LOGIN [IIS APPPOOLDefaultAppPool]

编辑:

当你连接到一个“数据库”时,你实际验证了3次(基本上)

  • 到托管SQL Server实例的Windows /域安装(超出此范围)
  • 对于SQL Server实例,您在“登录”的位置通过CREATE LOGIN设置SQL Server
  • 这个登录映射到零个或多个数据库作为“用户”,这是你需要在这里做的
  • 在这种情况下,您在SQL Server中设置为登录名,但不在SQL Server实例中托管的目标数据库中设置。


    正确放置CustomError标签和值,如:

    <configuration>
        <system.web>
          <customErrors mode="Off"/>
        </system.web>
    </configuration>
    

    这会给你带来确切的问题。


    尝试设置数据源=(本地)

    链接地址: http://www.djcxy.com/p/9709.html

    上一篇: Why does this code cause an error?

    下一篇: Application Role connect to SQL Server with "Login Failed" error