Sql哪里声明不能使用asp.net?

时间:2017-05-03 14:48:37

标签: c# sql asp.net sql-server

为了更好地解释我的个人网站项目的问题,我将从我创建的数据库开始。该表名为guitarItems。 enter image description here

这张表是我获取在网页上显示图像和吉他细节的数据的地方。为此,我创建了一个名为“GetGuitarItems”的方法来执行和读取sql语句。

public static ArrayList GetGuitarItems(string itemCategory)
{
    ArrayList list = new ArrayList();
    string query = string.Format("SELECT * FROM guitarItems WHERE brand LIKE '{0}'", itemCategory);

    try
    {
        conn1.Open();
        command1.CommandText = query;
        SqlDataReader reader = command1.ExecuteReader();

        while (reader.Read())
        {
            int id = reader.GetInt32(0);
            string type = reader.GetString(1);
            string brand = reader.GetString(2);
            string model = reader.GetString(3);
            double price = reader.GetDouble(4);
            string itemimage1 = reader.GetString(5);
            string itemimage2 = reader.GetString(6);
            string description = reader.GetString(7);
            string necktype = reader.GetString(8);
            string body = reader.GetString(9);
            string fretboard = reader.GetString(10);
            string fret = reader.GetString(11);
            string bridge = reader.GetString(12);
            string neckpickup = reader.GetString(13);
            string bridgepickup = reader.GetString(14);
            string hardwarecolor = reader.GetString(15);

            GuitarItems gItems = new GuitarItems(id, type, brand, model, price, itemimage1, itemimage2, description, necktype, body,
                fretboard, fret, bridge, neckpickup, bridgepickup, hardwarecolor);
            list.Add(gItems);
        }
    }
    finally
    {
        conn1.Close();
    }

    return list;
}

下一部分是此代码,您可以在其中显示从数据库中检索的数据。

public partial class Pages_GuitarItems1 : System.Web.UI.Page
{
private string brandType = "Ibanez";
private int x = 0;
protected void Page_Load(object sender, EventArgs e)
{
    FillPage();
}

private void FillPage()
{
    ArrayList itemList = new ArrayList();
    ArrayList itemListPage = new ArrayList();

    if (!IsPostBack)
    {
        itemList = ConnectionClassGuitarItems.GetGuitarItems("%");
    }
    else
    {
        itemList = ConnectionClassGuitarItems.GetGuitarItems(brandType);
    }

    StringBuilder sb = new StringBuilder();

    foreach (GuitarItems gList in itemList)
    {
        itemListPage.Add("GuitarItemsIbanezDetails" + (x + 1) + ".aspx");

        sb.Append(
                string.Format(
                    @"
                    <div class='one-two'>
                        <a href='{3}' runat='server'><img runat='server' src='{0}'/></a>
                        <div class='content'>
                            <div id='label'>{1} {2}</div>
                        </div>

                </div>", gList.ItemImage1, gList.Brand, gList.Model, itemListPage[x]));

        x++;

    }

    lblOutput.Text = sb.ToString();

}
}

现在的问题是它显示数据库中的每个吉他项目。如上面的代码所示,我想要展示的只是带有品牌“Ibanez”的吉他物品。我怀疑foreach代码,但至少目前,GetGuitarItemsMethod旨在获取Ibanez吉他项目,数据将传递给ArrayList itemList变量进行显示。我还检查了sql语句,看起来是正确的。希望你们能帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

改变
if (!IsPostBack)
{
    itemList = ConnectionClassGuitarItems.GetGuitarItems("%");
}
else
{
    itemList = ConnectionClassGuitarItems.GetGuitarItems(brandType);
}

    itemList = ConnectionClassGuitarItems.GetGuitarItems(brandType);