0

我制作了一个更新 sqlce 数据库的应用程序。问题是,当我尝试删除某些内容时,它会显示“InvalidArgument='1' 的值对'index' 无效。”

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlServerCe;
namespace Database_Application
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

        }

        public SqlCeConnection conn = new SqlCeConnection(@"Data Source=C:\automail.sdf");





        ////////////////////////////////////Methodes////////////////////////////////////


        private void Populate()
        {
            SqlCeCommand cm = new SqlCeCommand("SELECT * FROM Emails ORDER BY principalID", conn);
            listView1.Items.Clear();

            try
            {
                SqlCeDataReader dr = cm.ExecuteReader();

                while (dr.Read())
                {
                    ListViewItem it = new ListViewItem(dr["principalID"].ToString());
                    it.SubItems.Add(dr["query"].ToString());
                    it.SubItems.Add(dr["email"].ToString());
                    it.SubItems.Add(dr["subject"].ToString());
                    listView1.Items.Add(it);        
                }

                dr.Close();
                dr.Dispose();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                Application.ExitThread();
            }
        }






        ////////////////////////////////////Methodes////////////////////////////////////

        // Insert button (for data insert)
        private void button1_Click(object sender, EventArgs e)
        {
            if ( txtEmail.Text == "" || txtQuery.Text == "")
            {
                MessageBox.Show("Fill in all the information first!");
            }
            else
            {
                SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Emails(principalID, email, query, subject) VALUES(@principalID, @email, @query, @subject)", conn);
                cmd.Connection = conn;
                cmd.Parameters.AddWithValue("@principalID", txtPrincipal.Text);
                cmd.Parameters.AddWithValue("@email", txtEmail.Text);
                cmd.Parameters.AddWithValue("@query", txtQuery.Text);
                cmd.Parameters.AddWithValue("@subject", txtSubject.Text);

                try
                {
                    int affectedrows = cmd.ExecuteNonQuery();
                    if (affectedrows > 0)
                    {
                        Populate();
                        MessageBox.Show("Email added successfully!");

                        txtEmail.Clear();
                        txtPrincipal.Clear();
                        txtQuery.Clear();
                        txtSearch.Clear();
                        txtSubject.Clear();
                    }
                    else
                    {
                        MessageBox.Show("Failed to add email!");
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            } 

        }
        //if form shown fill the listview with the new information of the database
        private void Form1_Shown(object sender, EventArgs e)
        {
            try
            {
                conn.Open();
                Populate();
            }

            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.Message);
                Application.ExitThread();
            }

        }
        //open form 2 and hide this
        private void button2_Click(object sender, EventArgs e)
        {
            this.Hide();
            Form2 form2 = new Form2();
            form2.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        //if listview selected enable button, else disable
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {   
            if (listView1.SelectedItems.Count > 0)
            {
                //enable delete button
                button3.Enabled = true;
                ListViewItem itm = listView1.SelectedItems[0];
                string principalid = itm.SubItems[0].Text;
                string query = itm.SubItems[1].Text;
                string email = itm.SubItems[2].Text;

                string subject = itm.SubItems[3].Text;

                txtPrincipal.Text = principalid.ToString();
                txtEmail.Text = email.ToString();
                txtQuery.Text = query.ToString();
                txtSubject.Text = subject.ToString();


            }


            else
            {
                button3.Enabled = false;
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }
        //delete record button
        private void button3_Click_1(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count <= 0)
            {
                MessageBox.Show("Select a record!");
            }
            else
            {
                ListView.SelectedListViewItemCollection items = this.listView1.SelectedItems;
                //for (int i = 0; i < listView1.SelectedItems.Count; i++)
                foreach(ListViewItem item in items)
                { 
                    SqlCeCommand cm = new SqlCeCommand("DELETE * FROM Emails WHERE query ='" + listView1.SelectedItems[1].Text + "'", conn); 


                    try
                    {
                        cm.ExecuteNonQuery();
                        Populate();
                    }

                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                }   
            }
        }
        // if button clicked, transfer information out of the records selected to form2
        private void button2_Click_1(object sender, EventArgs e)
        {




        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("Are you sure you want to close this application?\nThis will terminate all systems which are active at the moment","Close", MessageBoxButtons.YesNo);
            if (result == DialogResult.Yes)
            {


            }
             if (result == DialogResult.No)
            {
                e.Cancel = true;
            }


        }

        private void button4_Click(object sender, EventArgs e)
        {
            //try
            //{
            //    SqlCeCommand cm = new SqlCeCommand("SELECT * FROM Emails where principalID like " + txtSearch.Text, conn);
            //}

            //catch (Exception ex)
            //
            //    MessageBox.Show(ex.Message);
            //}


        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

它抛出错误
SqlCeCommand cm = new SqlCeCommand("DELETE * FROM Emails WHERE query ='" + listView1.SelectedItems[1].Text + "'", conn);

有人能帮助我吗?我已经尝试过参数,但仍然没有运气......在我使用工具将CSV文件导入我的数据库之前它工作正常,但数据库结构仍然相同,所以它应该能够删除我选择的所有内容.

4

2 回答 2

1

您不能在命令中使用*withwhere条件delete..so 像这样尝试....

SqlCeCommand cm = new SqlCeCommand("DELETE FROM Emails WHERE query ='" + listView1.SelectedItems[0].Text + "'", conn);
于 2013-06-13T09:21:15.473 回答
0

忽略代码中的其他问题(Sql 的安全性等),我相信这是您要问的具体问题。

你正在使用这个:

listView1.SelectedItems[1].Text

...但是 C# 中的数组和列表是零绝对值,因此第一个选定的项目实际上是这样的:

listView1.SelectedItems[0].Text

还值得注意的是,由于您在循环中调用它,因此您将尝试为list 中的每个项目删除该单个项目。我怀疑循环是多余的,或者,您注释掉的循环(通过每个选定的项目)是您真正想要的,在这种情况下,您的意思是:

listView1.SelectedItems[i].Text

...当您循环时,它将依次使用每个项目的文本。

于 2013-06-13T09:24:36.500 回答