将ComboBoxItem保存为数据库中的值?

时间:2019-06-09 09:00:12

标签: c# wpf xaml

我正在使用文本框将一些数据插入db,并且工作正常。现在我需要添加选项以选择将在哪些条件(状态)下保存该数据。 我创建了ComboBox并添加了3个ComboBoxItems,但是我不知道如何为这3个项目赋值,因此我可以将它们保存在db表中。

Xaml代码

   <StackPanel>
            <TextBox Name="txtUser" Width="100" Height="30" Margin="10"> 
    </TextBox>
            <TextBox Name="txtDate" Width="100" Height="30" Margin="10"> 
    </TextBox>
            <TextBox Name="txtAge" Width="100" Height="30" Margin="10"> 
    </TextBox>
            <ComboBox Name="cmb1" Width="140" Margin="10">
                <ComboBoxItem >Something1</ComboBoxItem>
                <ComboBoxItem>Something2</ComboBoxItem>
                <ComboBoxItem>Something3</ComboBoxItem>
            </ComboBox>
  <Button Name="Save" Click="Save_Click" Margin="10" Width="100" 
  Height="30" Content="Save"></Button>
        </StackPanel>

后面的代码

private void Save_Click(object sender, RoutedEventArgs e)
        {

         try
            {

                OracleConnection con1 = new OracleConnection();
                con1.ConnectionString = "Data Source=";

                OracleCommand cmd = new OracleCommand();
                cmd.Connection = con1;

                cmd.CommandText = "Insert into 
  TABLE1 (User, Date, Age) values 
 ('"+txtUser.Text+"','"+txtDate.Text+"','"+txtAge.Text+"')";

                  con1.Open();


                cmd.ExecuteNonQuery();

                MessageBox.Show("Data saved successfully!","Have a nice day");

                con1.Close();

             }
            catch (Exception exe)
            {
                MessageBox.Show(exe.Message);
             }
        }

0 个答案:

没有答案
相关问题