Home php教程 php手册 C#DataTable detailed explanation

C#DataTable detailed explanation

Jul 06, 2016 pm 01:30 PM
sys using Quote Add to Detailed explanation

Add reference using System.Data; Create table // Create an empty table DataTable dt = new DataTable(); // Create an empty table named "Table_New" DataTable dt = new DataTable( " Table_New " ); Create Column // 1. Create an empty column DataColumn dc = new DataColumn();dt.Columns.Add(dc); //

Add quote

<span style="color: #0000ff;">using</span> System.Data;
Copy after login

Create table

<span style="color: #008000;">//</span><span style="color: #008000;">创建一个空表</span>
DataTable dt = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DataTable();
</span><span style="color: #008000;">//</span><span style="color: #008000;">创建一个名为"Table_New"的空表</span>
DataTable dt = <span style="color: #0000ff;">new</span> DataTable(<span style="color: #800000;">"</span><span style="color: #800000;">Table_New</span><span style="color: #800000;">"</span>);
Copy after login

Create Column

<span style="color: #008000;">//</span><span style="color: #008000;">1.创建空列</span>
DataColumn dc = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DataColumn();
dt.Columns.Add(dc);
</span><span style="color: #008000;">//</span><span style="color: #008000;">2.创建带列名和类型名的列(两种方式任选其一)</span>
dt.Columns.Add(<span style="color: #800000;">"</span><span style="color: #800000;">column0</span><span style="color: #800000;">"</span>, System.Type.GetType(<span style="color: #800000;">"</span><span style="color: #800000;">System.String</span><span style="color: #800000;">"</span><span style="color: #000000;">));
dt.Columns.Add(</span><span style="color: #800000;">"</span><span style="color: #800000;">column0</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">typeof</span><span style="color: #000000;">(String));
</span><span style="color: #008000;">//</span><span style="color: #008000;">3.通过列架构添加列</span>
DataColumn dc = <span style="color: #0000ff;">new</span> DataColumn(<span style="color: #800000;">"</span><span style="color: #800000;">column1</span><span style="color: #800000;">"</span>,System.Type.GetType(<span style="color: #800000;">"</span><span style="color: #800000;">System.DateTime</span><span style="color: #800000;">"</span><span style="color: #000000;">));
DataColumn dc </span>= <span style="color: #0000ff;">new</span> DataColumn(<span style="color: #800000;">"</span><span style="color: #800000;">column1</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">typeof</span><span style="color: #000000;">(DateTime));
dt.Columns.Add(dc);</span>
Copy after login

Create row

<span style="color: #008000;">//</span><span style="color: #008000;">1.创建空行</span>
DataRow dr =<span style="color: #000000;"> dt.NewRow();
dt.Rows.Add(dr);
</span><span style="color: #008000;">//</span><span style="color: #008000;">2.创建空行</span>
<span style="color: #000000;">dt.Rows.Add();
</span><span style="color: #008000;">//</span><span style="color: #008000;">3.通过行框架创建并赋值</span>
dt.Rows.Add(<span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span>,DateTime.Now);<span style="color: #008000;">//</span><span style="color: #008000;">Add里面参数的数据顺序要和dt中的列的顺序对应 <br>//4.通过复制dt2表的某一行来创建<br></span>dt.Rows.Add(dt2.Rows[i].ItemArray);
Copy after login

Assignment and value

<span style="color: #008000;">//</span><span style="color: #008000;">新建行的赋值</span>
DataRow dr =<span style="color: #000000;"> dt.NewRow();
dr[</span><span style="color: #800080;">0</span>] = <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span>;<span style="color: #008000;">//</span><span style="color: #008000;">通过索引赋值</span>
dr[<span style="color: #800000;">"</span><span style="color: #800000;">column1</span><span style="color: #800000;">"</span>] = DateTime.Now; <span style="color: #008000;">//</span><span style="color: #008000;">通过名称赋值
</span><span style="color: #008000;">//</span><span style="color: #008000;">对表已有行进行赋值</span>
dt.Rows[<span style="color: #800080;">0</span>][<span style="color: #800080;">0</span>] = <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span>; <span style="color: #008000;">//</span><span style="color: #008000;">通过索引赋值</span>
dt.Rows[<span style="color: #800080;">0</span>][<span style="color: #800000;">"</span><span style="color: #800000;">column1</span><span style="color: #800000;">"</span>] = DateTime.Now;<span style="color: #008000;">//</span><span style="color: #008000;">通过名称赋值
</span><span style="color: #008000;">//</span><span style="color: #008000;">取值</span>
<span style="color: #0000ff;">string</span> name=dt.Rows[<span style="color: #800080;">0</span>][<span style="color: #800080;">0</span><span style="color: #000000;">].ToString();
</span><span style="color: #0000ff;">string</span> time=dt.Rows[<span style="color: #800080;">0</span>][<span style="color: #800000;">"</span><span style="color: #800000;">column1</span><span style="color: #800000;">"</span>].ToString();
Copy after login

Filter rows

<span style="color: #008000;">//</span><span style="color: #008000;">选择column1列值为空的行的集合</span>
DataRow[] drs = dt.Select(<span style="color: #800000;">"</span><span style="color: #800000;">column1 is null</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">选择column0列值为"李四"的行的集合</span>
DataRow[] drs = dt.Select(<span style="color: #800000;">"</span><span style="color: #800000;">column0 = '李四'</span><span style="color: #800000;">"</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">筛选column0列值中有"张"的行的集合(模糊查询)</span>
DataRow[] drs = dt.Select(<span style="color: #800000;">"</span><span style="color: #800000;">column0 like '张%'</span><span style="color: #800000;">"</span>);<span style="color: #008000;">//</span><span style="color: #008000;">如果的多条件筛选,可以加 and 或 or
</span><span style="color: #008000;">//</span><span style="color: #008000;">筛选column0列值中有"张"的行的集合并按column1降序排序</span>
DataRow[] drs = dt.Select(<span style="color: #800000;">"</span><span style="color: #800000;">column0 like '张%'</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">column1 DESC</span><span style="color: #800000;">"</span>);
Copy after login

Delete row

<span style="color: #008000;">//</span><span style="color: #008000;">使用DataTable.Rows.Remove(DataRow)方法</span>
dt.Rows.Remove(dt.Rows[<span style="color: #800080;">0</span><span style="color: #000000;">]);
</span><span style="color: #008000;">//</span><span style="color: #008000;">使用DataTable.Rows.RemoveAt(index)方法</span>
dt.Rows.RemoveAt(<span style="color: #800080;">0</span><span style="color: #000000;">);
</span><span style="color: #008000;">//</span><span style="color: #008000;">使用DataRow.Delete()方法</span>
dt.Row[<span style="color: #800080;">0</span><span style="color: #000000;">].Delete();
dt.AcceptChanges();

</span><span style="color: #008000;">//-----</span><span style="color: #008000;">区别和注意点-----
</span><span style="color: #008000;">//</span><span style="color: #008000;">Remove()和RemoveAt()方法是直接删除
</span><span style="color: #008000;">//</span><span style="color: #008000;">Delete()方法只是将该行标记为deleted,但是还存在,还可DataTable.RejectChanges()回滚,使该行取消删除。
</span><span style="color: #008000;">//</span><span style="color: #008000;">用Rows.Count来获取行数时,还是删除之前的行数,需要使用DataTable.AcceptChanges()方法来提交修改。
</span><span style="color: #008000;">//</span><span style="color: #008000;">如果要删除DataTable中的多行,应该采用倒序循环DataTable.Rows,而且不能用foreach进行循环删除,因为正序删除时索引会发生变化,程式发生异常,很难预料后果。</span>
<span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = dt.Rows.Count - <span style="color: #800080;">1</span>; i >= <span style="color: #800080;">0</span>; i--<span style="color: #000000;">)
{
  dt.Rows.RemoveAt(i);
}</span>
Copy after login

Copy table

<span style="color: #008000;">//</span><span style="color: #008000;">复制表,同时复制了表结构和表中的数据</span>
DataTable dtNew = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DataTable();
dtNew </span>=<span style="color: #000000;"> dt.Copy();
</span><span style="color: #008000;">//</span><span style="color: #008000;">复制表</span>
DataTable dtNew = dt.Copy();  <span style="color: #008000;">//</span><span style="color: #008000;">复制dt表数据结构</span>
dtNew.Clear()  <span style="color: #008000;">//</span><span style="color: #008000;">清空数据</span>
<span style="color: #0000ff;">for</span> (<span style="color: #0000ff;">int</span> i = <span style="color: #800080;">0</span>; i < dt.Rows.Count; i++<span style="color: #000000;">)
{
    </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (条件语句)
    {
         dtNew.Rows.Add(dt.Rows[i].ItemArray);  </span><span style="color: #008000;">//</span><span style="color: #008000;">添加数据行</span>
<span style="color: #000000;">    }
}
</span><span style="color: #008000;">//</span><span style="color: #008000;">克隆表,只是复制了表结构,不包括数据</span>
DataTable dtNew = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DataTable();
dtNew </span>=<span style="color: #000000;"> dt.Clone();
</span><span style="color: #008000;">//</span><span style="color: #008000;">如果只需要某个表中的某一行</span>
DataTable dtNew = <span style="color: #0000ff;">new</span><span style="color: #000000;"> DataTable();
dtNew </span>=<span style="color: #000000;"> dt.Copy();
dtNew.Rows.Clear();</span><span style="color: #008000;">//</span><span style="color: #008000;">清空表数据</span>
dtNew.ImportRow(dt.Rows[<span style="color: #800080;">0</span>]);<span style="color: #008000;">//</span><span style="color: #008000;">这是加入的是第一行</span>
Copy after login

Table sort

DataTable dt = <span style="color: #0000ff;">new</span> DataTable();<span style="color: #008000;">//</span><span style="color: #008000;">创建表</span>
dt.Columns.Add(<span style="color: #800000;">"</span><span style="color: #800000;">ID</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">typeof</span>(Int32));<span style="color: #008000;">//</span><span style="color: #008000;">添加列</span>
dt.Columns.Add(<span style="color: #800000;">"</span><span style="color: #800000;">Name</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">typeof</span><span style="color: #000000;">(String));
dt.Columns.Add(</span><span style="color: #800000;">"</span><span style="color: #800000;">Age</span><span style="color: #800000;">"</span>, <span style="color: #0000ff;">typeof</span><span style="color: #000000;">(Int32));
dt.Rows.Add(</span><span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[] { <span style="color: #800080;">1</span>, <span style="color: #800000;">"</span><span style="color: #800000;">张三</span><span style="color: #800000;">"</span> ,<span style="color: #800080;">20</span>});<span style="color: #008000;">//</span><span style="color: #008000;">添加行</span>
dt.Rows.Add(<span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[] { <span style="color: #800080;">2</span>, <span style="color: #800000;">"</span><span style="color: #800000;">李四</span><span style="color: #800000;">"</span> ,<span style="color: #800080;">25</span><span style="color: #000000;">});
dt.Rows.Add(</span><span style="color: #0000ff;">new</span> <span style="color: #0000ff;">object</span>[] { <span style="color: #800080;">3</span>, <span style="color: #800000;">"</span><span style="color: #800000;">王五</span><span style="color: #800000;">"</span> ,<span style="color: #800080;">30</span><span style="color: #000000;">});
DataView dv </span>= dt.DefaultView;<span style="color: #008000;">//</span><span style="color: #008000;">获取表视图</span>
dv.Sort = <span style="color: #800000;">"</span><span style="color: #800000;">ID DESC</span><span style="color: #800000;">"</span>;<span style="color: #008000;">//</span><span style="color: #008000;">按照ID倒序排序</span>
dv.ToTable();<span style="color: #008000;">//</span><span style="color: #008000;">转为表</span>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to add a TV to Mijia How to add a TV to Mijia Mar 25, 2024 pm 05:00 PM

Many users are increasingly favoring the electronic ecosystem of Xiaomi smart home interconnection in modern life. After connecting to the Mijia APP, you can easily control the connected devices with your mobile phone. However, many users still don’t know how to add Mijia to their homes. app, then this tutorial guide will bring you the specific connection methods and steps, hoping to help everyone in need. 1. After downloading Mijia APP, create or log in to Xiaomi account. 2. Adding method: After the new device is powered on, bring the phone close to the device and turn on the Xiaomi TV. Under normal circumstances, a connection prompt will pop up. Select &quot;OK&quot; to enter the device connection process. If no prompt pops up, you can also add the device manually. The method is: after entering the smart home APP, click the 1st button on the lower left

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

How to fix Cldflt.sys blue screen error? How to fix Cldflt.sys blue screen error? Feb 20, 2024 am 10:30 AM

If you encounter a SYSTEM_SERVICE_EXCEPTION blue screen error and find that the Cldflt.sys file is faulty, this article will provide you with ways to resolve this issue. What is Cldflt.sys? The cloud file small filter driver (Cldflt.sys) is a key service in Windows systems and is used to manage Windows cloud storage functions. Its role is to assist users in synchronizing and managing data files between local devices and the cloud to ensure timely updates of file storage. What causes Cldflt.sysBSOD errors? Issues with OneDrive: Micro due to Cldflt.sys file or errors related to cloud storage and its sync

How to add a new script in Tampermonkey-How to delete a script in Tampermonkey How to add a new script in Tampermonkey-How to delete a script in Tampermonkey Mar 18, 2024 pm 12:10 PM

Tampermonkey Chrome extension is a user script management plug-in that improves user efficiency and browsing experience through scripts. So how does Tampermonkey add new scripts? How to delete the script? Let the editor give you the answer below! How to add a new script to Tampermonkey: 1. Take GreasyFork as an example. Open the GreasyFork web page and enter the script you want to follow. The editor here chooses one-click offline download. 2. Select a script. , after entering the script page, you can see the button to install this script. 3. Click to install this script to come to the installation interface. Just click here to install. 4. We can see the installed one-click in the installation script.

How to connect to Polygon network in MetaMask wallet? Tutorial guide for connecting MetaMask wallet to Polygon network How to connect to Polygon network in MetaMask wallet? Tutorial guide for connecting MetaMask wallet to Polygon network Jan 19, 2024 pm 04:36 PM

How to add a PolygonMainnet network To use MATIC (Polygon) with Metamask, you need to add a private network called "PolygonMainnet". Transferring in with the wrong network address can cause problems, so be sure to use the "PolygonMainnet" network before transferring out of $MATIC. The Metamask wallet is connected to the Ethereum mainnet by default, but we can simply add "PolygonMainnet" and use $MATIC. Just a few simple copy and paste steps and you're done. First, in the Metamask wallet, click on the network option in the upper right corner and select "C

Outlook stuck on adding account [Fixed] Outlook stuck on adding account [Fixed] Mar 23, 2024 pm 12:21 PM

When you encounter problems adding accounts in Outlook, you can try the following solutions to resolve it. Typically this can be caused by a faulty network connection, corrupted user profiles, or other temporary issues. Through the methods provided in this article, you can easily solve these problems and ensure that your Outlook can run normally. Outlook stuck on adding account If your Outlook is stuck on adding account, then use these fixes mentioned below: Disconnect and reconnect the internet Temporarily disable antivirus software Create a new Outlook profile Try adding account in safe mode Disable IPv6 Run Microsoft Support and Recovery Assistant Repair Office Application Outlook Add Account Required

Common ways to add elements to Java arrays Common ways to add elements to Java arrays Feb 21, 2024 am 11:21 AM

Common ways to add elements to Java arrays, specific code examples are required In Java, an array is a common data structure that can store multiple elements of the same type. In actual development, we often need to add new elements to the array. This article will introduce common methods of adding elements to arrays in Java and provide specific code examples. A simple way to create a new array using a loop is to create a new array, copy the elements of the old array into the new array, and add the new elements. The code example is as follows: //original array i

See all articles