为什么更新和删除代码不起作用? [英] why update and delete codes don't work ?

查看:90
本文介绍了为什么更新和删除代码不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

插入代码很好用

insert code Works well

//insert code
      
            string JSS_connetionString004 = null;
            OleDbConnection JSS_connection004;
            OleDbDataAdapter JSS_oledbAdapter004 = new OleDbDataAdapter();
            string JSS_sql004 = null;
            JSS_connetionString004 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
            JSS_connection004 = new OleDbConnection(JSS_connetionString004);
            JSS_sql004 = "insert into control_room(coroom_ID,coroom_OPERATION,coroom_EMPLOYEE,coroom_USER,coroom_INVOICE_NUM,coroom_SUPPLIER,coroom_CUSTOMER,coroom_USER_CHANGE,coroom_DATE,coroom_TIME,coroom_STATUS) values('" + JSS_str_control_room_ID2 + "','" + JSS_coroom_OPERATION + "','" + JSS_coroom_EMPLOYEE + "','" + JSS_coroom_USER + "','" + JSS_coroom_INVOICE_NUM + "','" + JSS_coroom_SUPPLIER + "','" + JSS_coroom_CUSTOMER + "','" + JSS_coroom_USER_CHANGE + "','" + JSS_coroom_DATE + "','" + JSS_coroom_TIME + "','" + JSS_coroom_STATUS + "')";
            try
            {

                JSS_connection004.Open();
                JSS_oledbAdapter004.InsertCommand = new OleDbCommand(JSS_sql004, JSS_connection004);
                JSS_oledbAdapter004.InsertCommand.ExecuteNonQuery();

            }
            catch (Exception ex)
            {

            }





更新和删除代码无法使用该代码执行而不会出现错误





update and delete codes do not work with that code executes without the appearance of errors

//edit
            string JSS_connetionString011 = null;
            OleDbConnection JSS_connection011;
            OleDbDataAdapter JSS_oledbAdapter011 = new OleDbDataAdapter();
            string JSS_sql011 = null;
            JSS_connetionString011 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
            JSS_connection011 = new OleDbConnection(JSS_connetionString011);
            JSS_sql011 = "update product_companies set prodc_NAME = '" + textBox1.Text + "',prodc_BARCODE = '" + textBox2.Text + "' where prodc_ID = '" + label3.Text + "'";

            try
            {
                JSS_connection011.Open();
                JSS_oledbAdapter011.UpdateCommand = JSS_connection011.CreateCommand();
                JSS_oledbAdapter011.UpdateCommand.CommandText = JSS_sql011;
                JSS_oledbAdapter011.UpdateCommand.ExecuteNonQuery();
                MessageBox.Show("تم تعديل البيانات", "تم التعديل", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);

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













string JSS_connetionString001 = null;
            OleDbConnection JSS_connection001;
            OleDbDataAdapter JSS_oledbAdapter001 = new OleDbDataAdapter();
            string JSS_sql001 = null;
            JSS_connetionString001 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|/je_salestoredb.mdb;Persist Security Info=True;Jet OLEDB:Database Password=JE2014";
            JSS_connection001 = new OleDbConnection(JSS_connetionString001);
            JSS_sql001 = "delete from product_companies where prodc_ID = '" + label3.Text + "'";
            try
            {
                JSS_connection001.Open();
                JSS_oledbAdapter001.DeleteCommand = JSS_connection001.CreateCommand();
                JSS_oledbAdapter001.DeleteCommand.CommandText = JSS_sql001;
                JSS_oledbAdapter001.DeleteCommand.ExecuteNonQuery();
                MessageBox.Show("تم حذف المجموعة", "تم الحذف", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign);

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





请帮助



please help

推荐答案

显而易见的原因将是label3.Text与数据库中的任何 prodc_ID 不匹配。如果它不匹配,SQL将不会影响任何记录,并赢了; t抛出错误因为eit不是错误!



使用调试器查看在你执行指令之前,你的命令字符串是什么样子,然后直接在DB中查看是否有匹配的记录。



BTW:不连接用于构建SQL命令的字符串。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
The obvious reason would be that label3.Text does not match any prodc_ID in your database. If it doesn't match, SQL will not affect any records, and won;t throw an error becaus eit isn't an error!

Use the debugger to see exactly what your command string looks like immediately before you execute the instruction, then look in the DB directly to see if there are any matching records.

BTW: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.


这篇关于为什么更新和删除代码不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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