闽公网安备 35020302035485号
在 C# 中,DataTable 类位于 System.Data 命名空间中,用于表示存储在内存中的数据表。该类允许创建、操作和与数据进行交互,并以行和列的形式呈现数据,类似于关系型数据库表。在应用程序中,它通常用于临时存储从数据库获取的数据,或在内存中处理数据。这种方法特别适用于需要增强 DataTable 基础功能的场景,例如添加特定功能、预定义的架构(列)或自定义的数据操作方法。
3.增强类型安全:通过在类中整合逻辑,提升代码的可维护性和类型安全性。
using System.Data;
namespace DatatableStructureExample
{
internal class StudentBaseTable : DataTable
{
string rowString = string.Empty;
public StudentBaseTable(string tableName) : base(tableName)
{
// 在基础类中设置列
InitializeColumns();
}
// 设置公共列的过程
protected virtual void InitializeColumns()
{
// 设置所有派生类使用的标准列
this.Columns.Add("UserId", typeof(int));
this.Columns.Add("UserName", typeof(string));
}
// 堆代码 duidaima.com
// 设置主键列的过程
public void SetPrimaryKey(string columnName)
{
this.PrimaryKey = new DataColumn[] { this.Columns[columnName] };
}
}
}
第 2 步:开发指定特定表的派生类using System.Data;
namespace DatatableStructureExample
{
internal class StudentInformationTable : StudentBaseTable
{
public StudentInformationTable(string tableName) : base(tableName)
{
this.Columns.Add("UserAge", typeof(int));
this.Columns.Add("UserAddress", typeof(string));
this.SetPrimaryKey("UserId");
}
// 添加学生信息的过程
public void AddStudentInformation(int userId, string userName, int userAge, string userAddress)
{
DataRow row = this.NewRow();
row["UserId"] = userId;
row["UserName"] = userName;
row["UserAge"] = userAge;
row["UserAddress"] = userAddress;
this.Rows.Add(row);
}
// 堆代码 duidaima.com
// 根据学号删除学生信息的过程
public void RemoveStudentInformationById(int id)
{
DataRow row = this.Rows.Find(id);
if (row != null)
{
this.Rows.Remove(row);
}
}
}
}
第 3 步:在应用中使用 StudentInformationTable 类<Window x:Class="DatatableStructureExample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="450" Width="800">
<Grid>
<StackPanel Margin="10" Orientation="Vertical">
<!-- 用户 ID -->
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="User ID:" VerticalAlignment="Center" Width="100"/>
<TextBox x:Name="UserIdTextBox" Width="200"/>
</StackPanel>
<!-- 用户名 -->
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="User Name:" VerticalAlignment="Center" Width="100"/>
<TextBox x:Name="UserNameTextBox" Width="200"/>
</StackPanel>
<!-- 用户年龄 -->
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="User Age:" VerticalAlignment="Center" Width="100"/>
<TextBox x:Name="UserAgeTextBox" Width="200"/>
</StackPanel>
<!-- 用户地址 -->
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="User Address:" VerticalAlignment="Center" Width="100"/>
<TextBox x:Name="UserAddressTextBox" Width="200"/>
</StackPanel>
<!-- 删除 ID -->
<StackPanel Orientation="Horizontal" Margin="0,5">
<TextBlock Text="Remove Id:" VerticalAlignment="Center" Width="100"/>
<TextBox x:Name="RemoveIdTextBox" Width="200"/>
</StackPanel>
</StackPanel>
<StackPanel Orientation="Horizontal">
<Button x:Name="BtnCreateRow" Content="Create Row" Height="40" Width="200" Click="BtnCreateRow_Click" Margin="30,0,0,0"/>
<Button x:Name="BtnDeleteRow" Content="Delete Row" Height="40" Width="200" Click="BtnDeleteRow_Click" Margin="20,0,0,0"/>
<Button x:Name="BtnShowDetails" Content="Show Details" Height="40" Width="200" Click="BtnShowDetails_Click" Margin="20,0,0,0"/>
</StackPanel>
<DataGrid x:Name="dataGrid" AutoGenerateColumns="True" Margin="10,252,10,10"/>
</Grid>
</Window>
后端实现using System.Collections.ObjectModel;
using System.Data;
using System.Windows;
namespace DatatableStructureExample
{
public partial class MainWindow : Window
{
StudentInformationTable studentInformation;
private ObservableCollection<DataItemModel> dataItems = new ObservableCollection<DataItemModel>();
public MainWindow()
{
InitializeComponent();
studentInformation = new StudentInformationTable("StudentTable");
}
private void BtnCreateRow_Click(object sender, RoutedEventArgs e)
{
studentInformation.AddStudentInformation(
Convert.ToInt32(UserIdTextBox.Text),
UserNameTextBox.Text,
Convert.ToInt32(UserAgeTextBox.Text),
UserAddressTextBox.Text);
}
private void BtnDeleteRow_Click(object sender, RoutedEventArgs e)
{
studentInformation.RemoveStudentInformationById(Convert.ToInt32(RemoveIdTextBox.Text));
}
private void BtnShowDetails_Click(object sender, RoutedEventArgs e)
{
dataGrid.ItemsSource = null;
if (dataItems.Count > 0)
{
dataItems.Clear();
}
foreach (DataRow item in studentInformation.Rows)
{
if (item != null)
{
var dataItemModel = new DataItemModel
{
Column1 = item[0].ToString(),
Column2 = item[1].ToString(),
Column3 = item[2].ToString(),
Column4 = item[3].ToString()
};
dataItems.Add(dataItemModel);
}
}
dataGrid.ItemsSource = dataItems;
}
}
}
第 4 步:实现结果