我需要以下代码逻辑的语法帮助:

我有一个代码块,可从数据库中获取电子邮件地址。电子邮件地址需要分配给带有逗号分隔的字符串变量strEmailAddress。

我的代码是:

SqlConnection conn = new SqlConnection(strConn);
string sqlEmailAddress = "usp_Get_Email_Address";
SqlCommand cmdEmailAddr = new SqlCommand(sqlEmailAddress, conn);
cmdEmailAddr.CommandType = CommandType.StoredProcedure;
con.Open();
SqlDataReader sqlDREmailAddr = cmdEmailAddr.ExecuteReader();


如何遍历记录并将结果存储在以逗号分隔的strEmailAddress中?

最佳答案

使用SqlDataReader.Read方法:

while (sqlDREmailAddr.Read())
{
 ...
 // Assumes only one column is returned with the email address
 strEmailAddress = sqlDREmailAddr.GetString(0);
}

10-08 04:48