在两个表中插入值,但在第二个表的2行中添加相同的值 [英] Inserting value in two table but it adding same value in 2 rows of second table

查看:54
本文介绍了在两个表中插入值,但在第二个表的2行中添加相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cmd.CommandText =INSERT INTO AD_DETAIL(CAT_ID,SUB_CAT_ID,AD_TITLE,AD_DESC,AD_PRICE,AD_IMG,BUY_SELL_ID,DATE,AD_POST_DATE,DELETED_AD)VALUES(+ ddlCountry.SelectedValue +,+ ddlState.SelectedValue +, '+ txtTitle.Text +','+ txtDesc.Text +','+ txtPrice.Text +','+Images /+ uniquefilename + extension +','+ rbPost。 SelectedValue +',@ date,@ postdate,0);



cmd.Parameters.AddWithValue(@ date,date.Date.ToLongDateString()) ;

cmd.Parameters.AddWithValue(@ postdate,datepost.Date.ToLongDateString());

cmd.Parameters.AddWithValue(@ ImagePath,Images /+ uniquefilename);

cmd.ExecuteNonQuery();



cmd.CommandText =INSERT INTO SELLER_DETAIL(SELLER_NAME,SELLER_EMAIL_ID,SELLER_MOB_NO ,SELLER_DEP,SELLER_FLOO R,EMP_ID)VALUES('+ txtName.Text +','+ txtEmailID.Text +',+ txtContact.Text +,'+ txtDep.Text +','+ txtFloor.Text +','+ txtEmpNo.Text +');

cmd.CommandText = "INSERT INTO AD_DETAIL (CAT_ID, SUB_CAT_ID, AD_TITLE, AD_DESC, AD_PRICE, AD_IMG, BUY_SELL_ID,DATE,AD_POST_DATE,DELETED_AD) VALUES (" + ddlCountry.SelectedValue + "," + ddlState.SelectedValue + ",'" + txtTitle.Text + "','" + txtDesc.Text + "','" + txtPrice.Text + "','" + "Images/" + uniquefilename + extension + "','" + rbPost.SelectedValue + "', @date,@postdate,0)";

cmd.Parameters.AddWithValue("@date", date.Date.ToLongDateString());
cmd.Parameters.AddWithValue("@postdate", datepost.Date.ToLongDateString());
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + uniquefilename);
cmd.ExecuteNonQuery();

cmd.CommandText = "INSERT INTO SELLER_DETAIL (SELLER_NAME, SELLER_EMAIL_ID, SELLER_MOB_NO, SELLER_DEP, SELLER_FLOOR,EMP_ID) VALUES ('" + txtName.Text + "','" + txtEmailID.Text + "'," + txtContact.Text + ",'" + txtDep.Text + "','" + txtFloor.Text + "','" + txtEmpNo.Text + "')";

推荐答案

为什么要从前端尝试两次插入数据两个表。

Why are you trying insert data two table with two times from front end.
Easily you can write a 
1.Storeprocedure with required parameters
2.Writed both of Insert query inside the storeprocedure
3.Call only storeprocedure one time with single connection 
4.It will insert to both of your table.


尝试在执行第一个查询后创建cmd的新实例。 ..类似



cmd = new SqlCommand();
try creating new instance of cmd after executing the first query ... something like

cmd = new SqlCommand() ;


Hi
在第一次查询时尝试这样使用cmd.CommandText和第二个查询也cmd.CommandText所以尝试声明不同的cmd(如cmd,cmd1)或如果你执行第一个查询你需要处理cmd,我认为cmd作为sqlcommand?如果你使用相同的对象你需要像cmd.Dispose()那样处理你再将你的查询分配给cmd.And这个在每个函数或每个查询中你需要使用dispose,关闭连接然后它只会影响其他在同一个函数中你尝试使用相同的sqlcommand对象执行2个查询,所以你已经处理或声明了新的名称cmd。









Hi Try like this in first query u use cmd.CommandText and second query also cmd.CommandText so try to declare different cmd(like cmd,cmd1)or if u execute first query u need to dispose the cmd ,i think cmd as sqlcommand ? is it if u use same object u need to dispose like cmd.Dispose() then again u assign the query to cmd.And one this in each function or each query u need to use dispose,close connection then only it not affect in other place,what u try above in same function u try to execute 2 queries with same sqlcommand object,so u have dispose or declare new name cmd.




cmd.CommandText = "INSERT INTO AD_DETAIL (CAT_ID, SUB_CAT_ID, AD_TITLE, AD_DESC, AD_PRICE, AD_IMG, BUY_SELL_ID,DATE,AD_POST_DATE,DELETED_AD) VALUES (" + ddlCountry.SelectedValue + "," + ddlState.SelectedValue + ",'" + txtTitle.Text + "','" + txtDesc.Text + "','" + txtPrice.Text + "','" + "Images/" + uniquefilename + extension + "','" + rbPost.SelectedValue + "', @date,@postdate,0)";
 
cmd.Parameters.AddWithValue("@date", date.Date.ToLongDateString());
cmd.Parameters.AddWithValue("@postdate", datepost.Date.ToLongDateString());
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + uniquefilename);
cmd.ExecuteNonQuery();
 

//Here u use cmd.Dispose(); or declare new cmd1 and use in second query.

cmd.CommandText = "INSERT INTO SELLER_DETAIL (SELLER_NAME, SELLER_EMAIL_ID, SELLER_MOB_NO, SELLER_DEP, SELLER_FLOOR,EMP_ID) VALUES ('" + txtName.Text + "','" + txtEmailID.Text + "'," + txtContact.Text + ",'" + txtDep.Text + "','" + txtFloor.Text + "','" + txtEmpNo.Text + "')";



问候

Aravind


Regards
Aravind


这篇关于在两个表中插入值,但在第二个表的2行中添加相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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