简体   繁体   English

加载了表单,但是如何将数据从SQL提取到组合框?

[英]A form is loaded but how to extract data from SQL to combobox?

Still learning C# 还在学习C#

A comboBox is created and Tables called mainCat and subCat is created. 创建一个comboBox并创建名为mainCat和subCat的表。

I have a code , but i am stuck to understand on how to get the data from mainCat to the comboBox , which is then used by another comboBox for the subCat to set a subcategory. 我有一个代码,但是我仍然想了解如何将数据从mainCat获取到comboBox,然后将其由另一个comboBox用于subCat设置子类别。

The Get Connection is underlined red. 获取连接用红色下划线标记。 Why? 为什么?

Here is my code - 这是我的代码-

System.Data.SqlServerCe.SqlCeConnection con;
System.Data.SqlServerCe.SqlCeDataAdapter da;
DataSet ds1;

private void Form2_Load(object sender, EventArgs e)
{
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
    conn.Open();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    using (SqlConnection Con = GetConnection())
    {
        SqlDataAdapter da = new SqlDataAdapter("Select Category.Category ,Category.Id from Category", Con);
        SqlCommand cmd = new SqlCommand("SELECT * from MAINCAT");
        DataTable dt = new DataTable();
        da.Fill(dt);
        mainCatU.DataSource = dt;
        mainCatU.DisplayMember = "Category";
        mainCatU.ValueMember = "Id";
        mainCatU.Text = "<-Please select Category->";
        myComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
    }

}

So i then i tried another code.. but still doesnt work.. 所以我然后我尝试了另一个代码。

public partial class User : Form
{
        System.Data.SqlServerCe.SqlCeConnection con;
        System.Data.SqlServerCe.SqlCeDataAdapter da;
        DataSet ds1;

        private void User_Load(object sender, EventArgs e)
        {
            con = new System.Data.SqlServerCe.SqlCeConnection();
            con.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Integrated Security=True";
            con.Open();
            MessageBox.Show("Database connected");
            ds1 = new DataSet();
            string sql = "SELECT * from MAINCAT";
            da = new System.Data.SqlServerCe.SqlCeDataAdapter(sql, con);
            da.Fill(ds1, "SCSID");
            mainCatU.DataSource = ds1;
            con.Close();
            mainCatU.Text = "<-Please select Category->";
            mainCatU.DropDownStyle = ComboBoxStyle.DropDownList;
            mainCatU.Enabled = true;
        }
}

then i just used the data bound item function through the combobox GUI.. 然后我只是通过组合框GUI使用了数据绑定项功能。

this.mAINCATTableAdapter.Fill(this.masterDataSet.MAINCAT);

but , the box didn't show any value , except "System.Data.DataRowView" in the comboBox 但是,该框未显示任何值,除了comboBox中的“ System.Data.DataRowView”

================================================================================== ================================================== ================================

System.Data.SqlServerCe.SqlCeConnection con; //not used at the moment
System.Data.SqlServerCe.SqlCeDataAdapter da; //not used at the moment
DataSet ds1;

private void User_Load(object sender, EventArgs e)
{

    SqlConnection conn = new SqlConnection();
    conn.ConnectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True";
    conn.Open();
    MessageBox.Show("Database connected");

    SqlDataAdapter da = new SqlDataAdapter("SELECT * from MAINCAT", conn);
    ds1 = new DataSet();
    da.Fill(ds1, "MainCat");
    mainCatU.DisplayMember = "maincat";
    mainCatU.ValueMember = "maincat";
    mainCatU.DataSource = ds1.Tables["MAINCAT"];

}

=============== ===============

and the combo box is still not showing anything from the database table 并且组合框仍未显示数据库表中的任何内容

You need to create the function GetConnection() : 您需要创建函数GetConnection()

public string ConnectionString { get; set;}

public SqlConnection GetConnection()
{
    SqlConnection cn = new SqlConnection(ConnectionString);
    return cn;
}

TBH, unless you want to do something in GetConnection , you might be better just creating it inline: TBH,除非您想在GetConnection做某事,否则最好内联创建它:

using (SqlConnection Con = new SqlConnection(ConnectionString))
{

EDIT: 编辑:

Based on your revised question, I think the problem now may be here: 根据您修改的问题,我认为现在的问题可能在这里:

mainCatU.DisplayMember = "maincat";
mainCatU.ValueMember = "maincat";
mainCatU.DataSource = ds1.Tables["MAINCAT"];

My guess is that your table structure is not maincat.maincat. 我的猜测是您的表结构不是maincat.maincat。 The display and value members should be set to the field name that you wish to display. 显示成员和值成员应设置为要显示的字段名称。

I am really sorry I am a vb developer but the concept is same to populate data into combo using sql is 我真的很抱歉我是vb开发人员,但是使用sql将数据填充到组合中的概念是相同的

Declare SQLConnection Declare SQLDataReader Declare SQLCommand 声明SQLConnection声明SQLDataReader声明SQLCommand

Try
    If Con.State = ConnectionState.Closed Then
        Con.Open()

    cmd.Connection = Con
    cmd.CommandText = "Select field1, field2 from table"


    dr = cmd.ExecuteReader()

    ' Fill a combo box with the datareader
    Do While dr.Read = True
        ComboBoxName.Items.Add(dr.GetString(0))
        ComboBoxName.Items.Add(dr.GetString(1))
    Loop

    Con.Close()
    End If

Catch ex As Exception
    MsgBox(ex.Message)

End Try

Hope it works for you. 希望对你有效。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM