可能有点乱,我先说明一下。我有两个dropdownlist,逻辑相同,所以我举其中一个例子。其ID分别为pclass, pstyle。产品列表dropdownlist,ID为pclass。 里面的值都是字母诸如 men'cloth,我想将这些值写入表products的classid列(类型为int)。classid对应表classtab的id..。 请问该怎么写才是?我用string strclass = this.pclass.SelectedValue;string strSql = "Insert into products(classid)values ('" + strclass +"')"; 这样肯定报错,实际上我想实现的效果,还应该涉及到classtab表的id字段。那应该怎么写才好? 这个问题困扰我半天了, 求高人解答! 急...

解决方案 »

  1.   

    你绑定下拉框时,直接给下拉框的值绑定为classid,显示文本绑定为名字,如:
    pclass.DataSource=..;
    pclass.DataValueField="classid";
    pclass.DataTextField="classname";
    pclass.DataBind();这样绑定后,取值时,取的就是下拉框对应的classid值了。就可以直接这样了。
    string strclass = this.pclass.SelectedValue;string strSql = "Insert into products(classid)values ('" + strclass +"')";
      

  2.   

    lz意思是这个 men'cloth 插入数据库有问题?写个函数把 men'cloth 变成 men''cloth 再试试
      

  3.   


    string strSql = "Insert into products(classid)values select classid from pstyle where style=('" + strclass +"')";
      

  4.   

    感谢LS诸位解答。 可能我表达的不够清楚。
    我现在需要将dropdownlist里的值(man's cloth , woman's cloth等等)写入products表的classid字段。很明显这些字段为varchar类型,而products表的classid的字段为int! 问题来了,我如果将man's cloth 添加到products里的classid里肯定出错,varchar!=int。
    诸位没注意到,我说的classid对应的是classtab表的id,而:我的dropdownlist里面的值绑定的就是classtab表的class字段! 所以无论如何,肯定与classtab表相关联。我的问题就是,怎样将man's cloth等dropdownlist里的值(类型为varchar,已经绑定在classtab表class字段)写入到products的classid(类型为int,对应classtab表的id字段)里?估计说得更乱了- - 但是真的很急,感觉这个蛮基础的.. 不过我对MSSQL不熟,只会写c#。
    请高手们帮忙..谢谢!
      

  5.   


    --如1楼用程序可以,也可以直接改下SQL语句。
    string strSql = "Insert into products(classid) select id from classtab where [class]='" + strclass +"'";
      

  6.   


    你表达确实有问题, 确实叫人看不懂, 还不如贴测试数据跟想要的结果来的直接"将man's cloth等dropdownlist里的值(类型为varchar,已经绑定在classtab表class字段)写入到products的 classid(类型为int,对应classtab表的id字段)里?"  云里雾里
      

  7.   

    谢谢解答。
    请问如果需要写入多个值呢?比如除了classid,还有一个styleid,该怎么写?
      

  8.   

    谢谢解答。
    请问如果需要写入多个值呢?比如除了classid,还有一个styleid,该怎么写?
    -------------
    styleid从哪来?哪个表的?
      

  9.   


    和classid一样,都是products表的。 相对应的即styletab的id字段。就是说,在你给出的这句:
    string strSql = "Insert into products(classid) select id from classtab where [class]='" + strclass +"'";
    除了classid,还要添加styleid。SQL语句我不太会..
      

  10.   


    string strSql = "Insert into products(classid,styleid) select a.id,b.id from classtab a,styletab b where a.[class]='" + strclass +"'
    and b.style='"+strstyle+"'";
      

  11.   


    感谢解答.. 不知道里的语句里的a和b是? 定义出来的变量还是说代指某个表呢,还没调试不知道能成功不。
    还有一个需求..希望解答..
    就是除了classid和styleid,还有其他的诸如name、price要写进products表。这些值和其他表没有关联,可以直接写进去。比如
    string strSql = "insert into products (price,name) values ('"+strprice+"','"+strname+"')";
    那么,怎样和以上那句合并为一句呢? 就剩这个问题啦.. 麻烦您了。
      

  12.   


    //这里的a,b不用管。是classtab和styletab的别名而已。。string strSql = "Insert into products(classid,styleid,price,name) select a.id,b.id,'"+strprice+"','"+strname+"' from classtab a,styletab b where a.[class]='" + strclass +"'
    and b.style='"+strstyle+"'";
      

  13.   


    用了您给的语句, 数据写不进去了.. 原来还写的进去。
    我看您的语句里似乎没有values, 是不是有错误呢?
      

  14.   


    你自己对照下字段,看你数据表中字段名是不是这个啊。你没说字段。我猜的。string strSql = "Insert into products(classid,styleid,price,name) 
    select a.id,b.id,'"+strprice+"','"+strname+"' from classtab a,styletab b where a.[class]='" + strclass +"' and b.style='"+strstyle+"'";
    //上面的b.style就是指styletab表的字段
      

  15.   


    这个语句不行啊 根本读不进去。 不是要用insert into value句式才行么?
    字段没错的,我检查过了。
      

  16.   


    两种语句效果是一样的,insert into table select ...可以从别的表中查数据
      

  17.   


    你先用showmessge(strSql)看看SQL语句是什么样的,把show出来的语句放查询分析器里执行看看能不能插入记录
      

  18.   

    这个语句不行啊 根本读不进去。 不是要用insert into value句式才行么?
    字段没错的,我检查过了。
    -----------------
    插入有两种方式:
    insert into 表名(字段名1,字段名2) values(值1,值2)
    insert into 表名(字段名1,字段名2) select 值1,值2 
      

  19.   


    试过了,两句效果相同。
    但是结合起来就是插不进去,快疯了string strSql2 = "Insert into products(xtid,xsid,model,pro,xh,tj,fabric,lining,art,fob,prgm,pimg,addtime,prgxx) select a.xtypeid,b.xstyleid,'" + strpkh + "','" + strppm + "','" + strxh + "','" + strtj + "','" + strpml + "','" + strpll + "','" + strpgy + "','" + strpfob + "','" + strpqdl + "','" + strad1 + "','" + DateTime.Now + "','" + fck + "' from xtype a,xstyle b where a.xtype='" + strxtid + "'and b.xstyle='" + strxsid + "'";你给的句式就是这样 但是就是插不进去。 数据库看不到记录。
    而用values就可以了。
    但values达不到将xtid和xsid的值插入另一张表的效果。
    唉郁闷死了
      

  20.   

    语句看起来没什么问题。楼主做开发的不会调试么?
    跟踪下,看最终执行的sql语句到底是什么把执行的sql语句拿到查询分析器里面执行。就知道错误在哪了。