如何从MS Access文本字段填充组合框,然后将组合框选择插入另一个表(How to fill a combobox from a MS Access text field then insert

编程入门 行业动态 更新时间:2024-10-28 01:20:19
如何从MS Access文本字段填充组合框,然后将组合框选择插入另一个表(How to fill a combobox from a MS Access text field then insert combobox selection into another table)

我正在尝试使用组合框中的信息插入Access表记录。 我正在使用winform和C#。 我简化了我的项目,只包括问题区域。 我展示了三个带有4个控件的方法(2个按钮和2个组合框。第一种方法是连接到Access数据库,然后显示第一个组合框中的表和视图列表。此方法也将调用最后一个方法,SelectName( )并从所选数据库的预定表中填充第二个组合框中的字段内容。第二种方法(buttonInsert_Click())是我的问题所在。我希望方法从combobox1插入所选表中的一个所选项单击插入按钮时从combobox2开始。我可以插入到所选表中的唯一内容是

System.Data.DataRowView

我项目的C#部分如下。 任何建议表示赞赏。 谢谢。

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace DbApp { public partial class Form1 : Form { private char ch = '"'; private OleDbConnection dbConn; private string sql = ""; public Form1() { InitializeComponent(); } private void buttonConnect_Click(object sender, EventArgs e) { string connectionString = ""; string stringData = ""; openFileDialog1.Filter = ""; openFileDialog1.ShowDialog(); Text = openFileDialog1.FileName; stringData = openFileDialog1.FileName; connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ch + Text + ch; if (dbConn != null) dbConn.Close(); dbConn = new OleDbConnection(connectionString); dbConn.Open(); comboBox1.Items.Clear(); DataTable info = dbConn.GetSchema("Tables"); for (int x = 0; x < info.Rows.Count; ++x) { if ((info.Rows[x][3].ToString() == "TABLE") || (info.Rows[x][3].ToString() == "VIEW")) { comboBox1.Items.Add((object)info.Rows[x][2].ToString()); } } SelectName(); } private void buttonInsert_Click(object sender, EventArgs e) { string name = this.comboBox2.SelectedItem.ToString(); try { dbConn = new OleDbConnection(); dbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + ch + openFileDialog1.FileName + ch; dbConn.Open(); sql = "INSERT INTO " + this.comboBox1.SelectedItem.ToString() + " (Names)" + "Values (@name)"; OleDbCommand myCommand = new OleDbCommand(sql, dbConn); myCommand.Parameters.Add("@name", OleDbType.VarChar).Value = name; myCommand.ExecuteNonQuery(); myCommand.Connection.Close(); } catch (Exception err) { MessageBox.Show("Error: " + err.Message.ToString()); } } private void SelectName() { string strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + ch + openFileDialog1.FileName + ch; try { using (dbConn = new OleDbConnection(strCon)) { dbConn.Open(); sql = "SELECT Name FROM Names"; OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(sql, dbConn)); DataSet ds = new DataSet(); adapter.Fill(ds, "Names"); comboBox2.Items.Clear(); this.comboBox2.DataSource = ds.Tables["Names"]; this.comboBox2.DisplayMember = "Name"; } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message.ToString()); } } } }

I am trying to insert an Access table record with information from a combobox. I am using a winform and C#. I have simplified my project to just include the problem area. I am showing three methods with 4 controls (2 buttons and 2 comboboxes. The first method is connecting to an Access database and then showing a list of the tables and views in the first combobox. this method will also call the last method, SelectName() and fill the second combobox with field contents from a predetermined table from the selected database. The second method (buttonInsert_Click()) is where my problem lies. I would like the method to insert into one of the selected tables from combobox1 the selected item from combobox2 when the insert button is clicked. The only content I can get to insert into the selected table is

System.Data.DataRowView

The C# section of my project is below. Any suggestions are appreciated. Thank You.

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Data.OleDb; namespace DbApp { public partial class Form1 : Form { private char ch = '"'; private OleDbConnection dbConn; private string sql = ""; public Form1() { InitializeComponent(); } private void buttonConnect_Click(object sender, EventArgs e) { string connectionString = ""; string stringData = ""; openFileDialog1.Filter = ""; openFileDialog1.ShowDialog(); Text = openFileDialog1.FileName; stringData = openFileDialog1.FileName; connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + ch + Text + ch; if (dbConn != null) dbConn.Close(); dbConn = new OleDbConnection(connectionString); dbConn.Open(); comboBox1.Items.Clear(); DataTable info = dbConn.GetSchema("Tables"); for (int x = 0; x < info.Rows.Count; ++x) { if ((info.Rows[x][3].ToString() == "TABLE") || (info.Rows[x][3].ToString() == "VIEW")) { comboBox1.Items.Add((object)info.Rows[x][2].ToString()); } } SelectName(); } private void buttonInsert_Click(object sender, EventArgs e) { string name = this.comboBox2.SelectedItem.ToString(); try { dbConn = new OleDbConnection(); dbConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + ch + openFileDialog1.FileName + ch; dbConn.Open(); sql = "INSERT INTO " + this.comboBox1.SelectedItem.ToString() + " (Names)" + "Values (@name)"; OleDbCommand myCommand = new OleDbCommand(sql, dbConn); myCommand.Parameters.Add("@name", OleDbType.VarChar).Value = name; myCommand.ExecuteNonQuery(); myCommand.Connection.Close(); } catch (Exception err) { MessageBox.Show("Error: " + err.Message.ToString()); } } private void SelectName() { string strCon = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" + ch + openFileDialog1.FileName + ch; try { using (dbConn = new OleDbConnection(strCon)) { dbConn.Open(); sql = "SELECT Name FROM Names"; OleDbDataAdapter adapter = new OleDbDataAdapter(new OleDbCommand(sql, dbConn)); DataSet ds = new DataSet(); adapter.Fill(ds, "Names"); comboBox2.Items.Clear(); this.comboBox2.DataSource = ds.Tables["Names"]; this.comboBox2.DisplayMember = "Name"; } } catch (Exception ex) { MessageBox.Show("Error: " + ex.Message.ToString()); } } } }

最满意答案

尝试这个:

string name = this.comboBox2.Text;

Try this:

string name = this.comboBox2.Text;

更多推荐

本文发布于:2023-08-03 14:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1395107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   字段   文本   fill   Access

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!