gpt4 book ai didi

c# - 当前上下文中不存在名称 'data'

转载 作者:太空宇宙 更新时间:2023-11-03 17:20:21 26 4
gpt4 key购买 nike

我试图在一个类中生成一个 try catch 方法,但我遇到了这个错误信息,请帮我解决这个问题。我的课是

public string Countryadd(string country, string id)
{

try
{
string data="0";
string qry1 = "select Country from Country where Country='" + country + "'";//Checking weather txtcountry(Country Name) value is already exixst or not. If exist return 1 and not exists go to else condition
SqlDataReader dr = conn.query(qry1);
if (dr.Read())
{
return data = "1";
}
else//If Country Name Not Exists
{
string qry = "insert into Country values('" + id + "','" + country + "')";//Insertin data into database Table Country
conn.nonquery(qry);
}

}
catch (Exception Ex)
{
ShowPopUpMsg(Ex.Message);
}
return data;
}

最佳答案

您需要将数据的定义放在try block 之前:

   string data="0";

try {

{} 括号定义变量的范围。

您只能访问该范围内的变量。

关于c# - 当前上下文中不存在名称 'data',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25520879/

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