SQL插入查询不执行而不在c#中抛出任何异常

时间:2012-04-27 16:10:56

标签: c# asp.net sql-server-2008 exception insert-query

我为论坛网站编写了一个插入函数,用于在数据库中插入新的讨论主题。功能如下:

public int createDiscuss(Discussion dis)
    {
        query = "insert into [MyForums].[dbo].[Discussions](desid, text, date, time, replyto, uid, isPrivate) values(@id, @text, @date, @time, @replyto, @uid, @pvp)";
        string did=getDesID();
        if (did == null)
            return -1;
        try
        {
            com = new SqlCommand(query, con);
            com.Parameters.AddWithValue("@id", did); 
            com.Parameters.AddWithValue("@text", dis.gettext()); 
            com.Parameters.AddWithValue("@date", SqlDateTime.Parse(DateTime.Now.ToString())); 
            com.Parameters.AddWithValue("@time", SqlDateTime.Parse(DateTime.Now.ToString()));
            com.Parameters.AddWithValue("@replyto", dis.getreplyto());
            com.Parameters.AddWithValue("@uid", dis.getuid());
            if (dis.IsPrivate() == false)
            { com.Parameters.AddWithValue("@pvp", 1); }
            else
            { com.Parameters.AddWithValue("@pvp", 2);  }
            con.Open();
            int r=com.ExecuteNonQuery();
            if (r <= 0)
            {
                return -1;
            }
            con.Close();

        }
        catch (Exception)
        {
            return -1;
        }
        return 0;
    }

此函数遇到错误或异常而不是返回-1并且由调用函数检查然后调用函数显示错误。问题是这个插入函数没有执行查询。当我检查值是否正确传递给AddWithValue函数作为参数时,我发现每个值都是在asp.net页面上给出的。但是当控制来到com.ExecuteNonQuery()函数时。它返回-1即错误。谁能告诉我我的代码有什么问题?或者我怎样才能检查为什么com.ExecuteNonQuery()没有返回任何受影响的行?

<小时/> 此异常显示

A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll
An exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll but was not handled in user code

5 个答案:

答案 0 :(得分:2)

话虽如此,SQL Profiler在这里是你的朋友。从profiler中获取生成的sql并在SQL mgmt studio中运行它。我99%的信心答案很明显。如果没有,那么请用profiler中生成的sql更新你的答案,我们将从那里开始。

如果您需要有关SQL Profiler的一些指导,那么简单的Google搜索会提供许多选项:SQL Profiler Search

答案 1 :(得分:2)

  • 您应该在finally块中关闭连接。在con.Close();之前遇到的任何异常都会导致您的连接保持打开状态。这可能是错误(取决于con

  • 的范围
  • 您不应该吞下异常,而是记录它。然后,您可以正确检查错误。如果您没有日志记录,请进行日志记录。但在此期间,请查看使用调试器捕获的异常。

  • 您还应该捕获更具体的异常,以便您可以区分连接错误和应用程序中的慢性错误。检查您调用的方法可能引发的异常。例如,con.Open()可以抛出这些:

SqlException

InvalidOperationException

首先抓住那些,最后抓住Exception

根据异常类型,您可以返回不同的错误代码和/或以不同的严重级别记录。


另外,找出一种方法(使用分析或只是复制+粘贴)来对数据库运行该查询,看看会发生什么。例如,你确定这部分是对的:

        com.Parameters.AddWithValue("@date",
            SqlDateTime.Parse(DateTime.Now.ToString())); 
        com.Parameters.AddWithValue("@time",
            SqlDateTime.Parse(DateTime.Now.ToString()));

@date@time完全相同。数据库值是否也相同,列是否都期望字符串?

答案 2 :(得分:1)

你需要做一些基本的调试。设置代码如:

catch(Exception ex)
{
  return -1;
}

在返回时设置断点,并查看异常是什么。

答案 3 :(得分:0)

你可能没有达到int r=com.ExecuteNonQuery();代码。可能是con.Open();方法的错误。

你能在你的捕获中抛出一个新的异常并告诉我们实际的错误吗?

答案 4 :(得分:0)

使用当前代码的挑战是在几种不同的场景中返回“-1”:

  • 发生异常
  • getDesID的结果为空
  • INSERT语句无法插入行

检查此问题的一种简单方法是从每个故障点返回不同的数字(例如-2或-3)。这样,您可以在不更改代码或附加调试器的情况下确定引发错误的位置。

在我看来,问题不太可能是您的SQL INSERT语句 - 如果语句本身无效或违反了数据库约束,SQL Server会向您提出一个异常处理程序捕获的异常。

相关问题