使用计数查询结果绑定标签

时间:2017-06-09 17:22:15

标签: c# mysql asp.net

我正在从我的query数据库中执行Access,结果为count,如果我没错,则计数数组给出array,所以首先我需要将array转换为int并将节目转换为label

这是我的query它位于名为connections的文件夹中,里面是class个名称genera-conections,我在其中创建了所有querys需要

/***********GET ALL THE FAKE TOOLS FROM FCH*************/
public class area_success_FCH
{
    public DataTable Sfchconnect()
    {
        string myConnectionString = @"C:\\Users\\gutiece\\Desktop\\database\\" + "Database1.accdb";

        DataTable SfchTable = new DataTable();

        OleDbConnection connection = new OleDbConnection();
        OleDbCommand command = new OleDbCommand();
        OleDbDataAdapter adapter = new OleDbDataAdapter();
        DataSet dataset = new DataSet();

        try
        {
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data source= " + myConnectionString;
            bool ok = System.IO.File.Exists(myConnectionString);
            String qry = "SELECT COUNT(*) FROM area WHERE standby = 1 AND area = 'FCH'";
            command.Connection = connection;
            command.CommandText = qry;

            adapter.SelectCommand = command;

            command.Connection.Open();
            OleDbDataReader reader = command.ExecuteReader(); // close conn after 
            SfchTable.Load(reader);
            if (!reader.IsClosed)
            {
                reader.Close();
            }

            return SfchTable;
        }
        catch (Exception)
        {
        }
        return SfchTable;
    }
}
/*******************************************************/

在我的dashboard.aspx“常见”页面后面的代码中我称之为:

/********************************FILL COUNT CLOUD******************************/
    connections.area_success_FCH Sfchconnect = new connections.area_success_FCH();
    connections.area_success_FCH SfchTable = new connections.area_success_FCH();
    /******************************************************************************/

我想在Label上使用dashboard.aspx结果填充count,但我不知道如何制作

希望有人可以帮助我

1 个答案:

答案 0 :(得分:1)

使用SqlCommand.ExecuteScalar()并将其转换为int:

cmd.CommandText = "SELECT COUNT(*) FROM area WHERE standby = 1 AND area = 'FCH'";
Int32 count = (Int32) cmd.ExecuteScalar();  

然后设置标签:

label.Text = count.ToString();

从另一个班级设置标签:

class withCode{
   cmd.CommandText = "SELECT COUNT(*) FROM area WHERE standby = 1 AND area = 'FCH'";
    public static Int32 count = (Int32)cmd.ExecuteScalar();        
}

class withLabel{
     label.Text = withCode.count.ToString();
}