日期:2014-05-18  浏览次数:20511 次

数据库相加减问题 解决就散分
我想用代码实现在Datagird上修改数据   来修改数据表的数据
先从数据表中拿出相应的数据   数量   然后让表1的数据减去表2的数据在加上现在Datagird上修改后的数据     我就是这个计算怎么弄不会
string   数量1   =((TextBox)(e.Item.Cells[6].Controls[0])).Text.Trim();
string   dk= "select   数量   from   表1   where   代号   like   ((TextBox)(e.Item.Cells[3].Controls[0])).Text ";
string   sa= "select   数量   from   表2   where   代号   like   ((TextBox)(e.Item.Cells[3].Controls[0])).Text ";
string   数量= "(+dk+) "- "(+sa+) "+ "+数量1+ ";//说string不能相加
string   数量= "(+dk+) "+ "(+sa+) "+ "+数量1+ ";//说定义了dk,sa的值但没有使用

------解决方案--------------------
沙发 ,帮忙顶
------解决方案--------------------
“select a.数量+b.数量+“+数量1+”from 表1 a,表2 b where id like ‘“+(TextBox)(e.Item.Cells[3].Controls[0])).Text;
这样拼接才对

------解决方案--------------------
(+dk+) "- "(+sa+)这里不能使用减号
string 数量 命名还一样,怎么能编译过的那...
string 数量

------解决方案--------------------
string 数量= "(+dk+) "- "(+sa+) "+ "+数量1+ ";//说string不能相加
先吧dk sa转换为int类型就可以加减了

string 数量 = (Convert.ToInt32(dk)-Convert.ToInt32(sa)+数量1).ToString();

这样就可以了


------解决方案--------------------
Convert.Toint16(....) 类型转换
------解决方案--------------------
楼主你这个错误太多了..

string 数量1 =((TextBox)(e.Item.Cells[6].Controls[0])).Text.Trim();
string dk= "select 数量 from 表1 where 代号 like " + ((TextBox)(e.Item.Cells[3].Controls[0])).Text ";
//这里要连一次数据库的才能计算出来这个数量,然后赋给一个整形变量
//大致的步骤
SqlConnection cn = new SqlConnection( "server=.;uid=sa;pwd=;database=yourserver ");
SqlCommand cmd = new SqlCommand(dk, cn);
cn.open();
int i = Convert.ToInt32(cmd.ExecuteScalar()); //请确认你的查询只返回一个值
cn.Close();
string sa= "select 数量 from 表2 where 代号 like " + ((TextBox)(e.Item.Cells[3].Controls[0])).Text ";
//这里也要连一次数据库的才能计算出来这个数量,然后赋给一个整形变量
//下面才能进行运算
string 数量= "(+dk+) "- "(+sa+) "+ "+数量1+ ";//说string不能相加

string 数量= "(+dk+) "+ "(+sa+) "+ "+数量1+ ";//说定义了dk,sa的值但没有使用