我制作了wpf数据库应用程序,我想使用组合框从数据库中选择表。
我有一个包含十个表的数据库。我可以连接到数据库,也可以从表中选择/更新/插入...项目。我需要在表之间切换。例如,如果单击“表1”,将选择“表1”,如果单击“表2”,则将选择“表2”。我相信Combobox对我的应用程序有益。这是我的选择代码:
public MainWindow()
{
InitializeComponent();
loadData();
}
public void loadData()
{
// vytvoření spojení
MySqlConnection con = new MySqlConnection(spojeni);
con.Open();
MySqlCommand vybrat = con.CreateCommand();
vybrat.CommandText = "SELECT * FROM barva";
MySqlDataAdapter adapt = new MySqlDataAdapter(vybrat);
DataSet data = new DataSet();
adapt.Fill(data);
dtGrid.ItemsSource = data.Tables[0].DefaultView;
}
PS。我为我的英语道歉
答案 0 :(得分:0)
我猜您正在寻找类似的东西(连接字符串属于web.config上的配置):
<connectionStrings>
<add name="YOUR CONNECTION" connectionString="Data Source= ;Initial Catalog= ; User ID= ;Password= ;" providerName="System.Data.SqlClient" />
</connectionStrings>
//Connection to Web.config connectionStrings
DataTable database = new DataTable();
string dbString = ConfigurationManager.ConnectionStrings["YOUR CONNECTION"].ConnectionString;
using (SqlConnection con = new SqlConnection(dbString))
{
try
{
//SQL query
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM sys.tables", con);
adapter.Fill(database);
//Populate ddlTable DropDownList
ddlTable.DataSource = database;
ddlTable.DataTextField = "name";
ddlTable.DataValueField = "name";
ddlTable.DataBind();
ddlTable.Items.Insert(0, new ListItem("-- Select Table --", "0"));
}
catch (Exception)
{
}
}
答案 1 :(得分:0)
这是一个非常简单的示例,将组合框绑定到字符串列表,然后在按下按钮时使用选定的字符串。
这是C#代码隐藏文件:
using System.Collections.Generic;
using System.Windows;
namespace WpfCombobox
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
}
public string MySimpleStringProperty { get; set; }
public List<string> MyListProperty { get; set; } = new List<string>() { "one", "two", "three" };
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
MessageBox.Show($"Item is {this.MySimpleStringProperty}");
}
}
}
很明显,您不仅可以在消息框中显示选定的项目,还可以在SQL中使用它。
这是WPF:
<Window x:Class="WpfCombobox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfCombobox"
mc:Ignorable="d"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<StackPanel>
<ComboBox ItemsSource="{Binding MyListProperty}"
SelectedItem="{Binding MySimpleStringProperty}"
IsSynchronizedWithCurrentItem="True"
Text="Select Option"
Margin="5"/>
<Button Click="ButtonBase_OnClick" Content="Click Me!" Margin="5" />
</StackPanel>
</Window>
请注意,组合框有一个ItemSource
和一个one, two, three
,SelectedItem
绑定到字符串列表Declare @año int, @mes int, @m int
Set @año = Year(Getdate())
Set @mes = Month(Getdate())
Set @m = 0
While @año < 2014 begin
While @m < 12 begin
Select @año as año, @m as mes
set @m = @m + 1
End
Set @año = @año - 1
Set @m = 0
End
,当用户选择一个项目时,[HttpGet]
public IHttpActionResult Order(int orderId) {
// ...
var response = new OrderResponse {
Data = orderData,
Message = message,
};
return Json(response);
}
会更改。