简体   繁体   English

在Visual Studio Windows Form中连接数据库和获取数据时出现问题

[英]Problems in connecting to the database and fetching data in Visual Studio Windows Form

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). 我有一个组合框,应该输出主类别(具有4行),然后还输出其下的子类别(例如,第一个主类别下的3个子类别,第二个主类别下的5个子类别,依此类推)。

My First Code displays the main category correctly (the combobox has 4 results) 我的第一个代码正确显示了主要类别(组合框有4个结果)

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) 但是我尝试在主类别的while循环下为子类别插入另一个循环,但组合框上的所有内容都是主类别的第一行(组合框只有1个结果,即结果的第一行)。主类别表)

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: 我唯一的编程经验是在php上,所以我尝试在PHP上创建预期的代码,并且效果很好,但是在Visual Studio中将其应用于C#时遇到了麻烦,如果您想知道我在尝试什么,这就是我的代码实现:

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). 我实际上不知道为什么我的第二条代码只执行一次循环,这是怎么回事,更糟糕的是它甚至不显示子类别(sql语句中WHERE旁边的maincategory_id='got_category_id'只是一个占位符我试图用maincategory_id='1'替换它,只是为了希望它至少检查它是否显示了子类别,但实际上我不知道在sql语句中插入整数的正确语法但在Visual Studio中,将其设置为'1'仍然不会显示任何内容)。

  2. What is the proper syntax on inserting an integer inside a sql statement in Visual Studio? 在Visual Studio中的sql语句内插入整数的正确语法是什么? I researched and tried putting the & at the beginning and end of the variable but it isn't working. 我研究并尝试将&放在变量的开头和结尾,但是它不起作用。

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. 尝试使用String.Format将类别ID正确嵌入查询中。

SqlCommand get_subcategory_name = new SqlCommand(String.Format("SELECT * FROM subcategory WHERE maincategory_id='{0}'", got_category_id), sc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据库未连接到Visual Studio - Database is not connecting to visual studio 将MySQL数据库与Visual Studio连接 - Connecting MySQL database with Visual Studio 连接到Visual Studio中的数据库以获取会员资格 - connecting to a database in visual studio for membership 如何使用Windows Form数据网格更新数据库? (Visual Studio 2010) - How to update database using Windows Form data grid? (Visual Studio 2010) 连接到Visual Studio Express 2013 for Web中的数据库 - Connecting to database in Visual Studio Express 2013 for Web SQL 数据库未连接到 Visual Studio 2015 - SQL database not connecting to the visual studio 2015 将SQLite数据库连接到Visual Studio 2012中的C#Windows Phone 8应用程序? - Connecting a SQLite database to a C# Windows Phone 8 Application in Visual Studio 2012? 如何在Visual Studio 2008中的两个Windows窗体之间共享数据 - How to share data between two Windows Form in Visual Studio 2008 Visual Studio中损坏的Windows窗体 - Corrupted windows form in visual studio 如何在Visual Studio中从数据库中获取数据时使HTML行可单击(使用C#) - How to make an HTML row clickable while fetching data from the database (Using C#) in visual Studio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM