douchan0523 2015-10-15 16:24
浏览 380
已采纳

连接到数据库和在Visual Studio Windows窗体中获取数据的问题

I have a combo box that's supposed to output the main category (has 4 rows) then also output the sub category under it (ex. 3 sub categories under the first main category, 5 sub categories under the second main category and so on).

My First Code displays the main category correctly (the combobox has 4 results)

private void Form1_Load(object sender, EventArgs e)
{
sc.Open();

//For Main Category
SqlCommand get_maincategory_name = new SqlCommand("SELECT * FROM maincategory", sc);
SqlDataReader dr2 = get_maincategory_name.ExecuteReader();
    while (dr2.Read())
    {
    comboBox1.Items.Add(dr2["maincategory_name"].ToString());
    }

sc.Close();
}

But I tried inserting another loop for the sub category under the while loop of the main category but all I'm getting on the combo box is the first row of the main category (the combobox only has 1 result which is the first row from the main category table)

The code I used was:

private void Form1_Load(object sender, EventArgs e)
{
sc.Open();

//For Main Category
SqlCommand get_maincategory_name = new SqlCommand("SELECT * FROM maincategory", sc);
SqlDataReader dr2 = get_maincategory_name.ExecuteReader();
    while (dr2.Read())
    {
    comboBox1.Items.Add(dr2["maincategory_name"].ToString());

    //For Sub Category
    int got_category_id = Convert.ToInt32(dr2["maincategory_id"]);
    SqlCommand get_subcategory_name = new SqlCommand("SELECT * FROM subcategory WHERE maincategory_id='got_category_id'", sc);
    SqlDataReader dr3 = get_subcategory_name.ExecuteReader();
        while (dr3.Read())
        {
        comboBox1.Items.Add(dr3["subcategory_name"].ToString());
        }
    }

sc.Close();
}

The only programming experience I have is on php so I tried creating my intended code on PHP and it worked perfectly but I'm having troubles applying it to C# in Visual Studio, here's my code if you guys are wondering what I'm trying to achieve:

echo "<select>";

// FOR MAIN CATEGORY
$maincategory_query = mysqli_query($con,"SELECT * FROM maincategory") or mysqli_error();
while($got = mysqli_fetch_assoc($maincategory_query))
{
$maincategory_id = $got['maincategory_id'];
$maincategory_name = $got['maincategory_name'];
echo "<option disabled>$maincategory_name</option>";

    // FOR SUB CATEGORY
    $subcategory_query = mysqli_query($con,"SELECT * FROM subcategory WHERE maincategory_id='$maincategory_id'") or mysqli_error();
    while($got = mysqli_fetch_assoc($subcategory_query))
    {
    $subcategory_id = $got['subcategory_id'];
    $subcategory_name = $got['subcategory_name'];
    echo "<option disabled>--$subcategory_name</option>";

        // FOR ITEM
        $item_query = mysqli_query($con,"SELECT * FROM item WHERE subcategory_id='$subcategory_id'") or mysqli_error();
        while($got = mysqli_fetch_assoc($item_query))
        {
        $item_id = $got['item_id'];
        $item_name = $got['item_name'];
        echo "<option>----$item_name</option>"; 
        }
    }
}

echo "</select>";

My Concerns:

  1. I don't actually know what's happening on why my second code is only doing the loop once and what's worse is it doesn't even display the sub category (the maincategory_id='got_category_id' beside the WHERE in my sql statement was only a placeholder and I tried replacing it by maincategory_id='1' just to check in hopes of it trying to at least check if it's displaying the sub category to no avail. I actually don't know the proper syntax to insert an integer inside a sql statement in Visual Studio but nevertheless, setting it to a value of '1' still doesn't display anything).

  2. What is the proper syntax on inserting an integer inside a sql statement in Visual Studio? I researched and tried putting the & at the beginning and end of the variable but it isn't working.

  • 写回答

1条回答

  • douying4203 2015-10-15 16:31
    关注

    It has to do with the way you are building your second query. Try using String.Format to proper embed you category ID into the query.

    SqlCommand get_subcategory_name = new SqlCommand(String.Format("SELECT * FROM subcategory WHERE maincategory_id='{0}'", got_category_id), sc);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题