【问题标题】:MySQL updating a entire column in datagridMySQL 更新数据网格中的整个列
【发布时间】:2018-06-26 15:34:48
【问题描述】:

我尝试使用按钮更新我的商店库存水平,当按下此按钮时,我希望所有数量都增加文本框中的数量,我已经实施了一些代码来执行此操作,但是它总是命中未插入数据的消息...

 using System;
 using System.Data;
 using System.Windows.Forms;
 using MySql.Data.MySqlClient;

 namespace Aliena_Store
{
public partial class Form3 : Form
{
    MySqlConnection connection = new MySqlConnection("server=localhost;user=root;database=Aliena_Store;port=3306;password=Blackie");
    public Form3()
    {
        InitializeComponent();
    }

    private void Form3_Load(object sender, EventArgs e)
    {
        {




          //MySqlConnection(VarribleKeeper.MySQLConnectionString);
            connection.Open();

            MySqlDataAdapter MyDA = new MySqlDataAdapter();
            string sqlSelectAll = "SELECT * From Aliena_Store.Game_Details";
            MyDA.SelectCommand = new MySqlCommand(sqlSelectAll, connection);

            DataTable table = new DataTable();
            MyDA.Fill(table);

            BindingSource bSource = new BindingSource();
            bSource.DataSource = table;


            dataGridView1.DataSource = bSource;

        }


    }

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {


    }

    private void SeeForm2_Click(object sender, EventArgs e)
    {
        Hide();
        Form2 f = new Form2(); // This is bad
        f.Show();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string updateQuery = ("UPDATE Aliena_Store.Game_details SET Quantity = '" + AddStock.Text + "'");


        try
        {
            MySqlCommand command = new MySqlCommand(updateQuery, connection);
            if (command.ExecuteNonQuery() == 1)
            {
                MessageBox.Show("DATA UPDATED");
            }
            else
            {
                MessageBox.Show("Data NOT UPDATED");
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

    private void AddStock_TextChanged(object sender, EventArgs e)
    {

    }
}
}

任何线索我的代码哪里出错了?

【问题讨论】:

  • 您应该始终使用参数化 SQL 以使您的代码更清晰,避免转换问题,并避免 SQL 注入攻击。
  • 0 分目前是一个大学项目,不打算展示安全功能
  • InExecuteNonQuery 对于 UPDATE、INSERT 和 DELETE 语句,返回值是受命令影响的行数。请参阅此帖子stackoverflow.com/a/38060528/2946329
  • 我目前有 5 个项目,所以 5 个独特的游戏,所以它应该更新所有 5 个的数量,说 5 如果这是在文本框中输入的内容..介意解释如何做一个 where 子句? (对 mysql 和 c# 非常陌生)

标签: c# mysql datagrid


【解决方案1】:

您的更新查询没有 WHERE 子句,因此每条记录都设置为新数量,ExecuteNonQuery 将返回一个数字,其中包含更改的行数。
仅当表中只有一行时,代码才会遇到正确的 if 情况。

一个简单的解决方法如下

if (command.ExecuteNonQuery() > 0)
    ... ok ...

如果您只想更新一条记录,则需要在查询中添加 WHERE 条件。但是这个 WHERE 条件要求您提供数据库表的PrimaryKey 的值,以便引擎识别要更改的记录。

例如

string updateQuery = @"UPDATE Aliena_Store.Game_details 
                       SET Quantity = @qty 
                       WHERE GameID = @id";

此查询将仅更新具有指定 GameID 的记录(其中 GameID 是您的字段的假设名称以及表的主键)

请注意,我在查询中使用了参数占位符。虽然这不是您问题的主要主题,但值得注意的是,编写正确的 SQL 代码除了安全点之外还会给您带来许多优势。将字符串文本解析为正确的数据类型没有问题,sql命令的可读性更高。

MySqlCommand command = new MySqlCommand(updateQuery, connection);
command.Parameters.Add("@qty", MySqlDbType.VarChar).Value = AddStock.Text;
command.Parameters.Add("@id", MySqlDbType.Int32).Value = Convert.ToInt32(txtGameID.Text);
if (command.ExecuteNonQuery() > 0)
     ....

【讨论】:

    猜你喜欢
    • 2016-12-12
    • 1970-01-01
    • 2013-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-03
    相关资源
    最近更新 更多