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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题