search
HomeBackend DevelopmentC#.Net TutorialC# DataRow.ItemArray 属性

C# DataRow.ItemArray 属性

Feb 21, 2017 am 11:03 AM
c#

DataRow.ItemArray property
Gets or sets all the values ​​of this row through an array.
Namespace: System.Data

Assembly: System.Data (in system.data.dll)

Code example:


private void CreateRowsWithItemArray()
{
    // Make a DataTable using the function below.
    DataTable dt = MakeTableWithAutoIncrement();
    DataRow relation;
    // Declare the array variable.
    object [] rowArray = new object[2];
    // Create 10 new rows and add to DataRowCollection.
    for(int i = 0; i <10; i++)
    {
        rowArray[0]=null;
        rowArray[1]= "item " + i;
        relation = dt.NewRow();
        relation.ItemArray = rowArray;
        dt.Rows.Add(relation);
    }
    PrintTable(dt);
}
 
private DataTable MakeTableWithAutoIncrement()
{
    // Make a table with one AutoIncrement column.
    DataTable table = new DataTable("table");
    DataColumn idColumn = new DataColumn("id", 
        Type.GetType("System.Int32"));
    idColumn.AutoIncrement = true;
    idColumn.AutoIncrementSeed = 10;
    table.Columns.Add(idColumn);

    DataColumn firstNameColumn = new DataColumn("Item", 
        Type.GetType("System.String"));
    table.Columns.Add(firstNameColumn);
    return table;
}
 
private void PrintTable(DataTable table)
{
    foreach(DataRow row in table.Rows)
    {
        foreach(DataColumn column in table.Columns)
        {
            Console.WriteLine(row[column]);
        }
    }
}

Exception:



##Exception typeConditions
ArgumentException

The array is greater than the number of columns in the table.

InvalidCastException

The value in the array does not match the DataType in its corresponding DataColumn.

ConstraintException

The editor broke the constraint.

ReadOnlyException

An editor attempted to change the value of a read-only column.

NoNullAllowedException

编辑试图将空值放在 DataColumn 对象的 AllowDBNull 为 false 的列中。

DeletedRowInaccessibleException

该行已被删除。

DataRow.ItemArray 属性源代码实现:


public object[] ItemArray
{
	get
	{
		int defaultRecord = this.GetDefaultRecord();
		object[] array = new object[this._columns.Count];
		for (int i = 0; i < array.Length; i++)
		{
			DataColumn dataColumn = this._columns[i];
			array[i] = dataColumn[defaultRecord];
		}
		return array;
	}
	set
	{
		if (value == null)
		{
			throw ExceptionBuilder.ArgumentNull("ItemArray");
		}
		if (this._columns.Count < value.Length)
		{
			throw ExceptionBuilder.ValueArrayLength();
		}
		DataColumnChangeEventArgs dataColumnChangeEventArgs = null;
		if (this._table.NeedColumnChangeEvents)
		{
			dataColumnChangeEventArgs = new DataColumnChangeEventArgs(this);
		}
		bool flag = this.BeginEditInternal();
		for (int i = 0; i < value.Length; i++)
		{
			if (value[i] != null)
			{
				DataColumn dataColumn = this._columns[i];
				if (-1L != this.rowID && dataColumn.ReadOnly)
				{
					throw ExceptionBuilder.ReadOnly(dataColumn.ColumnName);
				}
				if (dataColumnChangeEventArgs != null)
				{
					dataColumnChangeEventArgs.InitializeColumnChangeEvent(dataColumn, value[i]);
					this._table.OnColumnChanging(dataColumnChangeEventArgs);
				}
				if (dataColumn.Table != this._table)
				{
					throw ExceptionBuilder.ColumnNotInTheTable(dataColumn.ColumnName, this._table.TableName);
				}
				if (-1L != this.rowID && dataColumn.ReadOnly)
				{
					throw ExceptionBuilder.ReadOnly(dataColumn.ColumnName);
				}
				if (this.tempRecord == -1)
				{
					this.BeginEditInternal();
				}
				object obj = (dataColumnChangeEventArgs != null) ? dataColumnChangeEventArgs.ProposedValue : value[i];
				if (obj == null)
				{
					if (dataColumn.IsValueType)
					{
						throw ExceptionBuilder.CannotSetToNull(dataColumn);
					}
					obj = DBNull.Value;
				}
				try
				{
					int proposedRecordNo = this.GetProposedRecordNo();
					dataColumn[proposedRecordNo] = obj;
				}
				catch (Exception e)
				{
					if (ADP.IsCatchableOrSecurityExceptionType(e) && flag)
					{
						this.CancelEdit();
					}
					throw;
				}
				this.LastChangedColumn = dataColumn;
				if (dataColumnChangeEventArgs != null)
				{
					this._table.OnColumnChanged(dataColumnChangeEventArgs);
				}
			}
		}
		this.EndEdit();
	}
}

以上就是C#  DataRow.ItemArray 属性的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement
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
C# .NET: Building Applications with the .NET EcosystemC# .NET: Building Applications with the .NET EcosystemApr 27, 2025 am 12:12 AM

How to build applications using .NET? Building applications using .NET can be achieved through the following steps: 1) Understand the basics of .NET, including C# language and cross-platform development support; 2) Learn core concepts such as components and working principles of the .NET ecosystem; 3) Master basic and advanced usage, from simple console applications to complex WebAPIs and database operations; 4) Be familiar with common errors and debugging techniques, such as configuration and database connection issues; 5) Application performance optimization and best practices, such as asynchronous programming and caching.

C# as a Versatile .NET Language: Applications and ExamplesC# as a Versatile .NET Language: Applications and ExamplesApr 26, 2025 am 12:26 AM

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# .NET for Web, Desktop, and Mobile DevelopmentC# .NET for Web, Desktop, and Mobile DevelopmentApr 25, 2025 am 12:01 AM

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

C# .NET Ecosystem: Frameworks, Libraries, and ToolsC# .NET Ecosystem: Frameworks, Libraries, and ToolsApr 24, 2025 am 12:02 AM

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

Deploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideDeploying C# .NET Applications to Azure/AWS: A Step-by-Step GuideApr 23, 2025 am 12:06 AM

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

C# .NET: An Introduction to the Powerful Programming LanguageC# .NET: An Introduction to the Powerful Programming LanguageApr 22, 2025 am 12:04 AM

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NET Framework vs. C#: Decoding the Terminology.NET Framework vs. C#: Decoding the TerminologyApr 21, 2025 am 12:05 AM

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

Demystifying C# .NET: An Overview for BeginnersDemystifying C# .NET: An Overview for BeginnersApr 20, 2025 am 12:11 AM

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

See all articles

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools