【发布时间】:2011-03-03 08:49:33
【问题描述】:
我的查询是这样的,我想插入,但使用 SQL Server 2005
insert into postad(cityid,category_id,ad_id,title,ad_description,subcategory_id)
values((select city_id from city where city_name='mumbai'),
(select cat_id from category where cat_name='automobiles & vehicles'),
(select adid from adType where adtype like'wanted'),'myad','itys grwweefa',
(select subcat_id from subcategory where category_id =
(select cat_id from category where cat_name like'automobiles & vehicles')))
谁能帮我实现这个目标
string city = ddcity.SelectedItem.ToString();
string cat = ddcat.SelectedItem.ToString();
string subcat = ddsubcat.SelectedIndex.ToString();
string adtype = ddadtype.SelectedItem.ToString();
string title = txttitle.Text.Trim();
string description = txtdescription.Text.Trim();
string video = txtvideo.Text.Trim();
DateTime date = DateTime.Now;
SqlConnection myConnection = new SqlConnection("Data Source=DELL-PC\\SQLEXPRESS;Initial Catalog=eclass;Persist Security Info=True;integrated security = true");
string cityq = "select city_id from city where city_name=" + city;
string catq = "select cat_id from category where cat_name=" + cat;
string subq = "select subcat_id from city where subcat_name=" + subcat+"and category_id="+catq;
SqlCommand storead = new SqlCommand("INSERT INTO postad " + "(cityid,category_id,ad_id,title,ad_description,subcategory_id,video,date_creation) " + " values (@city,@cat,@adtype,@title,@description,@subcat,@video,@date)", myConnection);
storead.Parameters.AddWithValue("@title",title);
storead.Parameters.AddWithValue("@city",city);
storead.Parameters.AddWithValue("@cat",cat);
storead.Parameters.AddWithValue("@subcat",subcat);
storead.Parameters.AddWithValue("@adtype",adtype);
storead.Parameters.AddWithValue("@description",description);
storead.Parameters.AddWithValue("@video",video);
storead.Parameters.AddWithValue("@date",date)
这就是它的工作方式 n id 是主键
【问题讨论】:
-
您当前的查询有什么问题?
-
它说的语法错误!!!
-
始终使用您正在使用的 DBMS 标记您的数据库相关问题
-
哦,请不要费心接受之前对您的任何问题给出的任何答案。那是非常不合适的。
-
@shweta:@Uw Concept 有点讽刺。如果他们帮助您解决了您遇到的问题,您应该返回并接受问题的答案。
标签: c# sql sql-server-2005 ado.net