无法获得总行数

时间:2016-10-28 01:35:02

标签: c# sql-server ado.net

我有一个包含3列的表,每列有5行。现在我想在c#中获得这些行的总数,以动态创建该数量的标签,并获取标签名称的行值。同样,创建同样数量的文本框。然后在运行时,我想通过这个文本框将值提交给数据库。

注意:在这里,如果我增加表的行数,那么标签和文本框将自动/动态增加,并且通过文本框提交值将完美地工作。

但是,我所做的只是获得计数值1,我只是尝试了很多,但没有得到总计数值,实际上是5。

这里是我的代码......

private void Form1_Load(object sender, EventArgs e)
{
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
        //string cmText = "select ProductId,ProductName,UnitPrice from tblProductInventory";
        string cmText = "Select Count(ProductId) from tblProductInventory";
        SqlCommand cmd = new SqlCommand(cmText, con);
        con.Open();
        using (SqlDataReader rdr = cmd.ExecuteReader())
        {
            int count = rdr.FieldCount;
            while (rdr.Read())
            {
                //System.Windows.Forms.Label MyLabel;
                {
                    int y = 50;
                    Label myLabel = new Label();
                    for (int i = 0; i < count; i++)
                    {
                        myLabel = new Label();
                        myLabel.Location = new Point(88, y);
                        myLabel.Name = "txtVWReadings" + i.ToString();
                        myLabel.Size = new Size(173, 20);
                        myLabel.TabIndex = i;
                        myLabel.Visible = true;
                        myLabel.Text = rdr[i].ToString();
                        y += 25;
                        this.Controls.Add(myLabel);
                    }
                }
            }
        }
    }
}

我得到this output

2 个答案:

答案 0 :(得分:1)

Count(columname): 将仅计算该列中的NOT NULL值。

Count(*): 将计算该表中的记录数。

所以我猜你在NULL列中有一些ProductId个值。将其更改为

Select Count(*) from tblProductInventory

答案 1 :(得分:1)

问题似乎是您使用查询作为计数,但您想要字段的值。所以你可以把它改成

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{
    //string cmText = "select ProductId,ProductName,UnitPrice from tblProductInventory";
    string cmText = "Select Count(ProductId) from tblProductInventory";
    SqlCommand cmd = new SqlCommand(cmText, con);
    con.Open();
    Int32 count = (Int32) cmd.ExecuteScalar();
    int i = 1;
    cmText = "select ProductId,ProductName,UnitPrice from tblProductInventory";
    SqlCommand cmd1 = new SqlCommand(cmText, con);
    using (SqlDataReader rdr = cmd1.ExecuteReader())
    {
        int y = 50;
        Label myLabel = new Label();
        TextBox MyTxt = New TextBox();

        while (rdr.Read())
        {
            myLabel = new Label();
            myLabel.Location = new Point(88, y);
            myLabel.Name = "txtVWReadings" + i.ToString();
            myLabel.Size = new Size(173, 20);
            myLabel.TabIndex = i;
            myLabel.Visible = true;
            myLabel.Text = rdr[1].ToString(); //Since you want ProductName here
            y += 25;
            this.Controls.Add(myLabel);

            //Same Way Just include the TextBox
            //After all Position of TextBox 
            MyTxt.Text = rdr[2].ToString(); // I believe you need UnitPrice of the ProductName
            i++;
        }
    }
}