从 SQLException 错误输出异常

发布于 2024-08-14 00:54:03 字数 544 浏览 4 评论 0原文

我有一个 .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 技术交流群。

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

发布评论

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

评论(5

会傲 2024-08-21 00:54:03

您应该能够将 SQL 异常捕获为特定类型并读取特定的错误消息:

try{
   ...
}
catch(SQLException sqlex)
{
   /// do some work here with sqlex.Message
}
catch(Exception ex)
{
   /// this will trap any other 'type' of exception incase a sqlex is not thrown.
}

然后您可以将其抛出“堆栈上”,这意味着将其发送回调用失败代码的方法:

catch(SQLException sqlex)
    {
       throw sqlex;
    }

或者您可以抛出一个新的基于 sql 异常的消息:

catch (SQLException sqlex)
  {
    throw new Exception("My method threw an exception in SQL:" + sqlex.Message);
  }

所有这些方法都允许您将消息从堆栈发送回调用 SQL 的客户端。这是您应该呈现消息的地方。

Your should be able to trap the SQL Exception as a specifc type and read the particular error message:

try{
   ...
}
catch(SQLException sqlex)
{
   /// do some work here with sqlex.Message
}
catch(Exception ex)
{
   /// this will trap any other 'type' of exception incase a sqlex is not thrown.
}

You could then throw this "up the stack" which means sending it back to the method which called the failing code:

catch(SQLException sqlex)
    {
       throw sqlex;
    }

Or you could throw a new message based on the sql exception:

catch (SQLException sqlex)
  {
    throw new Exception("My method threw an exception in SQL:" + sqlex.Message);
  }

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.

我们的影子 2024-08-21 00:54:03

如果您在 web.config 中设置以下内容,则异常详细信息将显示在浏览器中。

<customErrors mode="off" />

或者您可以尝试安装 ELMAH,它的设置非常简单,并且可以配置为在内存中保留异常详细信息的日志。

If you set the following in web.config then the exception details will be shown in the browser.

<customErrors mode="off" />

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.

祁梦 2024-08-21 00:54:03

您可以为您的页面或应用程序启用跟踪,然后执行以下操作:

Trace.Write ("This is my exception:\n" + exception.ToString ());

然后转到 http://yourhost/Trace.axd 您将能够看到输出。

You can enable tracing for your page or application and then do:

Trace.Write ("This is my exception:\n" + exception.ToString ());

Then go to http://yourhost/Trace.axd and you will be able to see the output.

星光不落少年眉 2024-08-21 00:54:03

如果您可以访问调试器,您还可以

Debug.WriteLine(ex.Message) 

在 catch 语句中执行 a 操作。

这会将错误写入输出窗口。

If you can access the debugger, you can also do a

Debug.WriteLine(ex.Message) 

in your catch statement.

This will write the error to the Output window.

别理我 2024-08-21 00:54:03

既然你说你可以调试,(假设你可以更改代码)为什么不执行以下操作:

string strEx = Ex.ToString(); //Ex is the caught exception.

然后只需从 Visual Studio IDE 监视窗口/Visualizer & 复制 strEx 数据即可?然后使用数据。

Since you said you can debug, (Assuming you can change the code) why not do a:

string strEx = Ex.ToString(); //Ex is the caught exception.

Then just copy the strEx data from the Visual Studio IDE watch window/Visualizer & then use the data.

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