0
我不理解它。這段代碼應該可以工作,但肯定有一些我做錯了。SQL Server數據庫不會更新新信息
任何人都可以看到我做錯了什麼嗎?
string username = tbNewUSER.Text.Trim();
string password = tbNewPass.Text.Trim();
string role = "USER";
string str = "insert into UserValidation (USERNAME, PASSWORD, ROLE) values ('" + username + "','" + password + "','" + role + "')";
MessageBox.Show(username + " Registered", "User registration",MessageBoxButtons.OK, MessageBoxIcon.Information);
clsDB.InsUpDel(str);
這是跟進:
public static int InsUpDel(string str)
{
if (!(conn.State == ConnectionState.Open))
conn.Open(); //open connection if closed
int numRows = 0; //counter that checks number of rows affected in the db
try
{
SqlCommand cmd = new SqlCommand(str, conn);
numRows = cmd.ExecuteNonQuery();
cmd = null;
}
catch (SqlException ex)
{
string errorMsg = ex.Message; //more code can be put here
}
if (conn.State == ConnectionState.Open)
conn.Close();
return numRows;
}
謝謝。
可能要考慮使用參數化查詢。否則,你是否嘗試運行生成的查詢? – Stephen
您是否嘗試過嘗試/趕上? – Snowlockk
請注意,儘量避免在提交更改前顯示成功消息。 – Stephen