为什么ExecuteNonQuery()返回-1没有任何异常,而不插入数据库中的条目? [英] Why ExecuteNonQuery() returning -1 without any exception and without inserting the entry in database?

查看:118
本文介绍了为什么ExecuteNonQuery()返回-1没有任何异常,而不插入数据库中的条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下功能:

public Exception createTopic(Topic t)
{
    query = "insert into [DisData].[dbo].[discussions]([title],[description],[usrid],[dateadded],[desid],[likes],[shares],[visit],[replyto],[sno]) values(@title,@des,@uid,@dateadded,@did,@like,@share,@visit,@replyto,@sno)";
    try
    {
        com = new SqlCommand(query, con);
        com.Parameters.AddWithValue("@title", t.getTitle());
        com.Parameters.AddWithValue("@des", t.getDescription());
        com.Parameters.AddWithValue("@uid", t.getUsrID());
        com.Parameters.AddWithValue("@dateadded", t.getDate());
        com.Parameters.AddWithValue("@did", t.getDesID());
        com.Parameters.AddWithValue("@like", 0);
        com.Parameters.AddWithValue("@share", 0);
        com.Parameters.AddWithValue("@visit", 0);
        com.Parameters.AddWithValue("@replyto", t.getReplyToID());
        com.Parameters.AddWithValue("@sno", getDisCount() + 1);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();
        res.Redirect("viewthread.aspx?id=" + t.getDesID());
        return null;
    }
    catch (Exception e)
    {
        con.Close(); return e;
    }
}

连接字符串在包含类的构造函数中定义。问题是,每当我尝试执行此功能时,它都会执行而不会发生任何异常,即使不在Visual Studio Debugger Console上,也不会用用户提供的新条目来更新数据库。当我检查ExecuteNonQuery()的返回值时它返回-1。对我来说,代码似乎是可以的,或者可能是我错过了一些东西。请帮助我识别它。

The connection string is defined in the constructor of containing class. The problem is that, whenever I try to execute this function, it executes without giving any exception, even not on Visual Studio Debugger Console, and also does not updates the database with new entry provided by the user. When I checked for return value of ExecuteNonQuery() it is returning -1. To me the code seem to be okay or may be I am missing something. Please help me to identify it.

我还尝试通过删除所有AddWithValue()语句并将查询预定义为

I also tried to execute the query by removing all AddWithValue() statements and making the query pre-defined as


insert into
[DisData]。[dbo] .discussions
values('Test','TestDes','TestUID' 12-12-2012','sdsd',1,1,1,'sdsd',2)

insert into [DisData].[dbo].discussions values('Test','TestDes','TestUID','12-12-2012','sdsd',1,1,1,'sdsd',2)

但问题依然是相同...

But problem remains the same...

推荐答案

对于UPDATE,INSERT和DELETE语句,返回值是受命令影响的行数。当插入或更新的表上存在触发器时,返回值包括受插入或更新操作影响的行数以及受触发器或触发器影响的行数。对于所有其他类型的语句,返回值为-1。如果回滚发生,返回值也是-1。

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.

更多详细信息read link MSDN链接

More more detail read link MSDN Link

这篇关于为什么ExecuteNonQuery()返回-1没有任何异常,而不插入数据库中的条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆