gpt4 book ai didi

c# - 有什么想法可以缩短这段代码吗?

转载 作者:行者123 更新时间:2023-11-29 01:11:39 25 4
gpt4 key购买 nike

string menuRecipe = "SELECT * FROM recipelist WHERE menu_ID = '" + menuID + "'";
MySqlCommand recipeCmd = new MySqlCommand(menuRecipe, db.mycon);
MySqlDataReader recipeDr = recipeCmd.ExecuteReader();
string[] info = new string[8];
while (recipeDr.Read())
{
info[1] = (recipeDr["recipe_ID"].ToString());
info[2] = (recipeDr["stock_ID"].ToString());
info[4] = (recipeDr["recipe_quantity"].ToString());
string stockName = "SELECT stock_name FROM stocksdb WHERE stock_ID = '" + info[2] + "'";
cmd = new MySqlCommand(stockName, db.mycon);
MySqlDataReader nameDr = cmd.ExecuteReader();
while (nameDr.Read())
{
info[3] = (nameDr["stock_name"].ToString());
}
this.recipeList.Items.Add(new ListViewItem(new string[] { info[1], info[2], info[3], info[4] }));
}

我主要想减少使用多个 SQL 命令字符串的必要,尽管我不知道如何以及如何将它们隔离到各自的位置,因为有些可能会困惑。

最佳答案

您可以将查询更改为

SELECT r.*, s.stock_name FROM recipelist r
LEFT OUTER JOIN stocksdb s ON r.stock_ID = s.stock_ID
WHERE menu_ID = @parameter

关于c# - 有什么想法可以缩短这段代码吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13964760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com