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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 寻找一位有逆向游戏盾sdk 应用程序经验的技术
  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA