【问题标题】:Updating SQLCE causes entire column's data to change C#更新 SQLCE 会导致整个列的数据更改 C#
【发布时间】:2012-03-11 06:31:42
【问题描述】:

首先我想说的是,在过去的几周里,这个网站给了我很大的帮助。首先,我用 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 = "";


        }
    }
    }
    }

很抱歉发布了整个代码,我知道它没有整齐地编码,如果这只是一个语法错误,我只编码了大约一个月,请您指出,因为我几乎一直盯着这个一天,无法弄清楚,或者任何其他想法,非常感谢。

【问题讨论】:

    标签: c#


    【解决方案1】:

    我怀疑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 值一样。

    【讨论】:

      猜你喜欢
      • 2013-04-23
      • 2019-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多