如何修复此更新查询? C#.Net使用Mysql Wb [英] How Do I Fix This Update Query? C#.Net Using Mysql Wb

查看:58
本文介绍了如何修复此更新查询? C#.Net使用Mysql Wb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我如何修复这种错误,我使用各种插入查询,但没有任何工作,所以我决定再次编码回到开头。并且仍然得到这个错误,它非常令人沮丧。有人想帮我吗?



这条消息总是提示:你的SQL语法有错误;请查看与你的Mysql服务器版本对应的手册在第1行'Description ='ink Cartridges',Item Code ='0012',Stock ='System.Windows.Form。'附近使用正确的语法。



 使用系统; 
使用 System.Collections.Generic ;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;

命名空间 ToolManagementTest
{
public 部分 Form5:表格
{
public Form5()
{
InitializeComponent();
}

public void btn1_Click( object sender,EventArgs e)
{
try
{
string modConnection = datasource = localhost; port = 3306; username =根;口令= 1234\" ;
string modQuery = UPDATE toolsmanagement.itemlist SET项目描述=' + .modItemTxt.Text + ',Item Code =' + this .modCodeTxt.Text + ',Stock =' + this .modStockTxt + ',Unit =' + this .modUnitTxt.Text + ',Item Type =' + this .modTypeTxt.Text + ',Item Status =' + this .modStatTxt.Text + 'WHERE Item Code =' + this .modCodeTxt.Text + ';

MySqlConnection modData = new MySqlConnection(modConnection);
MySqlCommand modCommandData = new MySqlCommand(modQuery,modData);
MySqlDataReader MyReader;



modData.Open();
MyReader = modCommandData.ExecuteReader();
MessageBox.Show( Registered Data ..);

this .Hide();
while (MyReader.Read())
{

}
modCommandData.ExecuteNonQuery();
modData.Close();

}
catch (例外情况)
{
MessageBox.Show(ex.Message);
}
}


}
}

解决方案

< blockquote>将 this.modStockTxt 更改为 this.modStockTxt.Text

除此之外更好地使用参数而不是连接字符串来构建sql语句,你的应用程序现在广泛用于sql攻击。

带参数的示例sql语句:

 更新 toolsmanagement.itemlist  SET  
`项目描述`= @ ItemDescription
`股票`= @ Stock
`Unit` = @ Unit
`项目类型`= @ ItemType
`Etems Status` = @ ItemStatus
WHERE
`项目代码`= @ ItemCode



注意当你在列名中有空格时,使用``

因为你有不需要的更新语句 MySqlDataReader ,使用上面的参数化sql语句创建 MySqlCommand 然后你可以设置每个参数值如下

 cmd.P arameters.AddWithValue(  @ ItemDescription this  .modItemTxt.Text); 
// 对所有其他参数执行相同操作



最后执行命令

 cmd.ExecuteNonQuery(); 


hello guys, how do i fix this kind of error, i used all kinds of insert query, but nothing is working, so i decided to coding again back to the beginning. and still getting this error, it's so very frustrating. anyone would like to help me?,

this message always prompt: "You have an error in your SQL syntax; check the manual that corresponds to your Mysql server version for the right syntax to use near 'Description = 'ink Cartridges', Item Code = '0012', Stock = 'System.Windows.Form.' at line 1.

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 MySql.Data.MySqlClient;

namespace ToolManagementTest
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }

        public void btn1_Click(object sender, EventArgs e)
        {
            try
            {
            string modConnection = "datasource=localhost;port=3306;username=root;password=1234";
            string modQuery = "UPDATE toolsmanagement.itemlist SET Item Description = '" + this.modItemTxt.Text + "',Item Code = '" + this.modCodeTxt.Text + "',Stock = '" + this.modStockTxt + "',Unit = '" + this.modUnitTxt.Text + "',Item Type = '" + this.modTypeTxt.Text + "',Item Status = '" + this.modStatTxt.Text + "' WHERE Item Code = '" + this.modCodeTxt.Text + "'";

            MySqlConnection modData = new MySqlConnection(modConnection);
            MySqlCommand modCommandData = new MySqlCommand(modQuery, modData);
            MySqlDataReader MyReader;
         

            
                modData.Open();
       MyReader = modCommandData.ExecuteReader();                                  
                MessageBox.Show("Registered Data..");
                
                this.Hide();
                while (MyReader.Read())
                {

                }
                modCommandData.ExecuteNonQuery();
                modData.Close();

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

       
    }
}

解决方案

change this.modStockTxt to this.modStockTxt.Text
other than that you better use parameters instead of concatenating strings to build sql statement, your application is widely open for sql attacks now.
sample sql statement with parameters:

UPDATE toolsmanagement.itemlist SET 
     `Item Description` = @ItemDescription,
     `Stock` = @Stock,
     `Unit` = @Unit,
     `Item Type`= @ItemType,
     `Item Status` = @ItemStatus 
WHERE 
     `Item Code` = @ItemCode 


Note that when you have space in column names, use ``
since you have update statement you don't need MySqlDataReader , create MySqlCommand using parameterized sql statement as above and then you can set each parameter values as below

cmd.Parameters.AddWithValue("@ItemDescription", this.modItemTxt.Text);
// do the same for all other parameters


finally execute the command

cmd.ExecuteNonQuery();


这篇关于如何修复此更新查询? C#.Net使用Mysql Wb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆