本文介绍了C#从AccessDatabase 2010获取某些数据并显示在组合框C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我曾尝试在MSDN论坛上发布此问题,但是,我没有成功。 我正在尝试为一家企业制作一个C#Windows Forms应用程序将管理其员工。该应用程序使用Access 2010数据库。编辑员工页面是我遇到的麻烦。我希望员工能够从组合框中选择,格式为: FirstName LastName,EmployeePosition 数据库名称是EmployeeInformation.accdb 我想从数据库中获取的字段是: - FirstName - LastName - EmployeePosition 组合框的名称是cboSelectEmp 我用来尝试获取这些数据的当前代码是 private void LoadDataToCbo() { string connString = @ Provider = Microsoft.ACE.OLEDB.12.0; Data Source = EmployeeInformation.accdb; // string query = @SELECT FirstName from Employees; string query = @ SELECT EmpID,LastName +','+ FirstName +'('+ EmployeePosition +')'作为名称来自员工; OleDbDataAdapter dAdapter = new OleDbDataAdapter(query,connString); DataTable source = new DataTable(); dAdapter.Fill(来源); cboSelectEmp.DataSource = source; // cboSelectEmp.ValueMember =FirstName; // cboSelectEmp.DisplayMember =FirstName; cboSelectEmp.DisplayMember = 名称; cboSelectEmp.ValueMember = EmpID; } 解决方案 我使用此代码修复它: private void LoadDataToCbo() { string connString = @ Provider = Microsoft.ACE.OLEDB.12.0 ; Data Source = EmployeeInformation.accdb; string query = @ SELECT(FirstName +''+ LastName +','+ EmployeePosition)作为员工的FirstName; OleDbDataAdapter dAdapter = new OleDbDataAdapter(query,connString); DataTable source = new DataTable(); dAdapter.Fill(来源); cboSelectEmp.DataSource = source; cboSelectEmp.DisplayMember = FirstName; } 看起来它是ValueMember属性是那个搞砸我的东西。 谢谢大家的帮助 试试这个: string query = @ SELECT EmpID,FirstName as [Name],LastName,EmployeePosition FROM Employees; 在备注下的文档中检查此行OleDbDataAdapter构造函数的此重载使用selectConnectionString参数来设置SelectCommand属性。但是,它不会打开连接。您仍然必须明确打开连接。 http://msdn.microsoft.com/en-us/library/2f8y4737(v=vs.110).aspx 我认为这是你的问题 试试这个 string query = @ SELECT EmpID,FirstName as [Name],LastName,EmployeePosition FROM Employees; 使用( var odb = new OleDbConnection(connectionString)) { odb.Open(); if (odb.State == ConnectionState.Open) { var dset = new DataTable(); var adapter = new OleDbDataAdapter(); var newCmd = new OleDbCommand(query,odb) { CommandType = CommandType.Text }; adapter.SelectCommand = newCmd; adapter.Fill(dset); odb.Close(); } } I have tried posting this question on MSDN forums, however, i had no success.I am trying to make a C# Windows Forms Application for a business that will manage its employees. The application uses an Access 2010 Database. The edit Employee page is what I am having trouble with. I want the Employees to be able to be selected from a combobox in the format of:FirstName LastName, EmployeePositionThe database name is EmployeeInformation.accdbThe fields that I want from the Database are: - FirstName - LastName - EmployeePositionThe name of the Combo Box is cboSelectEmpThe current code i am using to try and get this data is private void LoadDataToCbo() { string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=EmployeeInformation.accdb"; // string query = @"SELECT FirstName from Employees"; string query = @"SELECT EmpID, LastName + ', ' + FirstName + ' (' + EmployeePosition + ')' as Name FROM Employees"; OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString); DataTable source = new DataTable(); dAdapter.Fill(source); cboSelectEmp.DataSource = source; //cboSelectEmp.ValueMember = "FirstName"; //cboSelectEmp.DisplayMember = "FirstName"; cboSelectEmp.DisplayMember = "Name"; cboSelectEmp.ValueMember = "EmpID"; } 解决方案 I fixed it using this code:private void LoadDataToCbo() { string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=EmployeeInformation.accdb"; string query = @"SELECT (FirstName + ' ' + LastName + ', ' + EmployeePosition) as FirstName from Employees"; OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString); DataTable source = new DataTable(); dAdapter.Fill(source); cboSelectEmp.DataSource = source; cboSelectEmp.DisplayMember = "FirstName"; }it looks like it was the ValueMember property was the thing that was messing with me.Thanks everyone for all your helpTry This:string query = @"SELECT EmpID, FirstName as [Name], LastName , EmployeePosition FROM Employees";Check this line in documentation under remarks This overload of the OleDbDataAdapter constructor uses the selectConnectionString parameter to set the SelectCommand property. However, it does not open the connection. You still must explicitly open the connection.http://msdn.microsoft.com/en-us/library/2f8y4737(v=vs.110).aspxI think that is your problemTry thisstring query = @"SELECT EmpID, FirstName as [Name], LastName , EmployeePosition FROM Employees";using (var odb = new OleDbConnection(connectionString)) { odb.Open(); if (odb.State == ConnectionState.Open) { var dset = new DataTable(); var adapter = new OleDbDataAdapter(); var newCmd = new OleDbCommand(query, odb) { CommandType = CommandType.Text }; adapter.SelectCommand = newCmd; adapter.Fill(dset); odb.Close(); }} 这篇关于C#从AccessDatabase 2010获取某些数据并显示在组合框C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-28 14:10