将行值检索到文本框C#& SQL Server

时间:2016-11-28 03:14:36

标签: c# sql-server

您对我如何将其添加到行中有任何想法:

enter image description here

在这个texbox中?

enter image description here

或更早10行到10个文本框?

我只知道要检索一行:

if (Inventory.passQty == "1")
{
    SqlConnection connection = new SqlConnection("Data Source = DESKTOP-ANJELLO\\SQLEXPRESS; Initial Catalog = db_ADAPurchase; Persist Security Info = True; User Id = sa; Password = mm4;");
    connection.Open();

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = connection;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = String.Format("SELECT * FROM tbl_PurchaseRequest WHERE request_id = {0}", Inventory.passID);

    SqlDataReader dr = cmd.ExecuteReader();

    if (dr.Read())
    {
        label_RID.Text = dr.GetString(0);
        label_Item1.Text = dr.GetString(1);
        txb_Title.Text = dr.GetString(2);
        cbx_vendor.Text = dr.GetString(3);
        txb_address.Text = dr.GetString(4);
        label_date.Text = dr.GetString(5);
        cbx_terms.Text = dr.GetString(6);
        txb_ITD1.Text = dr.GetString(7);
        txb_Qty1.Text = dr.GetSqlInt32(8).ToString();
        label_unit1.Text = dr.GetString(9);
        txb_UntP1.Text = dr.GetSqlInt32(10).ToString();
        txb_TotP1.Text = dr.GetSqlInt32(11).ToString();
        label_total.Text = dr.GetSqlInt32(12).ToString();
        txb_reqBy.Text = dr.GetString(13);
    }

    connection.Close();
}

如何使其在不同的文本框中检索多行?

感谢您的帮助!

PS:先生@ mohit-shrivastava你可以再帮我一次吗?

2 个答案:

答案 0 :(得分:0)

你可以做这样的事情,但GridView是一个非常好的想法来实现这种情况。

if (Inventory.passQty == "1")
{
    SqlConnection connection = new SqlConnection("Data Source = DESKTOP-ANJELLO\\SQLEXPRESS; Initial Catalog = db_ADAPurchase; Persist Security Info = True; User Id = sa; Password = mm4;");
    connection.Open();
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = connection;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = String.Format("SELECT * FROM tbl_PurchaseRequest WHERE request_id = {0}", Inventory.passID);

    SqlDataReader dr = cmd.ExecuteReader();

    if (dr.Read())
    {
        label_RID.Text = dr.GetString(0);
        label_Item1.Text = dr.GetString(1);
        txb_Title.Text = dr.GetString(2);
        cbx_vendor.Text = dr.GetString(3);
        txb_address.Text = dr.GetString(4);
        label_date.Text = dr.GetString(5);
        cbx_terms.Text = dr.GetString(6);
        label_total.Text = dr.GetSqlInt32(12).ToString();
        txb_reqBy.Text = dr.GetString(13);
        int number = 1;
        do
        {
            TextBox tb = this.Controls.Find("txb_ITD" + number.ToString(), true).FirstOrDefault() as TextBox;
            tb.Text = dr.GetString(7);
            TextBox tb1 = this.Controls.Find("txb_Qty" + number.ToString(), true).FirstOrDefault() as TextBox;
            tb1.Text = dr.GetSqlInt32(8).ToString();
            TextBox tb2 = this.Controls.Find("label_unit" + number.ToString(), true).FirstOrDefault() as TextBox;
            tb2.Text = dr.GetString(9);
            TextBox tb3 = this.Controls.Find("txb_UntP" + number.ToString(), true).FirstOrDefault() as TextBox;
            tb3.Text = dr.GetSqlInt32(10).ToString();
            TextBox tb4 = this.Controls.Find("txb_TotP" + number.ToString(), true).FirstOrDefault() as TextBox;
            tb4.Text = dr.GetSqlInt32(11).ToString();
            number++;
            //Since Query can pull more records than 10
            if(number>=10)
            {
                break;
            }
        }
        while(dr.Read())
    }
    connection.Close();
}

此代码将以您阅读它的方式读取SQL数据,然后填充应该打印一次的所有文本框。 Liek RequestIDItemTitleVendor等等。然后进入循环。现在dr已经有了第一个记录,所以我们不想再次读取dr,否则它会占用记录集中的下一个记录,所以我们使用do-while而不是while。现在我们正在尝试从Control.ControlCollection.Find中找到控件,并将提取的值放到相关的文本框中。

注意:该代码尚未经过测试。但它是为了让您了解必须采取哪些措施来获得预期的输出。

答案 1 :(得分:0)

我认为通过创建多个服务器端文本框来创建多行。你需要jquery。这更容易更快更可靠:

您需要的步骤:

1.创建一个从数据库中提取所有行的Web服务 2.通过从webservice中提取数据来绑定表行 3.创建一个Web服务,将所有数据插入数据库。