当按下删除按钮时,没有从一个或多个参数给出值
本文关键字:一个 参数 按钮 删除 | 更新日期: 2023-09-27 18:09:05
当前我正在做的是选择一个数据从MsDatabase和字符串后
query = "delete from RegisterItem where Name =" +txtName1.Text+"";
它显示了一个错误,说一个或多个参数没有给出值,而我已经检查了txtName1是否匹配Name
中的列{
OleDbConnection connect = new OleDbConnection();
public MenClothing(string text)
{
InitializeComponent();
txtUsername.Text = text;
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'Teronkee'Desktop'OFFICAL STAC'StacProductions'DatabaseSaveItem.accdb";
}
public MenClothing()
{
InitializeComponent();
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:'Users'Teronkee'Desktop'OFFICAL STAC'StacProductions'DatabaseSaveItem.accdb";
}
private int upperCase(string pass)
{
int num = 0;
foreach (char ch in pass)
{
if (char.IsUpper(ch))
{
num++;
}
}
return num;
}
private void btnlogout_Click(object sender, EventArgs e)
{
this.Hide();
Form2 Return = new Form2(txtUsername.Text);
Return.ShowDialog();
}
private void MenClothing_Load(object sender, EventArgs e)
{
try
{
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
string query = "select * from RegisterItem";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
comboBox1.Items.Add(reader["Name"].ToString());
}
connect.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
connect.Close();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBox1.ImageLocation = ItemUrl.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
pictureBox1.ImageLocation = ItemUrl.Text;
}
private void button1_Click(object sender, EventArgs e)
{
try {
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "insert into RegisterItem([Name], [Url],[Description], [Price]) values('" + ItemName.Text + "','" + ItemUrl.Text + "','" + ItemDescription.Text + "','" + ItemPrice.Text + "')";
string str = ItemUrl.Text;
pictureBox1.ImageLocation = str;
string str1 = textBox1.Text;
Image img = Image.FromFile(str);
pictureBox1.Image = img;
command.ExecuteNonQuery();
MessageBox.Show("Data Saved");
txtID1.Text = txtUsername.Text;
txtName1.Text = ItemName.Text;
txtDescription1.Text = ItemDescription.Text;
txtPrice1.Text = ItemPrice.Text;
ItemName.Text = "";
ItemDescription.Text = "";
ItemPrice.Text = "";
connect.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " +ex);
}
connect.Close();
}
private void label5_Click(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void textBox5_TextChanged(object sender, EventArgs e)
{
}
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
string query = "delete from RegisterItem where Name =" +txtName1.Text+"";
command.CommandText = query;
command.ExecuteNonQuery();
MessageBox.Show("Data deleted ");
connect.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
connect.Close();
//if (txtUsername.Text == txtID1.Text)
// {
// txtName1.Text = "";
// txtDescription1.Text = "";
// txtPrice1.Text = "";
// }
}
private void ItemDescription_TextChanged(object sender, EventArgs e)
{
}
private void AddString_Click(object sender, EventArgs e)
{
string namestr = txtName1.Text;
comboBox1.Items.Add(namestr);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
string query = "select * from RegisterItem where Name='" + comboBox1.Text + "'";
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
txtName1.Text = reader["Name"].ToString();
txtDescription1.Text = reader["Description"].ToString();
txtPrice1.Text = reader["Price"].ToString();
}
connect.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error " + ex);
}
connect.Close();
}
}
}
您的查询没有正确引用。ID是一个数字,所以它不需要单引号,Name是一个字符串——它需要。
这就是ID有效而name无效的原因。将删除查询更改为
string query = "delete from RegisterItem where Name = '" + txtName1.Text + "'";
你应该使用string.format()来避免这些错误,最好使用参数化查询