在这里,我尝试在SMO Ojects中使用innerException捕获sql异常。
我收到此错误消息

Msg 121, Level 15, State 1, Procedure test_sp_syntaxtext, Line 124
The select list for the INSERT statement contains more items than the insert list.


但是通过使用这段代码

catch (Exception ex)
          {
           ErrorFlag = true;
           //ex.Source.ToString();
           e2dInsertAndGenerateErrorLog(ex.InnerException.Message.ToString(), FileName, "CHECKINAPPLY", PathName, "ErrorLog.Err");
          }


我得到这个

The select list for the INSERT statement contains more items than the insert list.


我也需要缓存这一行

Msg 121, Level 15, State 1, Procedure test_sp_syntaxtext, Line 124


有什么建议吗?

编辑:

  StreamReader str = new StreamReader(FiletextBox.Text.ToString());
  string script = str.ReadToEnd();
  str.Dispose();
  SqlConnection conn = new SqlConnection("Data Source=xx;database=xxx;User id=sx;Password=xxxx");
  Server server = new Server(new ServerConnection(conn));
  server.ConnectionContext.ExecuteNonQuery(script);

最佳答案

使用SQLException

    catch (SqlException ex)
    {
        for (int i = 0; i < ex.Errors.Count; i++)
        {
            errorMessages.Append("Index #" + i + "\n" +
                "Message: " + ex.Errors[i].Message + "\n" +
                "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                "Source: " + ex.Errors[i].Source + "\n" +
                "Procedure: " + ex.Errors[i].Procedure + "\n");
        }
        Console.WriteLine(errorMessages.ToString());
    }

关于c# - 如何在C#中捕获SQLException?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5962884/

10-11 04:16