C# WPF:将 ComboBox 中的项目添加到 Access DB
C# WPF: Add item from ComboBox to Access DB
最近几天我一直在努力解决这个问题。我有一个带有 DO/SEE/EXPECT 的表单,带有 TextBoxes(这有效),但是当我尝试添加一个包含 3 个项目的 ComboBox 时,我总是在 cmd.ExecuteNonQuery() 上收到 OleDbException ;:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Data type mismatch in criteria expression.
我做错了什么?
using System.Windows;
using System.Data.OleDb;
using System.Configuration;
namespace WpfApplication3
{
public partial class Records : Window
{
public Records()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "insert into [tblInboxRecords](DO, SEE, EXPECT, Segment) Values (@do,@see,@ex,@sg)";
cmd.Parameters.AddWithValue("@do", txtdo.Text);
cmd.Parameters.AddWithValue("@see", txtsee.Text);
cmd.Parameters.AddWithValue("@ex", txtexpect.Text);
cmd.Parameters.AddWithValue("@sg", boxsegment.Text); // This is the combobox
cmd.Connection = con;
cmd.ExecuteNonQuery();
{
MessageBox.Show("Item Inserted");
}
}
}
}
这是 ComboBox 的 .xaml:
<ComboBox x:Name="boxsegment" HorizontalAlignment="Left" Height="20" Margin="72,85,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="Type A (Operator)"/>
<ComboBoxItem Content="Type B (Urgent)"/>
<ComboBoxItem Content="Type C (Critical)"/>
</ComboBox>
当我寻找解决方案时,我只能找到如何使用您的数据库中的项目到 WPF,而没有找到其他方法。
找到了!
通过使用string SegmentBox = boxsegment.Text.ToString();
而不是cmd.Parameters.AddWithValue("@sg", boxsegment.Text);
最近几天我一直在努力解决这个问题。我有一个带有 DO/SEE/EXPECT 的表单,带有 TextBoxes(这有效),但是当我尝试添加一个包含 3 个项目的 ComboBox 时,我总是在 cmd.ExecuteNonQuery() 上收到 OleDbException ;:
An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll
Additional information: Data type mismatch in criteria expression.
我做错了什么?
using System.Windows;
using System.Data.OleDb;
using System.Configuration;
namespace WpfApplication3
{
public partial class Records : Window
{
public Records()
{
InitializeComponent();
}
private void button_Click(object sender, RoutedEventArgs e)
{
OleDbConnection con = new OleDbConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["Connection"].ToString();
con.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "insert into [tblInboxRecords](DO, SEE, EXPECT, Segment) Values (@do,@see,@ex,@sg)";
cmd.Parameters.AddWithValue("@do", txtdo.Text);
cmd.Parameters.AddWithValue("@see", txtsee.Text);
cmd.Parameters.AddWithValue("@ex", txtexpect.Text);
cmd.Parameters.AddWithValue("@sg", boxsegment.Text); // This is the combobox
cmd.Connection = con;
cmd.ExecuteNonQuery();
{
MessageBox.Show("Item Inserted");
}
}
}
}
这是 ComboBox 的 .xaml:
<ComboBox x:Name="boxsegment" HorizontalAlignment="Left" Height="20" Margin="72,85,0,0" VerticalAlignment="Top" Width="120">
<ComboBoxItem Content="Type A (Operator)"/>
<ComboBoxItem Content="Type B (Urgent)"/>
<ComboBoxItem Content="Type C (Critical)"/>
</ComboBox>
当我寻找解决方案时,我只能找到如何使用您的数据库中的项目到 WPF,而没有找到其他方法。
找到了!
通过使用string SegmentBox = boxsegment.Text.ToString();
而不是cmd.Parameters.AddWithValue("@sg", boxsegment.Text);