sql在两个表中插入记录 [英] sql insert records in two tables

查看:76
本文介绍了sql在两个表中插入记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在这里,我使用代码来提交按钮,并希望通过此按钮在两个表中插入记录
1.客户
2.order

在这里,通过下拉列表通过文本框和产品名称为客户插入记录,产品的价格来自数据库.我将基于将计算和计算total_amount的比率从文本框中插入数量,并在订单表中插入记录,我正在使用以下代码.
此代码仅更新单个表

Hi,

Here I am using a code for submit button with the help of this button I want to insert records in two table
1.customer
2.order

Here insert records for customer by text box and product name using by drop down list and rate comes from database I insert quantity from text box on the basis of rate that will calculate and make total_amount and insert records in order table I am using following code.
This code updating only single table

protected void btnSubmit_Click(object sender, EventArgs e)
        {
            SqlConnection con=new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
            SqlCommand cmd;
            DataSet ds=new DataSet();
          
            try
            {
                cmd = new SqlCommand("insert into customer(customer_name,customer_phone,customer_address)values(@customer_name,@customer_phone,@customer_address)", con);
                //cmd.Parameters.Add("@product_name", SqlDbType.NVarChar).Value = DropDownList1.Items.ToString();
                cmd.Parameters.Add("@customer_name", SqlDbType.NVarChar).Value = txtName.Text;
                cmd.Parameters.Add("@customer_phone", SqlDbType.NVarChar).Value = TxtPhone.Text;
                cmd.Parameters.Add("@customer_address", SqlDbType.NVarChar).Value = txtAddress.Text;
                
                cmd = new SqlCommand("insert into product_order(product_name,product_quantity,product_amount)values(@product_name,@product_quantity,@product_amount)", con);
                cmd.Parameters.Add("@product_name", SqlDbType.NVarChar).Value = DropDownList1.SelectedItem.Text;
                cmd.Parameters.Add("@product_quantity", SqlDbType.NVarChar).Value = txtqty.Text;
                cmd.Parameters.Add("@product_amount", SqlDbType.NVarChar).Value = txtAmount.Text.ToString();
                con.Open();
                cmd.ExecuteNonQuery();
                Response.Write("yr records updated..");
                //da = new SqlDataAdapter("select * from product", con);
                //GridView1.DataSource = ds;
                //GridView1.DataBind();
                cmd.Dispose();
             }
            catch(Exception exe)
            {
                Response.Write(exe);
            }
          
            ds.Dispose();
            con.Close();
            
            
        }

推荐答案

您有一个cmd对象,在创建了客户之后,您要在使用下一个cmd = new SqlCommand命令插入记录之前将其删除. .

在设置命令以插入订单之前,必须先调用cmd.ExecuteNonQuery();.

干杯.
You have one cmd object and after you create the customer you get rid of it before you insert the record with your next cmd = new SqlCommand command.

You have to call cmd.ExecuteNonQuery(); before you set up your command to insert the order.

Cheers.


快速答案:您需要在第二个cmd = new SqlCommand()之前放置另一个cmd.ExecuteNonQuery().您还应该将其包装在事务中.
Quick answer: you need to put another cmd.ExecuteNonQuery() prior to the 2nd cmd = new SqlCommand(). You should also wrap this in a transaction. That should get you going.


使用两个不同的命令,例如

Use two Different Command Like

SqlCommand cmd = new SqlCommand();
-- assign first Query to this command

SqlCommand cmd1 = new SqlCommand();
-- assign second Query to this command

then use
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();


谢谢


这篇关于sql在两个表中插入记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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