无法获取总行数

Can't get the total row count

我有一个 table,它有 3 列,每列有 5 rows.Now 我想在 C# 中获取这些总行数,以动态创建该数量的标签并获取行标签 name.Similarly 的值,在运行时创建与 well.Then 相同数量的文本框,我想通过此文本框将值提交给数据库。

注意:在这里,如果我增加 table 的行数,那么标签和文本框将增加 automatically/dynamically 以及通过文本框提交值将完美工作。

但是,我所做的只是得到计数值 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。

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

Count(*) : 将计算 table.

中的记录数

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

Select Count(*) from tblProductInventory

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

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++;
        }
    }
}