C# SQL 未显示我绘制的所有数据
本文关键字:数据 绘制 SQL 显示 | 更新日期: 2023-09-27 18:37:04
嗨,我无法在标签中显示表格的所有结果。
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = MyConnectionString;
myConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT CN.ClientID, CN.GivenName1, CN.Surname, CI.DateOfBirth FROM [RioOds].dbo.ClientIndex CI LEFT JOIN [RioOds].[dbo].[ClientName] CN ON CN.ClientID = CI.ClientID AND CN.AliasType = '1' AND CN.EndDate IS NULL WHERE CN.ClientID =" + ClientIDTxt.Text + ";", myConnection);
ResultsLabel.Text = cmd.ExecuteScalar().ToString();
我正在尝试将查询结果写入结果标签,仅显示客户端 ID...有什么帮助或想法吗?
首先
..你正在做什么来尝试这些代码is not a good way to doing such
..但如果你只想要解决方案,那么这里是解决方案..
SqlConnection myConnection = new SqlConnection();
myConnection.ConnectionString = "";
myConnection.Open();
SqlCommand cmd = new SqlCommand("SELECT CN.ClientID, CN.GivenName1, CN.Surname, CI.DateOfBirth FROM [RioOds].dbo.ClientIndex CI LEFT JOIN [RioOds].[dbo].[ClientName] CN ON CN.ClientID = CI.ClientID AND CN.AliasType = '1' AND CN.EndDate IS NULL WHERE CN.ClientID =" + ClientIDTxt.Text + ";", myConnection);
var reader = cmd.ExecuteReader();
StringBuilder sb = new StringBuilder();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; i++)
{
if (i!=0)
{
sb.Append(",");
}
sb.Append(reader[i].ToString());
}
sb.AppendLine();
}
ResultsLabel.Text = sb.ToString();
注意:ExecuteScalar 仅返回first column of first row
(仅一个值)作为结果