将db列添加到文本框并将其保存在db(total)中

时间:2017-09-12 06:49:14

标签: c#

我想通过使用Update将文本框中的值添加到db中名为Quantity的列中,因此每次单击save时都会收集db(Quantity)中的集合 对不起,我的英语不好

注意:db = invoice,Table = Product 编辑:解决它,我希望这段代码对ppl有用。

         private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=NAWAF;Initial Catalog=invoice;Integrated Security=True");

        con.Open();
        SqlCommand cmd = new SqlCommand(@"INSERT INTO [invoice].[dbo].[invoice]
            ([Invoice_Number]
            ,[Inventory_ID]
            ,[Received_Date]
            ,[Supplier_Code]
            ,[Supplier_Name]
            ,[Product_Code]
            ,[Product_name]
            ,[Serial_Number]
            ,[MGF_Date]
            ,[Product_unit]
            ,[ReceivedQ])
       VALUES     
            ('" + textBox1.Text + "' ,'" + comboBox1.Text + "' , '" + textBox3.Text + "' , '" + comboBox2.Text + "' , '" + comboBox3.Text + "' , '" + comboBox4.Text + "' , '" + comboBox5.Text + "' , '" + textBox2.Text + "' , '" + textBox4.Text + "' , '" + comboBox6.Text + "' , '" + textBox6.Text + "')", con);

               cmd.ExecuteNonQuery();

               con.Close();
               con.Open();
               SqlDataAdapter sda = new SqlDataAdapter("Select Invoice_Number,Received_Date,Supplier_Code,Supplier_Name,Product_Code,Product_name,Serial_Number,MGF_Date,ReceivedQ from invoice where   Invoice_Number like '" + textBox1.Text + "'", con);
               DataTable dt = new DataTable();
               sda.Fill(dt);
               dataGridView1.DataSource = dt;
              SqlCommand ccm= new SqlCommand("UPDATE product SET quantity += '" + textBox6.Text + "' FROM product WHERE Product_name like '" + comboBox5.Text + "'", con);
               ccm.ExecuteNonQuery();
               textBox3.Text = "";
               textBox2.Text = "";
               textBox4.Text = "";
               comboBox4.Text = "";
               comboBox5.Text = "";
               comboBox6.Text = "";
               textBox6.Text = "";
               con.Close();



    }

2 个答案:

答案 0 :(得分:0)

其实我不明白你在哪个查询中遇到问题插入或更新? 你可以尝试这样插入:

string query="INSERT INTO [invoice].[dbo].[invoice]([Invoice_Number],[Inventory_ID],[Received_Date],[Supplier_Code],[Supplier_Name],[Product_Code],[Product_name],[Serial_Number],[MGF_Date],[Product_unit],[ReceivedQ])           VALUES('" + textBox1.Text + "' , '" + comboBox1.Text + "' , '" + textBox3.Text + "' , '" + comboBox2.Text + "' , '" + comboBox3.Text + "' , '" + comboBox4.Text + "' , '" + comboBox5.Text + "' , '" + textBox2.Text + "' , '" + textBox4.Text + "' , '" + comboBox6.Text + "' , '" + textBox6.Text + "')"

=new SqlCommand(query, con);

and update query Like this: 
new SqlCommand("UPDATE p SET quantity = '" + textBox6.Text + "' FROM PRODUCT p WHERE Product_name like '" + comboBox5.Text + "'")

答案 1 :(得分:0)

我找到了解决方案

SqlCommand ccm= new SqlCommand("UPDATE product SET quantity = '" + textBox6.Text + "' FROM product WHERE Product_name like '" + comboBox5.Text + "'", con)
ccm.ExecuteNonQuery();

问题是ExecuteNonQuery();我应该在每个sqlcommand之后把它放到

我以为我不应该使用它