从 SQLException 错误输出异常
我有一个 .aspx 页面调用 .asmx 页面的 Web 服务。在该 .net Web 服务中,我尝试打开与 SQLServer 2008 的数据库连接。连接失败。我不知道为什么。我在 try / catch 中执行此操作,当我调试时,catch 确实会被命中。我不确定我可以在那里输出什么,因为我无权访问服务器的文件系统来写入日志文件。
我找到了这篇文章:
try
{
SqlCommand cmd =
new SqlCommand("raiserror('Manual SQL exception', 16, 1)",DBConn);
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string msg = ex.Message; // msg = "Manual SQL exception"
}
here,它可能对我有用,但我不知道如何将 msg 字符串输出到调用此 Web 服务的页面?有没有办法通过让调用页面也实现相同的异常处理程序来将其传播到异常链?
谢谢 // :)
I have a .aspx page calling a .asmx page's web service. In that .net web service I attempt to open a database connection to SQLServer 2008. The connection is failing. I am not sure why. I am doing this in a try / catch and the catch does get hit when I debug. I'm not sure what I can output there though as I don't have access to the server's filesystem to write a log file.
I found this posting:
try
{
SqlCommand cmd =
new SqlCommand("raiserror('Manual SQL exception', 16, 1)",DBConn);
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
string msg = ex.Message; // msg = "Manual SQL exception"
}
here and it might do the trick for me, but I don't know how to make the msg string output to the page which called this web service? Is there a way to propagate it up the exception chain by having the calling page also implement that same exception handler?
Thanks // :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您应该能够将 SQL 异常捕获为特定类型并读取特定的错误消息:
然后您可以将其抛出“堆栈上”,这意味着将其发送回调用失败代码的方法:
或者您可以抛出一个新的基于 sql 异常的消息:
所有这些方法都允许您将消息从堆栈发送回调用 SQL 的客户端。这是您应该呈现消息的地方。
Your should be able to trap the SQL Exception as a specifc type and read the particular error message:
You could then throw this "up the stack" which means sending it back to the method which called the failing code:
Or you could throw a new message based on the sql exception:
All of these approaches allow you to send messages back up the stack to the client that called the SQL. This is where you should render you message.
如果您在 web.config 中设置以下内容,则异常详细信息将显示在浏览器中。
或者您可以尝试安装 ELMAH,它的设置非常简单,并且可以配置为在内存中保留异常详细信息的日志。
If you set the following in web.config then the exception details will be shown in the browser.
Or you could try installing ELMAH, it's very easy to set up, and can be configured to keep a log of exception details in memory.
您可以为您的页面或应用程序启用跟踪,然后执行以下操作:
然后转到 http://yourhost/Trace.axd 您将能够看到输出。
You can enable tracing for your page or application and then do:
Then go to http://yourhost/Trace.axd and you will be able to see the output.
如果您可以访问调试器,您还可以
在 catch 语句中执行 a 操作。
这会将错误写入输出窗口。
If you can access the debugger, you can also do a
in your catch statement.
This will write the error to the Output window.
既然你说你可以调试,(假设你可以更改代码)为什么不执行以下操作:
然后只需从 Visual Studio IDE 监视窗口/Visualizer & 复制 strEx 数据即可?然后使用数据。
Since you said you can debug, (Assuming you can change the code) why not do a:
Then just copy the strEx data from the Visual Studio IDE watch window/Visualizer & then use the data.