這裏是我的代碼,每當我在文本框中輸入,然後再出現錯誤按下按鈕時:SQLEXCEPTION中的ExecuteNonQuery
SQLEXCEPTION是未處理的,列名或提供值的數目不匹配表定義
請幫
private void button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=XXYZZ\SQLEXPRESS;AttachDbFilename=C:\Users\trist\Documents\Invent.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;");
SqlCommand cmd = new SqlCommand ("Insert into tblLogin values ('"+ txtUsername.Text + '"'+ txtPassword.Text + "')",con);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (i > 0)
{
MessageBox.Show("Registered");
}
else
{
MessageBox.Show("HEHE");
}
}
你錯過了一個','在你傳遞的兩個值之間!注意sql注入 –
你應該真的使用[Using Statement](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement)。 –