有2个问题。在 SQL 中,NULL 不等于任何东西,甚至不等于 NULL。即NULL = NULL是错误的。所以第一个问题是WHERE ColumnName = NULL
其次,SQL 的语法无效。使用时不能指定 where 子句INSERT .. VALUES,必须使用SELECT .. WHEREor IF.. ELSE。例如
INSERT INTO table ([Tag ID])
SELECT TOP 1 @PhoneNumber
FROM YourTable
WHERE YourColumn IS NULL
或者
IF EXISTS (SELECT 1 FROM YourTable WHERE YourColumn IS NULL)
BEGIN
INSERT Table ([tAG id]) VALUES(@PhoneNumber)
END
在这两个示例中都是Exists inYourTable的表。YourColumn
最后,尽可能避免在构建 SQL 语句时使用字符串连接。
string sql = // One of the queries above
cmd.CommandText = sql
cmd.Parameters.Add("@PhoneNumber", SqlDbType.Varchar).Value = textBox.Text;
cmd.ExecuteNonQuery();