Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MySql说专栏可以';对于不为null的列,不能为null![使用命名参数]_C#_Sql_Mysql_Database - Fatal编程技术网

C# MySql说专栏可以';对于不为null的列,不能为null![使用命名参数]

C# MySql说专栏可以';对于不为null的列,不能为null![使用命名参数],c#,sql,mysql,database,C#,Sql,Mysql,Database,我试图通过MySql/.Net连接器使用.Net执行INSERT INTO查询。查询使用参数。这相当简单: INSERT INTO post ( ID, content, post_url, blogID, title, addedOn, updatedDate, commentsFeedURL, active, viewCount, commentCount, languageID, authorName, postDate, posRating, negRating, adult)

我试图通过MySql/.Net连接器使用.Net执行INSERT INTO查询。查询使用参数。这相当简单:

INSERT INTO post (
ID, content, post_url, blogID, title, addedOn, 
updatedDate, commentsFeedURL, active, viewCount, 
commentCount, languageID, authorName, postDate, 
posRating, negRating, adult) 
VALUES(
@ID, @content, @post_url, @blogID, @title, @addedOn, 
@updatedDate, @commentsFeedURL, @active, @viewCount, 
@commentCount, @languageID, @authorName, @postDate, 
@posRating, @negRating, @adult)
当我运行它时(所有参数都已正确分配),我会得到一个错误

“列'post_url'不能为空”

但它不是空的。这是参数post_url中的值

这是我用来为SQL查询分配参数的代码

cmd.Parameters.AddWithValue("post_url", postOld.URL);

我得到这种行为的原因可能是什么?

在调用Execute方法之前,是否可以检查每个参数中的值?很容易不小心做了一些搞糟的事情。

那张桌子上有插入触发器吗?可能是触发器试图插入到另一个表中,该表的post\u url列不能为null。

尝试将post\u url移动到参数和值列表中的内容之前,以排除由于@content中的复杂值而导致驱动程序将参数错误地传递给MySQL的可能性。

这里没有被刺耳的声音,但这就是为什么测试用例是一件好事。编写一个单元测试来验证传递一个非null postrl是否有效,传递一个null postrl将从数据库中得到这个错误


然后编写一个集成测试(或模拟)来证明
postOld.URL
是非空的。

行不应该是这样的:

cmd.Parameters.AddWithValue("@post_url", postOld.URL);
INSERT INTO post (ID, content, post_url, blogID, title, addedOn, updatedDate, commentsFeedURL, active, viewCount, commentCount, languageID, authorName, postDate, posRating, negRating, adult)" +
                            " VALUES(?ID, ?content, ?post_url, ?blogID, ?title, ?addedOn, ?updatedDate, ?commentsFeedURL, ?active, ?viewCount, ?commentCount, ?languageID, ?authorName, ?postDate, ?posRating, ?negRating, ?adult)

或者你能省略@符号吗?

好了,伙计们,我终于找到了正确的答案

问题很简单,在MySQL查询中,参数标记为“?”而不是“@”。不幸的是,许多查询似乎使用“@”运行良好(它们不是),因此在出现问题时,人们会在稍后发现这一点

谢谢你的回答。我重新编写了我的查询,如下所示:

cmd.Parameters.AddWithValue("@post_url", postOld.URL);
INSERT INTO post (ID, content, post_url, blogID, title, addedOn, updatedDate, commentsFeedURL, active, viewCount, commentCount, languageID, authorName, postDate, posRating, negRating, adult)" +
                            " VALUES(?ID, ?content, ?post_url, ?blogID, ?title, ?addedOn, ?updatedDate, ?commentsFeedURL, ?active, ?viewCount, ?commentCount, ?languageID, ?authorName, ?postDate, ?posRating, ?negRating, ?adult)

它成功了。

你能试着打印出实际的查询字符串吗?是的,实际的查询字符串是帖子的一部分。以“插入”开头的位。尝试滚动它。另外,为什么不将字符串参数括在引号中?哦,它的mysql变量?你把它们放在哪里?你确定它在同一范围内吗?我建议你尝试“选择@ID、@content、@post_url、@blogID、@title、@addedOn、@updatedated、@commentsFeedURL、@active、@viewCount、@commentCount、@languageID、@authorName、@postDate、@posRating、@negRating、@成人”,并检查其输出。你在url_post的位置上看到了什么?是的,我检查过了。这和我提供的值是一样的。好吧。。。也许暂时允许该列为NULL,然后查看插入的内容。我也想到了这一点,所以我创建了一个SQLEncode函数来正确编码内容中的任何值。不起作用。不过,仅仅改变顺序看看会发生什么不值得一试吗?嗯,postold.URL不是空的。显然MySql世界中没有人使用参数化查询。真是浪费!在带有PDO的PHP中,命名参数使用冒号作为前缀(例如,“VALUES(:ID)”),并且仅在MySQLi扩展中可用的未命名参数中使用问号分隔(例如,“VALUES(?)),因此我不会发现这个错误,因为我不熟悉C#。