我正在創建一個登錄選單并輸入詳細資訊:
Username:test2
Email:Test2
Password: testPassword
Confirm Password: testPassword
確認密碼必須與密碼相同才能進入資料庫,因此它將是一個值

然后我得到錯誤:
System.Data.SqlClient.SqlException: '無效的列名 'test2test2testPassword'。INSERT 陳述句中的列數多于 VALUES 子句中指定的值。VALUES 子句中的值數必須與 INSERT 陳述句中指定的列數相匹配。
有人可以幫忙嗎?
uj5u.com熱心網友回復:
您的代碼中存在多個問題:
- 您應該在查詢中使用引數:https : //docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.parameters?view=dotnet-plat-ext-5.0
- 值應以逗號分隔:Best way to store password in database
insert into RegisterTable(RegUsername, RegEMail, RegPassword)
Values('test2','test2','testPassword')
- 永遠不要在您的資料庫中存盤未散列的密碼
uj5u.com熱心網友回復:
評論很好地涵蓋了它,但是您在查詢中缺少值之間的分隔,此外您不應該相信用戶輸入的文本并將其直接用于查詢
你應該使用像這樣的引數化命令
var checkCount =
new SqlCommand("insert into [TableName] (id, othercolumn) VALUES(@id, @otherColumn)",
conn);
checkCount.Parameters.AddWithValue("@id", id);
checkCount.Parameters.AddWithValue("@otherColumn", otherColumn);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/336008.html
上一篇:我從.NET5升級到.NET6,現在從LINQ查詢中獲取SqlNullValueException
下一篇:有一個時間列的最近時期
