为什么这段代码会导致错误?

发布于 2024-09-10 07:01:47 字数 3139 浏览 6 评论 0原文

我花了几个小时寻找如何连接到本地 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”配置文件中创建一个标记。然后,该标签应将其“mode”属性设置为“Off”。

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

堆栈跟踪:[SqlException (0x80131904): 无法打开数据库 登录请求的“dbtest”。这 登录失败。用户登录失败 'IIS APPPOOL\DefaultAppPool'。]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException 异常,布尔中断连接) +6244425 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj) +245
System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior、SqlCommand cmdHandler、 SqlDataReader数据流, 批量复制简单结果集 批量复制处理程序、TdsParserStateObject stateObj) +2811
System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(布尔值 入伍OK) +53
System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo 服务器信息,字符串新密码, 布尔值ignoreSniOpenTimeout,Int64 定时器过期、SqlConnection 拥有对象)+248
System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(字符串 主机、字符串、新密码、布尔值 重定向用户实例、SqlConnection 拥有对象、SqlConnectionString 连接选项,Int64 计时器启动) +6260362 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection 拥有对象、SqlConnectionString 连接选项,字符串新密码, 布尔重定向用户实例) +6260328 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity 身份、SqlConnectionString 连接选项,对象 提供商信息,字符串新密码, SqlConnection 拥有对象,布尔值 重定向用户实例)+354
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions 选项,对象池GroupProviderInfo, DbConnectionPool 池、DbConnection 拥有连接)+703
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection 拥有连接、DbConnectionPool 池、DbConnectionOptions 选项)+54 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection 拥有对象)+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 外部连接、DbConnectionFactory 连接工厂)+6265031
System.Data.SqlClient.SqlConnection.Open() +258 ASP.index_aspx.__Render__control1(HtmlTextWriter __w, 控制参数Container) +71 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter 作家,ICollection儿童)+115
System.Web.UI.Page.Render(HtmlTextWriter 作家)+38
System.Web.UI.Page.ProcessRequestMain(布尔值 includeStagesBeforeAsyncPoint,布尔值 includeStagesAfterAsyncPoint) +4240

I have been looking for hours on how to connect to my local SQL database. I can't believe its been this difficult. Anyway, i think I am more on the right track with this code.

<%
    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()
%>

However, It still does not work. I don't know where to begin because I also get this message when the page loads:

Runtime Error

Description: An application error occurred on the server. The current custom error settings for this application prevent the details of the application error from being viewed remotely (for security reasons). It could, however, be viewed by browsers running on the local server machine.

Details: To enable the details of this specific error message to be viewable on remote machines, please create a tag within a "web.config" configuration file located in the root directory of the current web application. This tag should then have its "mode" attribute set to "Off".

I created a web.config file, and it didn't change anything. I'm so lost. Any help is appreciated!

Stack Trace: [SqlException
(0x80131904): Cannot open database
"dbtest" requested by the login. The
login failed. Login failed for user
'IIS APPPOOL\DefaultAppPool'.]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException
exception, Boolean 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
options, Object poolGroupProviderInfo,
DbConnectionPool pool, DbConnection
owningConnection) +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
owningObject) +81
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection
owningObject) +1657
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection
owningConnection) +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
writer) +38
System.Web.UI.Page.ProcessRequestMain(Boolean
includeStagesBeforeAsyncPoint, Boolean
includeStagesAfterAsyncPoint) +4240

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

初相遇 2024-09-17 07:01:47

就在这里

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

应用程序池身份帐户在 SQL Server 中设置,但未在数据库 dbtest 中设置。如果您无法连接到 SQL Server 实例,您会收到不同的错误消息,但您正在连接以获取上述错误消息。1

因此,在 SQL 中:

USE dbtest
GO
CREATE USER [IIS APPPOOL\DefaultAppPool] FROM LOGIN [IIS APPPOOL\DefaultAppPool]

编辑:

当您连接到“数据库”时,您实际上进行了身份验证3 次(基本上)

  1. 到托管 SQL Server 实例的 Windows/域安装(超出此处的范围)
  2. 到 SQL Server 实例,您通过 CREATE LOGIN “登录”设置 SQL Server
  3. 此登录映射到零个或多个数据库作为“用户”,这就是您需要在此处执行的操作。

在本例中,您已在 SQL Server 中设置为登录名,但未在 SQL Server 实例内托管的目标数据库中设置。

It's here

Cannot open database "dbtest"
requested by the login. The login
failed. Login failed for user 'IIS
APPPOOL\DefaultAppPool'

The App Pool identity account is set up in SQL Server but is not set up in the database dbtest. You get a different error message if you could not connect to the SQL Server instance, but you are connecting for you to get this error message above.1

So, in SQL:

USE dbtest
GO
CREATE USER [IIS APPPOOL\DefaultAppPool] FROM LOGIN [IIS APPPOOL\DefaultAppPool]

Edit:

When you connect to a "database", you actually authenticate 3 times (basically)

  1. To the Windows/domain install hosting the SQL Server Instance (out of scope here)
  2. To the SQL Server instance, where you are a "login" set up SQL Server via CREATE LOGIN
  3. This login maps to zero or more databases as a "user", which is what you needed to do here

In this case, you were set up in SQL Server as a login but not set up in the target database hosted within the SQL Server instance.

且行且努力 2024-09-17 07:01:47

正确放置 CustomError 标记和值,例如:

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

这将向您暴露确切的问题。

Put the CustomError tag and value properly, like:

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

This will expose the exact issue to you.

只有影子陪我不离不弃 2024-09-17 07:01:47

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

Try to set Data Source=(local)

妥活 2024-09-17 07:01:47

您是否遵循了所有说明并确保 web.config 中的 customerErrors 标记如下所示:

真正的错误似乎是用户 ASP .Net 正在运行,因为无权登录数据库。

Have you followed all the instructions and made sure the customerErrors tag in the web.config looks like this:

<customErrors mode="Off" />

And the real error appears to be that the user ASP.Net is running as does not have permission to log in to the database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文