我在c#编码,我想比较"数量" in" sell"数据表与"买"数据表和if"数量" in" buy"表格大于我需要减去"购买"来自"出售"和"数量"或者继续。请告诉我们如何获取比较值。我已经编码如下,但显示错误。
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
var sql = @"select scriptname,accnum,Quantity,price from transac where transactio = 'Sell' and scriptname = '" + TextBox2.Text + "' and accnum ='" + TextBox1.Text + "'";
var sqll = @"select scriptname,accnum,Quantity,price from transac where transactio = 'Buy' and scriptname ='" + TextBox2.Text + "' and accnum ='" + TextBox1.Text + "'";
var da = new SqlDataAdapter(sqll, conn);
var dataTablebuy = new DataTable();
da.Fill(dataTablebuy);
var dataAdapter = new SqlDataAdapter(sql, conn);
var dataTablesell = new DataTable();
dataAdapter.Fill(dataTablesell);
foreach (DataRow row in dataTablesell.Rows)
{
foreach (DataRow rw in dataTablebuy.Rows)
{
if (rw["Quantity"] > row["Quantity"])
{
rw["Quantity"] = rw["Quantity"] - row["Quantity"];
}
else
{
break;
}
}
}
}
catch (System.Data.SqlClient.SqlException sqlEx)
{
Response.Write("error" + sqlEx.ToString());
}
catch (Exception ex)
{
Response.Write("error" + ex.ToString());
}
}
答案 0 :(得分:0)
试试这个:
if (double.Parse(rw["Quantity"].ToString()) > double.Parse(row["Quantity"].ToString()))
{
rw["Quantity"] = double.Parse(rw["Quantity"].ToString()) - double.Parse(row["Quantity"].ToString())
}
答案 1 :(得分:0)
我只是想提供更可行的选择,以备你需要的时候。
我刚刚在查询中介绍了您的场景。我根据您的需要加入了表格并获得了数据
您只需使用此查询获取数据。
请查询c#代码
选择T1.scriptname,T1.accnum,(情况(T2.Quantity> T1.Quantity)然后(T2.Quantity-T1.Quantity),否则T1.Quantity结束)作为数量,T1.price 来自transac T1,transac T2 哪里 T1.transactio ='卖' 和 T2.transactio ='买' 和 T1.scriptname ='TextBox2.Text' 和 T1.accnum ='TextBox1.Text' 和 T2.scriptname ='TextBox2.Text' 和 T2.accnum ='TextBox1.Text' 和 T1.scriptname = T2.scriptname 和 T2.accnum = T2.accnum