我正在尝试从Ingredients表中检索Ingredient_ID,Ingredient_Name和Ingredient Amount。我试图在多行文本框的列表中并排列出它们。所以它看起来像下面的
orange | 1
apple | 5
kiwi | 2
在数据库中,我有20个独特的成分,其中14个链接到食谱ID。发生的事情是它检索第一行的14行。
例如:
steak | 1
steak | 1
steak | 1
等...
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] != null)
{
if (Session["Basket"] != null)
{
ShoppingBasket.Visible = true;
redPnl.Visible = false;
RecipeID.Text += Session["Basket"].ToString();
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("Select * From Ingredient Where Recipe_ID=@RecipeID", con);
sda.SelectCommand.Parameters.Add("@RecipeID", SqlDbType.VarChar).Value = RecipeID.Text;
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
foreach (var row in dt.Rows)
{
IngredientsList.Text += dt.Rows[0].ItemArray[0].ToString() + " | " + dt.Rows[0].ItemArray[1].ToString() + " | " + dt.Rows[0].ItemArray[2].ToString() + Environment.NewLine;
}
}
}
答案 0 :(得分:2)
试试这个:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["New"] != null)
{
if (Session["Basket"] != null)
{
ShoppingBasket.Visible = true;
redPnl.Visible = false;
RecipeID.Text += Session["Basket"].ToString();
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0; AttachDbFilename=C:\Users\Donald\Documents\Visual Studio 2013\Projects\DesktopApplication\DesktopApplication\Student_CB.mdf ;Integrated Security=True");
con.Open();
SqlDataAdapter sda = new SqlDataAdapter("Select Ingredient_Name, Ingredient_Amount From Ingredient Where Recipe_ID=@RecipeID", con);
sda.SelectCommand.Parameters.Add("@RecipeID", SqlDbType.VarChar).Value = RecipeID.Text;
DataTable dt = new DataTable();
sda.Fill(dt);
IngredientsList.Text = string.Empty;
for (int i = 0; i<dt.Rows.Count ;i++)
{
IngredientsList.Text += dt.Rows[i].ItemArray[0].ToString();
IngredientsList[i].Text += " | " + dt.Rows[i].ItemArray[1].ToString() + Enviornment.NewLine;
//dt.Clear(); :EDITED
}
dt.Clear();
}
}
}
答案 1 :(得分:1)
您只看到一个值,因为您只是设置一个值,就在这里:
if (dt.Rows.Count > 0)
{
IngredientsList.Text = dt.Rows[0].ItemArray[0].ToString();
IngredientsList.Text = dt.Rows[0].ItemArray[1].ToString();
dt.Clear();
}
如果您想要查询结果中的所有行,则必须循环遍历它们并将追加它们添加到控件中。也许是这样的:
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
IngredientsList.Text += row.ItemArray[0].ToString() + " | " + row.ItemArray[1].ToString() + Environment.NewLine;
}
}
格式化将是一个问题。这更有可能产生这样的东西:
orange | 1
apple | 5
kiwi | 2
所以你可能需要抛出一些字符串填充。如果您知道应该的宽度,那么您可以明确地填充它。基本上通过替换它:
row.ItemArray[0].ToString()
用这个:
row.ItemArray[0].ToString().PadRight(6)
只需使用您需要的任何总宽度值。另一方面,如果您需要计算总宽度,那么您需要首先检查表记录以确定最宽的值并使用它。
有很多方法可以改善这一点。例如,使用StringBuilder
或string.Format()
代替直接连接值。或者也许利用其他控件来显示数据,稍微修改用户体验。
答案 2 :(得分:-1)
if (dt.Rows.Count > 0)
{
foreach(var row in dt.Rows){
IngredientsList.Text.Append(String.Format("{0}|{1}",row.ItemArray[0], row.ItemArray[1]));
}
dt.Clear();
}
类似的东西应该这样做