使用 DataReader 使用数据库多列中的数据填充文本框

Fill textboxes with data from database multiple columns with datareader

我是数据库方面的新手,但想用数据读取器和 sql 查询用数据填充文本框。 我的程序从我数据库中的每艘船创建 4 个文本框。每个文本框都需要填充来自不同列的数据。填满4个文本框后,将添加一个带有4个文本框的新船,并且必须将每列的第二行添加到文本框。

到目前为止,这是我的代码:

public void Schip()
{    
    using (SqlConnection thisConnection = new SqlConnection("Data Source=VMB-LP12;Initial Catalog=SmmsData;Integrated Security=True"))
    {
        string oString = "Select SchipNaam, RederijNr, Lengte, Laadvermogen FROM AlgSchipInfo";
        SqlCommand oCmd = new SqlCommand(oString, thisConnection);

        thisConnection.Open();
        using (SqlDataReader oReader = oCmd.ExecuteReader())
        {
            int i = 0;
            while (oReader.Read())
            // for (int i = 0; i < count; i++)
            {

                System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
                new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                createDiv.ID = "createDiv";
                this.Controls.Add(createDiv);
                List<TextBox> tb_names = new List<TextBox>();
                TextBox tb_name = new TextBox();

                //TextBox tb_name = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_1";
                tb_name.Text = (oReader["SchipNaam"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Scheepspnaam: <input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);

                //TextBox tb_name1 = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_2";
                tb_name.Text = (oReader["RederijNr"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> RederijNr:<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);

                //TextBox tb_name2 = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_3";
                tb_name.Text = (oReader["Lengte"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Lengte  :<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);

                //TextBox tb_name3 = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_4";
                tb_name.Text = (oReader["Laadvermogen"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Laadvermogen:<input type='text' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);
                i++;
            }
        }
    }
}

我使用查询查找四列,并使用数据读取器读取数据。 使用 tb_name.text = (oReader["SchipMaam"].ToString()) 将填充文本框。我错过了这个拼图中的一块吗?

问题是当我使用它时,我的文本框一直是空的。 到目前为止,我所知道的 DIV 或 ID 没有问题。 我如何用我的数据库 table?

列中的数据填充我的文本框

如果你能给我指出正确的方向就太好了。

使用此代码,您从未将值分配给正在生成的 <input type='text' 标签:

public void Schip()
{    
    using (SqlConnection thisConnection = new SqlConnection("Data Source=VMB-LP12;Initial Catalog=SmmsData;Integrated Security=True"))
    {
        string oString = "Select SchipNaam, RederijNr, Lengte, Laadvermogen FROM AlgSchipInfo";
        SqlCommand oCmd = new SqlCommand(oString, thisConnection);

        thisConnection.Open();
        using (SqlDataReader oReader = oCmd.ExecuteReader())
        {
            int i = 0;
            while (oReader.Read())
            // for (int i = 0; i < count; i++)
            {

                System.Web.UI.HtmlControls.HtmlGenericControl createDiv =
                new System.Web.UI.HtmlControls.HtmlGenericControl("DIV");
                createDiv.ID = "createDiv";
                this.Controls.Add(createDiv);
                List<TextBox> tb_names = new List<TextBox>();
                TextBox tb_name = new TextBox();

                //TextBox tb_name = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_1";
                tb_name.Text = (oReader["SchipNaam"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Scheepspnaam: <input type='text' value='" + tb_name.Text + "' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);

                //TextBox tb_name1 = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_2";
                tb_name.Text = (oReader["RederijNr"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> RederijNr:<input type='text' value='" + tb_name.Text + "' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);

                //TextBox tb_name2 = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_3";
                tb_name.Text = (oReader["Lengte"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Lengte  :<input type='text' value='" + tb_name.Text + "' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);

                //TextBox tb_name3 = new TextBox();
                tb_name.ID = "CreateT_" + i.ToString() + "_4";
                tb_name.Text = (oReader["Laadvermogen"].ToString());
                createDiv.Controls.Add(new LiteralControl
                ("<div class='form-group'><div class='clearfix' ></div><div class='row'><div class='col-md-3'></div><div class='col-md-3'> Laadvermogen:<input type='text' value='" + tb_name.Text + "' id='" + tb_name.ID + "' runat='server'/></div></div></div>"));
                tb_names.Add(tb_name);
                i++;
            }
        }
    }
}