更新SQLCE会导致整列的数据更改C#

时间:2012-03-10 22:11:21

标签: c#

首先我要说的是,在过去的几周里,这个网站对我来说是一个非常大的帮助。好吧,首先,我在C#中编写了一个Windows窗体应用程序,其中包含一个用于过滤的文本框,一个列表视图和两个文本框以及两个用于添加新库存和一个用于更新的按钮。

想法是打开程序,它显示所有库存,您选择要编辑的项目,并将值删除到两个文本框,您可以在其中修改和保存它们,或添加新的库存项目。

我从youtube上获得了一些用于更新SQLCE数据库的代码并且工作正常,但我不明白的是当我尝试在不同的表单上使用此代码时,更新我的库存清单,它将无法正常工作。它会更新整个列而不是选定的行。

我试过从头开始编写它甚至复制工作代码,但两者都给出了同样的错误。这是我正在使用的代码,如果库存上传不正确,它只是从两个文本框中的值更新数据库中的两个字段

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;        
    using System.Data.SqlServerCe;

    namespace WindowsFormsApplication1
    {
    public partial class Form1 : Form
    {
    SqlCeConnection cn = new SqlCeConnection("Data source = c:\\Clients.sdf");
    string QTYinStock;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        try
        {
            cn.Open();
            ReadStock(new SqlCeCommand("SELECT * FROM Stock ORDER BY Cartridge_Code", cn));

        }

        catch (SqlCeException ex)
        {
            MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Application.ExitThread();

        }
    }
    private void button2_Click(object sender, EventArgs e)
    {

        if (checkBox1.Checked == true)
        {

            //Check the QTY 
            int QTY;
            bool isInteger = int.TryParse(textBox2.Text, out QTY);



            if (isInteger)
            {
                SqlCeCommand cm = new SqlCeCommand("INSERT INTO Stock(QTY_in_Stock, Cartridge_Code) VALUES(@QTY_in_Stock, @Cartridge_Code)", cn);
                cm.Parameters.AddWithValue("@QTY_in_Stock", textBox2.Text);
                cm.Parameters.AddWithValue("@Cartridge_Code", textBox3.Text);


                try
                {
                    int affectedRows = cm.ExecuteNonQuery();

                    if (affectedRows > 0)
                    {
                        MessageBox.Show("Insert Successfull!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        textBox1.Text = "";
                        textBox2.Text = "";
                        textBox3.Text = "";
                        ReadStock(new SqlCeCommand("SELECT * FROM Stock ORDER BY Cartridge_Code", cn));

                    }

                    else
                    {
                        MessageBox.Show("Insert Failed!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);

                    }
                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);


                }
            }
            else
            {
                MessageBox.Show("No data inserted! The QTY is not valid!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);


            }
        }
        else 
        {
            MessageBox.Show("Please ensure all details are correct and check the checkbox");
        }
    }

    private void Texboxes_TextChanged(object sender, EventArgs e)
    {
        if (textBox2.Text != "" && textBox3.Text != "")
        {
            button1.Enabled = true;
            button2.Enabled = true;

        }

        else
        {
            button1.Enabled = false;
            button2.Enabled = false;
        }
    }
    public void ReadStock(SqlCeCommand cm)
    {

        listView1.Items.Clear();

        SqlCeDataReader dr3 = cm.ExecuteReader();

        while (dr3.Read())
        {

            ListViewItem it3 = new ListViewItem(dr3["QTY_in_Stock"].ToString());
            it3.SubItems.Add(dr3["Cartridge_Code"].ToString());




            listView1.Items.Add(it3);
        }
        dr3.Close();
        dr3.Dispose();

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        ReadStock(new SqlCeCommand("SELECT * FROM Stock WHERE Cartridge_Code LIKE '%" + textBox1.Text + "%' ORDER BY Cartridge_Code", cn));

    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked == true)
        {

            int QTY;
            bool isInteger = int.TryParse(textBox2.Text, out QTY);

            if (isInteger)
            {
                SqlCeCommand cm = new SqlCeCommand("UPDATE Stock SET QTY_in_Stock = @QTY_in_Stock, Cartridge_Code = @Cartridge_Code WHERE QTY_in_Stock = '" + QTYinStock +"'", cn);
                cm.Parameters.AddWithValue("@QTY_in_Stock", textBox2.Text);
                cm.Parameters.AddWithValue("@Cartridge_Code", textBox3.Text);



                try
                {
                    cm.ExecuteNonQuery();
                    ReadStock(new SqlCeCommand("SELECT * FROM Stock ORDER BY Cartridge_Code", cn));

                    textBox2.Text = textBox3.Text = "";


                }
                catch (Exception ex)
                {

                    MessageBox.Show(ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);


                }

                checkBox1.Checked = false;
            }
            else
            {

                MessageBox.Show("The QTY Is Not Valid", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);

            }
        }
        else
        {
            MessageBox.Show("Please ensure all Data is correct and checkbox is Checked");
        }
    }

    private void listView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listView1.SelectedItems.Count > 0)
        {

            QTYinStock = textBox2.Text = listView1.SelectedItems[0].Text;
            textBox3.Text = listView1.SelectedItems[0].SubItems[1].Text;


        }

        else
        {
            textBox2.Text = textBox3.Text = "";


        }
    }
    }
    }

抱歉发布整个代码,我知道它的编码不整齐,我只编写了大约一个月,如果这只是一个语法错误,请你指出它,因为我一直盯着这几乎一天,不能弄清楚,或任何其他的想法,这样做非常赞赏。

1 个答案:

答案 0 :(得分:0)

我怀疑QTYinStock匹配Stock表中的多条记录。 Stock表应该有一个主键设置,其中包含一个自动递增的整数值,因此您可以唯一地标识每条记录。

而不是:

  

更新库存设置QTY_in_Stock = @QTY_in_Stock,Cartridge_Code =   @Cartridge_Code WHERE QTY_in_Stock ='“+ QTYinStock +”'“

你会用:

UPDATE Stock SET QTY_in_Stock = @QTY_in_Stock, Cartridge_Code = @Cartridge_Code WHERE StockID = " + stockID

stockID是变量中保存的唯一行标识符。加载记录时,您需要在某个时候退出此值。您还应该考虑将WHERE子句中的变量设置为参数,就像@QTY_in_Stock和@Cartridge_Code值一样。