我想用代码实现在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的值但没有使用
先从数据表中拿出相应的数据 数量 然后让表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的值但没有使用
这样拼接才对
刚刚是相加,现在是相减lz拼接sql语句是乱写, ((TextBox)(e.Item.Cells[3].Controls[0])).Text"---这个是string变量不是sql变量,你居然放到引号里面去了
string 数量 命名还一样,怎么能编译过的那...
string 数量
先吧dk sa转换为int类型就可以加减了string 数量 = (Convert.ToInt32(dk)-Convert.ToInt32(sa)+数量1).ToString();这样就可以了
先吧dk sa转换为int类型就可以加减了string 数量 = (Convert.ToInt32(dk)-Convert.ToInt32(sa)+数量1).ToString();这样就可以了
-----------
瞎说,问题都没看清楚就乱回答
”(+dk+)"-"(+sa+)这个根本就不是减号的问题,而是sql语句拼接有问题
dk,sa是string变量不是sql变量不能放在引号内!!!
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的值但没有使用