0

在庫システムに問題があります。1つのテキストボックスが関係している場合、それは完全に機能しました。ただし、2 つ以上のテキスト ボックスが含まれている場合、具体的にはエラーが表示されます。(Knooks and Cranies は、入力したサプライヤーの例です。)

このエラーに遭遇したのはこれが初めてなので、何が問題なのかよくわかりません。

ここに私のコードがあります:

namespace SupplyRequestAndInventoryManagementSystem
{
    public partial class DI_Assets : Form
    {
        connection con = new connection();
        MySqlCommand cmd;
        MySqlDataReader reader;

        public DI_Assets()
        {
            InitializeComponent();
        }

        //load data from database
        private void DI_Assets_Load(object sender, EventArgs e)
        {
            loadDepartment();
        }

        //checker
        private void ListDelivery_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ListDelivery.SelectedItems.Count > 0)
            {
                ListViewItem itm = ListDelivery.SelectedItems[0];
                lblAssetIDChecker.Text = itm.SubItems[4].Text;
            }
        }

        //load data from supplier
        private void btnSearch_Click(object sender, EventArgs e)
        {
            loadtbl();
        }

        //add button
        private void btnAdd_Click(object sender, EventArgs e)
        {
            DialogResult dg = MessageBox.Show("Are you sure you want to add new Delivery Information?", "Message", MessageBoxButtons.YesNo);
            if (dg == DialogResult.Yes)
            {
                if (SuppNameCombo.Text == "" || txtProdName.Text == "" || txtProdBrand.Text == "" || txtQty.Text == "" || DTPReceived.Text == "")
                {
                    MessageBox.Show("Don't leave blanks!");
                }
                else
                {
                    con.Close();
                    cmd = new MySqlCommand("Select * from deliver_mat where Del_MSupplier ='" + SuppNameCombo.Text + "' and Del_MName='" + txtProdName.Text + "' and Del_MBrand ='" + txtProdBrand.Text + "' and  Del_MQty= '" + txtQty.Text + "' and Del_MReceived='" + DTPReceived.Text + "'", con.con);
                    con.Open();
                    reader = cmd.ExecuteReader();
                    if (reader.Read())
                    {
                        MessageBox.Show("Delivery Information already exist, sepcify a new one!");
                    }
                    else
                    {
                        addSection();
                        MessageBox.Show("Delivery Information successfully added!");
                        loadtbl();
                        txtProdName.Text = "";
                        txtProdBrand.Text = "";
                        txtQty.Text = "";
                        DTPReceived.Text = "";

                    }

                }
            }
            else
            {
                MessageBox.Show("Adding new Delivery Information has been cancelled!");
            }

        }

        //update button
        private void btnUpdate_Click(object sender, EventArgs e)
        {
            DialogResult dg = MessageBox.Show("Are you sure you want to update the section?", "Message", MessageBoxButtons.YesNo);
            if (dg == DialogResult.Yes)
            {
                if (SuppNameCombo.Text == "" && txtProdName.Text == "" && txtProdBrand.Text == "" && txtQty.Text == "" && DTPReceived.Text == "")
                {
                    MessageBox.Show("Please choose section to be updated!");
                }
                else
                {
                    updateSection();
                    MessageBox.Show("Section has been successfully updated!");
                    loadtbl();
                    SuppNameCombo.Text = "";
                    txtProdName.Text = "";
                    txtProdBrand.Text = "";
                    txtQty.Text = "";
                    DTPReceived.Text = "";
                }
            }
            else
            {
                MessageBox.Show("Updating section has been cancelled!");
            }
        }

        //----------------------------------------------------------------------------------------------


        //Retrieving Data from DB to listview
        void loadtbl()
        {
            con.Close();
            DataTable table = new DataTable();
            cmd = new MySqlCommand("Select * from deliver_assets where Del_ASupplier = '" + SuppNameCombo.Text + "'", con.con);
            con.Open();
            reader = cmd.ExecuteReader();

            ListDelivery.Items.Clear();
            while (reader.Read())
            {
                ListViewItem item = new ListViewItem(reader["Del_AName"].ToString());
                item.SubItems.Add(reader["Del_ABrand"].ToString());
                item.SubItems.Add(reader["Del_AReceived"].ToString());
                item.SubItems.Add(reader["Del_AQty"].ToString());
                item.SubItems.Add(reader["Del_aID"].ToString());
                item.SubItems.Add(reader["Del_ASupplier"].ToString());
                ListDelivery.Items.Add(item);
            }
        }

        //Load Data to combo box
        void loadDepartment()
        {
            con.Close();
            DataTable table5 = new DataTable();
            cmd = new MySqlCommand("Select Supp_Name from supply", con.con);
            con.Open();
            reader = cmd.ExecuteReader();
            table5.Load(reader);
            foreach (DataRow row in table5.Rows)
            {
                SuppNameCombo.Items.Add(row["Supp_Name"]);
            }
        }

        //add function
        void addSection()
        {
            con.Close();
            cmd = new MySqlCommand("insert into deliver_assets(Del_AName, Del_ABrand, Del_AReceived, Del_AQty, Del_Asupplier) values('" + txtProdName.Text + "', '" + txtProdBrand.Text + "', '" + DTPReceived.Text + "', '" + txtQty.Text + "', '" + SuppNameCombo.Text + "')", con.con);
            con.Open();
            reader = cmd.ExecuteReader();
        }

        //update function
        void updateSection()
        {
            con.Close();
            cmd = new MySqlCommand("update deliver_assets set Del_ASupplier ='" + SuppNameCombo.Text + "' and  Del_AName ='" + txtProdName.Text + "' and Del_ABrand ='" + txtProdBrand.Text + "' and  Del_AQty ='" + txtQty.Text + "' and  Del_AReceived ='" + DTPReceived.Text + "'  where Del_aID ='" + lblAssetIDChecker.Text + "'", con.con);
            con.Open();
            reader = cmd.ExecuteReader();
        }








    }
}
4

1 に答える 1

0

コードには、データベース タスクを処理する際に避けるべき多くのエラーが含まれています。

  • 破棄可能なオブジェクト (特に MySqlConnection) のグローバル変数の使用
  • パラメータを使用しない
  • Update ステートメントの構文が正しくありません
  • 挿入/更新クエリを実行するための不適切なメソッドの使用
  • 接続を確立するためにすべてを行う方法を使用しようとしています (最初に関連)
  • 特定のデータ型を持つ列が文字列をその値として喜んで受け入れると考える

例を挙げて、更新コードを書き直そうとします

    //update function
    void updateSection()
    {
        string cmdText = @"update deliver_assets 
            set Del_ASupplier =@sup.
                Del_AName = @name,
                Del_ABrand = @brand
                Del_AQty = @qty
                Del_AReceived = @recv
             where Del_aID = @id";
        using(MySqlConnection con = new MySqlConnection(.....))
        using(MySqlCommand cmd = new MySqlCommand(cmdText, con))
        {
           cmd.Parameters.Add("@sup", MySqlDbType.VarChar).Value = SuppNameCombo.Text;
           cmd.Parameters.Add("@name", MySqlDbType.VarChar).Value = txtProdName.Text;
           cmd.Parameters.Add("@brand", MySqlDbType.VarChar).Value =  txtProdBrand.Text; 
           cmd.Parameters.Add("@qty", MySqlDbType.VarChar).Value = Convert.ToDouble(txtQty.Text);
           cmd.Parameters.Add("@recv", MySqlDbType.VarChar).Value = DTPReceived.Text;
           cmd.Parameters.Add("@id", MySqlDbType.Int32).Value =  Convert.ToInt32(lblAssetIDChecker.Text);
           con.Open();
           int rowsUpdated = cmd.ExecuteNonQuery();
           if(rowUpdated > 0)
               MessageBox.Show("Record updated");
      }
   }

列の正しいデータ型について確信が持てないことに注意してください。上記の例に示されている MySqlDbType 値を変更して、列と互換性のある DataType でパラメーターを作成する必要があります。

于 2016-01-02T15:38:42.063 に答える