C#中如何把查询的值赋给变量?

2024-12-12 11:44:27
推荐回答(1个)
回答1:

假设你的表是这个样子的(代码仅供参考) table1(ID,主键;RuleID) table2(ID,主键,RuleID) using (SqlConnection con = new SqlConnection(“镇铅……”)) { con.Open(); SqlCommand cmd = new SqlCommand("select RuleID from table1 where ID="+你的ID,第一个表,con); string result = cmd.ExecuteScaler().ToString(); cmd.CommandText = "select RuleID from table1 where ID="+你历唤的ID,第二个表; string result2 = cmd.ExecuteScaler().ToString(); if(result.Equals(result2)) { cmd.CommandText="update 第二张表 set RuleID="+result+" where ID="第二张表的要修改的主键御烂好值"; cmd.ExecuteNonQuery(); } }