本文介绍了如何返回输出参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从此方法返回输出参数但有问题。
我尝试过:
I'm trying to return an output parameter from this method but having issue.
What I have tried:
public void GetStaffName(string id, out string Name)
{
try
{
// dt = new DataTable();
if (con.State != ConnectionState.Open)
{
con.Open();
}
string query = "select * from stafflist where staffidno = @staffidno";
SqlCommand sqlCmd = new SqlCommand(query, con);
sqlCmd.Parameters.AddWithValue("@staffidno", id);
SqlDataReader dr = sqlCmd.ExecuteReader();
while (dr.Read())
{
string Name = dr["emp_name"].ToString();
// return Name;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
推荐答案
public void GetStaffName(string id, out string Name)
{
Name = "";
try
{
if (con.State != ConnectionState.Open)
{
con.Open();
}
string query = "select * from stafflist where staffidno = @staffidno";
using (SqlCommand sqlCmd = new SqlCommand(query, con))
{
sqlCmd.Parameters.AddWithValue("@staffidno", id);
SqlDataReader dr = sqlCmd.ExecuteReader();
if (dr.Read())
{
Name = dr["emp_name"].ToString();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
}
这篇关于如何返回输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!