gpt4 book ai didi

c# - 方法错误,用mysql

转载 作者:行者123 更新时间:2023-11-29 02:47:12 24 4
gpt4 key购买 nike

<分区>

我的函数编码如下,但出现错误,我试图通过返回字符串的方法从数据库中获取项目详细信息。

    public static string GetItemDetails(int i_id, string i_name)
{
MySqlConnection SQL_con = new MySqlConnection(VPconfig.sql_conns);
string[] ItemData = null;
i_name = i_name == null ? string.Empty : i_name;
i_id = i_id == 0 ? 0 : i_id;

try
{
if(i_id != 0)
{
//get Item details by ID
string query = "SELECT * FROM items WHERE id="+i_id;
MySqlCommand xv = new MySqlCommand(query, SQL_con);
SQL_con.Open();
MySqlDataReader rdr = xv.ExecuteReader();
while(rdr.Read())
{
ItemData[0] = rdr.GetString("id");
ItemData[1] = rdr.GetString("iname");
ItemData[2] = rdr.GetString("iprice");
}
SQL_con.Close();

string master = ItemData[0] +"*"+ ItemData[1] +"*"+ ItemData[2];

return master;//Return format ID*NAME*PRICE
}
else if(i_name != string.Empty)
{
//get Item details by Name
string query = "SELECT * FROM items WHERE iname=" + i_name;
MySqlCommand xv = new MySqlCommand(query, SQL_con);
SQL_con.Open();
MySqlDataReader rdr = xv.ExecuteReader();
while (rdr.Read())
{
ItemData[0] = rdr.GetString("id");
ItemData[1] = rdr.GetString("iname");
ItemData[2] = rdr.GetString("iprice");
}
SQL_con.Close();

string master = ItemData[0] + "*" + ItemData[1] + "*" + ItemData[2];

return master;//Return format ID*NAME*PRICE
}
else
{
MessageBox.Show("No Items! found with ID/Name: " + i_id + i_name);
}

//if nothing
return null;
}
catch(Exception ex)
{
MessageBox.Show("Error Code: 17 "+ex.Message);

//if nothing
return null;
}

现在,如果我传递以下参数,则显示错误“错误代码:17 对象引用未设置为对象的实例。”这基本上来自捕获,

string xv = VPfunctions.GetItemDetails(5, string.Empty);
ta.Text = xv;

谢谢。

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