如何在 C# 中查询和检索数据表中的信息

发布于 2024-09-28 13:03:27 字数 240 浏览 4 评论 0原文

我正在将文件夹中的信息放入数据表中。我使用以下代码行将信息放入数据表中:

dtUpgradeFileInfo.Rows.Add(nums[0],nums[1],nums[2],test1);

它似乎有效,但我对 C# 中的数据表不如 VB 中的熟悉。如果第一列中有多行具有相同的值,我将如何搜索第一列具有特定值且第三列具有最高值的数据表。我也不确定如何在找到所需的行后检索信息。每列的类型分别是int、int、int、string。

I am putting information from a folder into a datatable. I'm putting the info into the datatable using the following code line:

dtUpgradeFileInfo.Rows.Add(nums[0],nums[1],nums[2],test1);

It appears to be working but I'm not as familiar with datatables in C# as I am in VB. How would i search the datatable where the first column has a certain value and the third column has the highest value in case there are multiple rows with the same value in the first column. I am also unsure of how to retrieve the info once I've found the row I need. The type for each column is int,int,int,string respectively.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

原谅我要高飞 2024-10-05 13:03:27

如果您所说的 VB 是指 VB.NET 而不是 VB6 之类的东西,那么使用 DataTable(而不是旧版 VB 记录集)的代码在 C# 中将是相同的。相反,您将采用的方法,显然语法会有所不同,因为它是 C#。有分号和方括号,您可以在其中排除圆括号。但他们使用相同的对象,调用相同的方法。

无论如何,你可以这样做(C# 3.0+)

DataRow matchingRow = (from DataRow row in dtUpgradeFileInfo.Rows
                       where (int)row["Column1"] == yourValue
                       orderby (int)row["Column3"] descending
                       select row).FirstOrDefault();

if (matchingRow != null)
{
    // get to work
}

并且对于非 LINQ 答案(任何版本的 C#)

string filter = "Column1 = " + yourValue.ToString();
string sortOrder = "Column3 desc";

dtUpgradeFileInfo.DefaultView.RowFilter = filter;
dtUpgradeFileInfo.DefaultView.Sort = sortOrder;
DataRow myRow = null;
DataTable filteredTable = dtUpgradeFileInfo.DefaultView.ToTable();
if (filteredTable.Rows.Count > 0)
    myRow = filteredTable.Rows[0];

if (myRow != null)
{
    // get to work
}

If by VB you mean VB.NET and not something like VB6, then the code to work with DataTables (as opposed to legacy VB recordsets) will be the same in C#. Rather, the methods you would employ, obviously the syntax will be differently because it's C#. There's semi-colons and brackets where you might except parentheses. But they're using the same objects, calling the same methods.

Anyway, you could do this (C# 3.0+)

DataRow matchingRow = (from DataRow row in dtUpgradeFileInfo.Rows
                       where (int)row["Column1"] == yourValue
                       orderby (int)row["Column3"] descending
                       select row).FirstOrDefault();

if (matchingRow != null)
{
    // get to work
}

And for a non-LINQ answer (any version of C#)

string filter = "Column1 = " + yourValue.ToString();
string sortOrder = "Column3 desc";

dtUpgradeFileInfo.DefaultView.RowFilter = filter;
dtUpgradeFileInfo.DefaultView.Sort = sortOrder;
DataRow myRow = null;
DataTable filteredTable = dtUpgradeFileInfo.DefaultView.ToTable();
if (filteredTable.Rows.Count > 0)
    myRow = filteredTable.Rows[0];

if (myRow != null)
{
    // get to work
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文