如果記錄存在,我想在C#應用程序中進行額外的控制。C#從SQL Server數據庫中選擇
我得到了以下代碼 - 但它保持返回-1的結果,即使記錄確實存在於SQL Server數據庫中。
有人可以幫助我嗎?我已經添加了 - >它出錯的地方
private void btnVerwijderen_Click(object sender, RoutedEventArgs e)
{
if (autonrTextBox.Text == "")
{
MessageBox.Show("Waarschuwing u kunt geen auto verwijderen indien er GEEN autonr is ingevuld");
}
else
{
--> SqlConnection con = new SqlConnection(@"Data Source=DESKTOP-RSEBNR7;Initial Catalog=AudiDealer;Integrated Security=True");
--> string check = "SELECT autonr FROM auto WHERE autonr [email protected]";
--> SqlCommand command1 = new SqlCommand(check, con);
--> command1.Parameters.AddWithValue("@autonr", autonrTextBox.Text);
con.Open();
int auto = command1.ExecuteNonQuery();
con.Close();
--> X - 1 MessageBox.Show(auto.ToString());
if (auto > 0)
{
try
{
con.Open();
using (SqlCommand command = new SqlCommand("DELETE FROM auto WHERE autonr =" + autonrTextBox.Text, con))
{
command.ExecuteNonQuery();
}
con.Close();
}
catch (SystemException ex)
{
MessageBox.Show(string.Format("An error occurred: {0}", ex.Message));
}
}
else
{
MessageBox.Show("Het opgegeven autonr komt niet voor in de database. controleer deze.");
}
}
}
嘗試設置它作爲參數值時,您鑄造'autonrTextBox.Text'爲int。 – krillgar