- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我的数据库有问题。我有两个表 Employee
和 Department
。
Employee
包含 7 列:
Id (int), Name (nvarchar(max)), LastName(nvarcharmax), Age(int), Dep_nt(nvarchar(max)), Profession (nvarchar(max)), Salary (real).
Department
包含两列:
Id (int), DepartmentName(nvarchar(max)
我有一个 DpListBox
(ListBox),其中包含有关部门的信息,以及 Ep
(Listview),其中包含有关在这些部门工作的员工的信息。
我需要根据从 DpListBox
中选择的值填充 Ep
。主要问题在于这行代码:
SqlCommand command = new SqlCommand($@"SELECT * FROM Employee WHERE Dep_nt=" + DpListBox.SelectedValue.ToString(), connection);
我认为 DpListBox.SelectedValue = System.Data.DataRowView
因为我的数据表包含两个字段 Id
和 DepartmentName
我只需要在这里部门名称
。
下面是我的代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace BigCompanyinc
{
/// <summary>
/// Логика взаимодействия для MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string connectionString = @"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Hospital;Integrated Security=True;Pooling=False";
SqlDataAdapter adapter = new SqlDataAdapter();
public MainWindow()
{
InitializeComponent();
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand("SELECT DepartmentName FROM Department ", connection);
adapter.SelectCommand = command;
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
DpListBox.ItemsSource = dataTable.DefaultView;
MessageBox.Show("Выберите департамент, чтобы начать работу.");
}
/// <summary>
/// Добавить новый департамент
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button2_Click(object sender, RoutedEventArgs e)
{
var sql = String.Format("INSERT INTO Department (DepartmentName) " + "VALUES (N'{0}')",
Name7.Text);
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand(sql, connection);
command.ExecuteNonQuery();
command = new SqlCommand(@"UPDATE Deparment SET DepartmentName = @DepartmentName WHERE ID =@ID", connection);
command.Parameters.Add("@DepartmentName", SqlDbType.NVarChar, -1, "DepartmentName");
SqlParameter param = command.Parameters.Add("@ID", SqlDbType.Int, 0, "ID");
param.SourceVersion = DataRowVersion.Original;
adapter.UpdateCommand = command;
DataTable dataTable = new DataTable();
adapter.Fill(dataTable);
DpListBox.ItemsSource = dataTable.DefaultView;
}
}
/// <summary>
/// Выбран новый элемент ListBox для коллекции департаментов
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void DpListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
SqlConnection connection = new SqlConnection(connectionString);
SqlDataAdapter adapter = new SqlDataAdapter();
Console.WriteLine(DpListBox.SelectedValue.ToString());
SqlCommand command = new SqlCommand($@"SELECT * FROM Employee WHERE Dep_nt=" + DpListBox.SelectedValue.ToString(), connection);
adapter.SelectCommand = command;
DataTable dataTable1 = new DataTable();
adapter.Fill(dataTable1);
Ep.ItemsSource = dataTable1.DefaultView;
}
}
}
我使用 WPF,这里是 XAML 代码:
<Window x:Name="Staff" x:Class="BigCompanyinc.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:BigCompanyinc"
mc:Ignorable="d"
Title="Staff" Height="513.5" Width="991.833" ResizeMode="NoResize"
Icon="icon1.ico">
<Grid Height="504" VerticalAlignment="Top" Margin="10,0,-23,-19">
<Grid.RowDefinitions>
</Grid.RowDefinitions>
<TextBox x:Name="Header2" HorizontalAlignment="Left" Margin="10,39,0,0" VerticalAlignment="Top" Width="144" Height="22" Text="Добавить департамент" IsReadOnly="True"/>
<TextBox x:Name="Name6" HorizontalAlignment="Left" Margin="10,69,0,0" VerticalAlignment="Top" Width="144" Height="22" Text="Название" IsReadOnly="True"/>
<TextBox x:Name="Name7" HorizontalAlignment="Left" Margin="10,99,0,0" VerticalAlignment="Top" Width="144" Height="22" IsReadOnly="False"/>
<Button x:Name="button2" HorizontalAlignment="Left" Margin="10,129,0,0" VerticalAlignment="Top" Width="70" Height="22" Background="LightBlue"
Content="Добавить" Click="Button2_Click"/>
<TextBox x:Name="Department2" HorizontalAlignment="Left" Margin="10,12,0,0" VerticalAlignment="Top" Width="144" Height="22" Text="Департамент" IsReadOnly="True"/>
<ListBox ItemsSource="{Binding Dep}" SelectedItem="DepartmentName" x:Name="DpListBox" HorizontalAlignment="Left" Margin="10,165,0,138" Width="144" SelectionChanged="DpListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding DepartmentName}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<ListView x:Name="Ep" HorizontalAlignment="Left" Height="201" Margin="164,165,0,0" VerticalAlignment="Top" Width="791">
<ListView.View>
<GridView>
<GridViewColumn Width="130" Header="Имя" DisplayMemberBinding="{Binding Name}"/>
<GridViewColumn Width="130" Header="Фамилия" DisplayMemberBinding="{Binding LastName}"/>
<GridViewColumn Width="130" Header="Возраст" DisplayMemberBinding="{Binding Age}"/>
<GridViewColumn Width="130" Header="Департамент" DisplayMemberBinding="{Binding Dep_nt}"/>
<GridViewColumn Width="130" Header="Профессия" DisplayMemberBinding="{Binding Profession}"/>
<GridViewColumn Width="130" Header="Заработная плата" DisplayMemberBinding="{Binding Salary}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</Window>
最佳答案
您需要执行以下操作才能从 DataRowView
获取列值。
DataRowView dataRowView = DpListBox.SelectedItem as DataRowView;
string value = "";
if (dataRowView != null) {
value = dataRowView.Row["DepartmentName"] as string;
}
然后您必须将 value
而不是 DpListBox.SelectedValue.ToString()
传递给您的查询。
SqlCommand command = new SqlCommand(@"SELECT * FROM Employee WHERE Dep_nt='" + value + "'", connection);
注意:始终尝试使用准备好的语句(参数化查询)这将防止 SQL 注入(inject)。
因此参数化查询将是。
SqlCommand command = new SqlCommand(@"SELECT * FROM Employee WHERE Dep_nt=@Dep_nt", connection);
command.Parameters.AddWithValue("@Dep_nt", value);
关于c# - System.Data.SqlClient.SqlException : "The multi-part identifier "System. Data.DataRowView“无法绑定(bind)。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53568321/
我将combobox.datasource 设置为一个数据 View 项(以便它绑定(bind)到一个表),当我从combobox.selectedvalue 获得返回值时。返回错误,因为它的类型为“
在验证 ComboBox 时,我试图检查 ComboBox 中的值是否在数据绑定(bind)到所述 ComboBox 的值列表中。 数据源是 BindingSource,底层项目是 DataRowVi
我需要在用户点击时从 dataGrid 中收取项目值,将其放入数据行 View 中以获取第一个值“IdEmployee”并分配给变量。 这是我的方法,问题是我的变量 dataRowView 是 Nul
我在 winform 中有一个组合框,它通过调用 MySQL 中的存储过程来获取数据。 我的存储过程: CREATE PROCEDURE `GetCourses`() BEGIN SELECT cou
DataGrid.ItemSource 在代码后面设置为 dataview。列名和计数需要即时更改,因此对象列表不能很好地工作。 我的表格显示完全正常。这很好,但是我的 GridCellStyle 将
为了将DataTable连接到ComboBox或列出项目,我还需要添加其他内容吗? 当前,它将显示项目,但是它们都是System.Data.DataRowView,我不确定为什么。 ... ...
我有一个问题: SqlDataAdapter da = new SqlDataAdapter("SELECT dbo.User.name FROM dbo.User", con); DataTable
我想知道 DataView 和 DataRowView 的区别。我查阅了 MSDN,它说 DataRowView 只是 DataView 的自定义表示。 但是您也可以将 DataView 转换为 Da
我使用 linq to sql 来填充 gridview: var results = from r in db.MyForm1_hosps where r
protected void Page_Load(object sender, System.EventArgs e) { DataTable dt = GetDataTable("selec
场景是我想从所选行的 onMouseDoubleClick 事件中获取值。我将该行转换到 DataRowView,但该对象没有任何值(value)。在调试时,当我将光标悬停在 SelectedItem
除了循环、枚举 ICollection 以分别将它们转换为已知 ICollection 中的列的类型化列表之外,是否有任何可能的方法。我正在使用 .net Framework 2.0、C#2.0。预期
我们刚刚使用 Prism 启动了一个 WPF 项目。我们的问题是我们必须使用 System.Data.DataRowView 而不是 DataGrid 的类型化版本。我们正在使用这样的代码:
我有一个在主窗体打开时调用的方法 private void populateComboBoxes() { SqlCeDataAdapter breakfastAdapter = new Sql
我有一个组合框 cbAnalytes: cbAnalytes.DataSource = ConnectandReadList(qcvalues_query); ConnectandReadList 在
情况如下:我需要绑定(bind)一个 WPF FixedPage反对 DataRow .绑定(bind)不适用于 DataRows ;他们反对DataRowViews .我需要以最通用的方式执行此操作
我有一个ComboBox,它需要在下拉菜单中显示多个字段。但只想存储一个。现在,它可以显示多个字段。但是无论我选择什么,文本框中显示的值都是“System.Data.DataRowView”。有谁知道
我有这个代码 private void cmb_public_manag_SelectedIndexChanged(object sender, EventArgs e) { //DB_Ma
我正在尝试使用列中的值填充下拉列表。现在的问题是:我没有在下拉列表中获得实际值(国家代码,如印度 (+61))。相反,我在下拉列表中得到“System.Data.DataRowView”(多次)。
我的C#代码如下 private void offHwyManufacturer_SelectedValueChanged(object sender, EventArgs e) {
我是一名优秀的程序员,十分优秀!