如何打印sql异常?

时间:2014-12-15 11:23:26

标签: vb.net sql-server-2008 exception-handling sqlexception

我只想在我的代码中打印sql异常。声明是这样的

Catch Msg As Data.SqlClient.SqlException
  'On any Exception set the connection false
  ConnectDb = False
  writeLogdbconn(" in sql exception ") 'this method will print the message in text file at some directory location
  Throw

我想在这里打印sql异常。当我尝试这个时:

writeLogdbconn("in try - catch " & Msg)

收到错误

  

&安培;没有为字符串和sql异常定义操作数。

请建议我在这里使用什么来打印sql异常。

1 个答案:

答案 0 :(得分:1)

Msg是一个类,而不是字符串。您可能需要使用Msg.Message

writeLogdbconn("in try - catch " & Msg.Message)

您还可以查看MSDN页面上Exception.Message的示例代码。