Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将数据读取到数组中,然后填充到C中的组合框中#_C#_Database_Arrays_Combobox_Selecteditem - Fatal编程技术网

C# 将数据读取到数组中,然后填充到C中的组合框中#

C# 将数据读取到数组中,然后填充到C中的组合框中#,c#,database,arrays,combobox,selecteditem,C#,Database,Arrays,Combobox,Selecteditem,我在数据库中有用户数据(名称、用户名、图像),我需要在组合框中列出名称和id。当选择组合框中的项目时,它应该显示一张图片。我已经创建了一个User和一个UserDB类。在我的用户类中,我有以下代码: public class User { private String u_firstname; private String u_lastname; private String u_username = string.Empty; private Byte []

我在数据库中有用户数据(名称、用户名、图像),我需要在组合框中列出名称和id。当选择组合框中的项目时,它应该显示一张图片。我已经创建了一个User和一个UserDB类。在我的用户类中,我有以下代码:

public class User
  {
    private String u_firstname;
    private String u_lastname;
    private String u_username = string.Empty;
    private Byte [] u_image;
    private Byte [] u_template;


    public User(OleDbDataReader dr)
    {            
        u_firstname = dr["USER_FIRST_NM"].ToString();
        u_lastname = dr["USER_LAST_NM"].ToString();
        u_username = dr["USER_NAME"].ToString();
        u_image = (Byte [])dr["USER_IMAGE"];
        u_template = (Byte [])dr["USER_TEMPLATE"];

    }
}
public class UserDB
 {
    public static String workingDirectory = System.IO.Directory.GetCurrentDirectory();
    public static String dbProvider = "Microsoft.ACE.OLEDB.12.0";
    public static String dbName = "Users_DB.accdb";        
    public static String dbConnString = String.Format(@"Provider={0};Data Source={1}\Data\{2}", dbProvider, workingDirectory, dbName);


    public User[] usersInDatabaseList()
    {
        User u;
        List<User> clist = new List<User>();

        OleDbConnection cn = new OleDbConnection(dbConnString);

        String strSQL = "SELECT USER_FIRST_NM, USER_LAST_NM, USER_NAME, USER_IMAGE, USER_TEMPLATE FROM KF001_USERS";

        OleDbCommand cm = new OleDbCommand(strSQL, cn);
        OleDbDataReader dr;

        cn.Open();

        dr = cm.ExecuteReader(CommandBehavior.CloseConnection);

        while (dr.Read())
        {                
            u = new User(dr);
            clist.Add(u);
        }

        cn.Close();


        return clist.ToArray();
    }
}
在我的UserDB类中,我有以下代码:

public class User
  {
    private String u_firstname;
    private String u_lastname;
    private String u_username = string.Empty;
    private Byte [] u_image;
    private Byte [] u_template;


    public User(OleDbDataReader dr)
    {            
        u_firstname = dr["USER_FIRST_NM"].ToString();
        u_lastname = dr["USER_LAST_NM"].ToString();
        u_username = dr["USER_NAME"].ToString();
        u_image = (Byte [])dr["USER_IMAGE"];
        u_template = (Byte [])dr["USER_TEMPLATE"];

    }
}
public class UserDB
 {
    public static String workingDirectory = System.IO.Directory.GetCurrentDirectory();
    public static String dbProvider = "Microsoft.ACE.OLEDB.12.0";
    public static String dbName = "Users_DB.accdb";        
    public static String dbConnString = String.Format(@"Provider={0};Data Source={1}\Data\{2}", dbProvider, workingDirectory, dbName);


    public User[] usersInDatabaseList()
    {
        User u;
        List<User> clist = new List<User>();

        OleDbConnection cn = new OleDbConnection(dbConnString);

        String strSQL = "SELECT USER_FIRST_NM, USER_LAST_NM, USER_NAME, USER_IMAGE, USER_TEMPLATE FROM KF001_USERS";

        OleDbCommand cm = new OleDbCommand(strSQL, cn);
        OleDbDataReader dr;

        cn.Open();

        dr = cm.ExecuteReader(CommandBehavior.CloseConnection);

        while (dr.Read())
        {                
            u = new User(dr);
            clist.Add(u);
        }

        cn.Close();


        return clist.ToArray();
    }
}
我完全不知道如何在组合框中显示我的数据库值。我也不确定在选择项目时如何显示图片。有谁能帮我或把我引向正确的方向吗

因此,我将filldropdownlist方法更改为:

 public void FillDropDownList()
    {

            UserDB db = new UserDB();
            User[] userList = db.cList();



            for (int i = 0; i < userList.Length; i++)
            {

                comboBoxUsersEnrolled.Items.Add(userList[i]);
                comboBoxUsersEnrolled.DisplayMember = "ComboName";




                if (comboBoxUsersEnrolled.SelectedIndex > 0 )
                {

                    byte[] pictureByteReader = (userList[i].UserImage);
                    MemoryStream ms = new MemoryStream(pictureByteReader);
                    Image picture = Image.FromStream(ms);
                    picBoxCapture.Image = picture;
                }

            }


    }
public void FillDropDownList()
{
UserDB=newuserdb();
User[]userList=db.cList();
for(int i=0;i0)
{
字节[]pictureByteReader=(用户列表[i].UserImage);
MemoryStream ms=新的MemoryStream(pictureByteReader);
Image picture=Image.FromStream(毫秒);
picBoxCapture.Image=图片;
}
}
}

我正在尝试添加到我的组合框,同时如果选中该项目,则将图片发送到picturebox。我的代码不起任何作用。没有错误或任何东西。救命啊

你已经完成了大部分工作

标记的
DisplayMember
属性指示应用于在组合框中显示的属性。您的类没有任何属性-它只有字段-因此首先您需要创建一个属性

public class User
{
    // all your existing fields

    public string Name
    {
        get
        {
            return this.u_username;
        }
    }
然后您可以设置
displaymber=“Name”
将名称用作显示属性


使用图像需要更多的工作-您需要在组合框中使用模板而不是字符串。尝试通过谷歌搜索找到一个例子。

为什么要使用
数组而不是
列表
?非常感谢Kirk。“获取/设置”用于显示值!现在我只需要弄清楚图像的东西。