小编典典

从SQL返回多个值到标签

sql

我有标签:

<asp:Label ID="lbl1" runat="server"></asp:Label>

后面的代码:

protected void Page_Load(object sender, EventArgs e)
{
    lbl1.Text = ImageCheck().ToString();
}

和:

protected int ImageCheck()
{
    SqlConnection connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\***.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
    string CommandText2 = "SELECT * FROM Machreta WHERE noImage = 1";
    SqlCommand command2 = new SqlCommand(CommandText2, connection);
    connection.Open();
    int check = (int)command2.ExecuteScalar();
    connection.Close();

    return check;
}

如何返回多个值?该标签仅显示单个值,但表中还有6个值。


阅读 152

收藏
2021-04-07

共1个答案

小编典典

试试这个:

    protected string ImageCheck()
    {

      var result = new StringBuilder();

    using(var connection = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\***.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"))
    {
        string CommandText2 = "SELECT * FROM Machreta WHERE noImage = 1";
        SqlCommand command2 = new SqlCommand(CommandText2, connection);
        connection.Open();

      using(var reader = command2.ExecuteReader())
      {
        while (reader.Read())
        {
          result.Append(reader.GetString(0));
        }
      }

      return result.ToString();

    }
 }

当然,这 只是 一个示例,并不能完全解决您的问题,但是应该作为一个起点:)

2021-04-07