更新SqlCommand-不在数据库中更新

时间:2017-02-20 14:38:57

标签: c# database sqlcommand

这不会在我的数据库表中更新。我过度看了些什么吗?

值在文本框中很好。没有错误显示,很奇怪。

          using (SqlConnection connection = new SqlConnection(@"Data Source = UKMAN1NB10038\SQLEXPRESS; Initial Catalog = TheVets; Integrated Security = True"))
        {
            SqlCommand command = new SqlCommand("UPDATE OwnerTable SET Owner_Fname =@OwnerFname , Owner_Lname = @OwnerLname, Owner_HouseNo = @OwnerHouse, Owner_Street = @OwnerStreet, Owner_County = @OwnerCounty, Owner_PostCode = @OwnerPost, Owner_Tele = @OwnerTele, Owner_Email = @OwnerEmail WHERE Owner_ID = '" + CB_EDIT_OWNER.SelectedText + "'", connection);

            command.CommandType = CommandType.Text;
            command.Connection = connection;



            command.Parameters.AddWithValue("@OwnerFname", TXT_EDIT_FNAME.Text);
            command.Parameters.AddWithValue("@OwnerLname", TXT_EDIT_LNAME.Text);
            command.Parameters.AddWithValue("@OwnerHouse", TXT_EDIT_HOUSE.Text);
            command.Parameters.AddWithValue("@OwnerStreet", TXT_EDIT_STREET.Text);
            command.Parameters.AddWithValue("@OwnerCounty", TXT_EDIT_COUNTY.Text);
            command.Parameters.AddWithValue("@OwnerPost", TXT_EDIT_POSTCODE.Text);
            command.Parameters.AddWithValue("@OwnerTele", TXT_EDIT_TELE.Text);
            command.Parameters.AddWithValue("@OwnerEmail", TXT_EDIT_EMAIL.Text);

            connection.Open();
            command.ExecuteNonQuery();

            connection.Close();
        }
    }

1 个答案:

答案 0 :(得分:4)

您需要在SelectedItem

上使用SelectedText而不是combobox

CB_EDIT_OWNER.SelectedText替换为:

CB_EDIT_OWNER.SelectedItem

然后这应该有用。

相关问题