数据库中的组合框 [英] combobox from database

查看:91
本文介绍了数据库中的组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我花了很多时间思考并尝试实现以下目标,但到目前为止还是失败了.

I have spent a lot of time thinking and trying to achieve the following but failed so far.

我想从数据库中填充一个组合框.问题是..我需要创建一个方法来这样做,因为数据库逻辑(查询,方法等)在单独的类中.因此,我想在另一个类/窗体中使用此方法来填充它.

I'd like to populate a combobox from database. The thing is.. I need to create a method in order to do that because the database logic (queries, methods etc) is in separate classs. So I'd like to use this method in another class/Form to populate it.

查询是-从其中User_Id ='''的布局中选择*,但是我不确定如何检索user_id,因为它是用另一种形式与用户对象一起添加的.

the query is - select * from Layout where User_Id ='", however I am not sure how to retrieve the user_id since it is added with the user object in another form.

我希望我的解释有意义.任何有关如何实现我的目标的建议将不胜感激.谢谢你!

I hope my explanation makes some sense. Any advise on how to achieve my goal would be highly appreciated. Thanks a mil!

推荐答案

你好

您没有提及数据库的类型,因此下面的示例适用于SQL Server.

You didn't mention what type of database so the following example is for SQL-Server.

在这种情况下,此类是一个类项目,但可以是表单项目,我想将后端与前端分开.

This class would be in this case a class project but it could be in the form project, I like to separate backend from front end.

在表单项目中向类项目添加引用.

Add a reference in the form project to the class project.

班级

using System.Data;
using System.Data.SqlClient;

namespace DataOperations
{
    public class CodeSample
    {
        string Server = "Your server name";
        string Catalog = "Your database name";
        string ConnectionString = "";
        public CodeSample()
        {
            ConnectionString =


"数据源= {Server};初始目录= {Catalog};集成安全性= True"; } 公用DataTable GetData(int UserIdentifier) { var dt = new DataTable(); 使用(SqlConnection cn =新的SqlConnection {ConnectionString = ConnectionString}) { 使用(SqlCommand cmd =新的SqlCommand {Connection = cn}) { cmd.CommandText =从布局中选择*,其中User_Id = @UserIdentifier"; cmd.Parameters.AddWithValue("@ UserIdentifier",UserIdentifier); cn.Open(); dt.Load(cmd.ExecuteReader()); } } 返回dt } } }
"Data Source={Server};Initial Catalog={Catalog};Integrated Security=True"; } public DataTable GetData(int UserIdentifier) { var dt = new DataTable(); using (SqlConnection cn = new SqlConnection { ConnectionString = ConnectionString }) { using (SqlCommand cmd = new SqlCommand { Connection = cn }) { cmd.CommandText = "select * from Layout where User_Id = @UserIdentifier"; cmd.Parameters.AddWithValue("@UserIdentifier", UserIdentifier); cn.Open(); dt.Load(cmd.ExecuteReader()); } } return dt; } } }

使用上述代码获取数据的表单代码

Form code to get data using the above

var id = 3;
var sampleOps = new CodeSample();
var dt = sampleOps.GetData(id);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "TO DO";

如果使用MS-Access或其他数据提供程序,请替换SqlClient的调用,例如SqlConnection和SqlCommand以及提供程序的类,例如MS-Access OleDbConnection,OleDbCommand等.

If using MS-Access or another data provider replace calls for SqlClient e.g. SqlConnection and SqlCommand with the provider's classes e.g. MS-Access OleDbConnection, OleDbCommand etc.

当您需要获取有关当前选定的ComboBox项的信息时,可以使用类似这样的代码

When you need to get information for the currently selected ComboBox item you can use code like this

var item = ((DataRowView)comboBox1.SelectedItem).Row.Field<string>("Some field name");


这篇关于数据库中的组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆