Arrays 返回整个数组的值
这是我从数据库中获取数据的代码Arrays 返回整个数组的值,arrays,return-value,Arrays,Return Value,这是我从数据库中获取数据的代码 public string showSkeds(string id) { DataTable dt = new DataTable(); SqlCommand cmd = new SqlCommand("showSkeds", con); cmd.Parameters.Add("@id", SqlDbType.NVarChar, 100).Value = id; cmd.CommandType = CommandType.Store
public string showSkeds(string id)
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("showSkeds", con);
cmd.Parameters.Add("@id", SqlDbType.NVarChar, 100).Value = id;
cmd.CommandType = CommandType.StoredProcedure;
dt = hlp.resultHelper(cmd);
int count = dt.Rows.Count;
List<string> value = new List<string>();
for (int i = 0; i < count; i++)//rows
{
value.Add(dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString());
}
string[] result = value.ToArray();
return **result**;
}
公共字符串显示框(字符串id)
{
DataTable dt=新的DataTable();
SqlCommand cmd=新的SqlCommand(“showSkeds”,con);
cmd.Parameters.Add(“@id”,SqlDbType.NVarChar,100).Value=id;
cmd.CommandType=CommandType.storedProcess;
dt=hlp.ResultlPer(cmd);
int count=dt.Rows.count;
列表值=新列表();
对于(int i=0;i
显然,结果有一个错误,因为它无法将字符串[]转换为字符串。如何将值返回为纯文本?返回数组值是否可能或是否正确?根据您在注释中的说明,您似乎只需要在函数本身的声明中调整返回类型(仔细查看第一行):
public string[]showSkeds(字符串id)
{
DataTable dt=新的DataTable();
SqlCommand cmd=新的SqlCommand(“showSkeds”,con);
cmd.Parameters.Add(“@id”,SqlDbType.NVarChar,100).Value=id;
cmd.CommandType=CommandType.storedProcess;
dt=hlp.ResultlPer(cmd);
int count=dt.Rows.count;
列表值=新列表();
对于(int i=0;i
由于result
是一种string[]
类型,因此第一行(public string[]..
)也应该是string[]
类型
public
,private
之后的类型正在声明函数要返回的数据类型,并且期望与return
行匹配,如果可能的话。为什么不能将方法本身声明为返回字符串数组,alapublic string[…
?我确实需要(string id)在那里,如何将其合并到代码中?数组。toString(result)
将是数组的文本表示形式(元素之间用方括号和逗号)。这是你想要的还是你想要的所有内容?对于该String.join(“,result)
String id
是发送到函数中的参数,而public String[]showSkeds…
将返回字符串数组。您需要返回什么是一个更好的问题?我需要返回正在读取的数组的全部数据@谢谢你,先生。很少调整,但我希望这也能帮助那些在使用数组时遇到困难的人。
public string[] showSkeds(string id)
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("showSkeds", con);
cmd.Parameters.Add("@id", SqlDbType.NVarChar, 100).Value = id;
cmd.CommandType = CommandType.StoredProcedure;
dt = hlp.resultHelper(cmd);
int count = dt.Rows.Count;
List<string> value = new List<string>();
for (int i = 0; i < count; i++)//rows
{
value.Add(dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString());
}
string[] result = value.ToArray();
return result;
}