首页 > 代码库 > RowState和DataRowVersion使用示例

RowState和DataRowVersion使用示例

using System;using System.Data;namespace TestApp{    class Program    {        static void Main(string[] args)        {            DataTable cars = new DataTable("Cars");            DataColumn vin = new DataColumn("Vin");            vin.DataType = typeof(string);            vin.AllowDBNull = false;            vin.Caption = "VIN";            cars.Columns.Add(vin);            DataColumn manufactor = new DataColumn("Manufactor");            manufactor.DataType = typeof(string);            manufactor.MaxLength = 100;            manufactor.AllowDBNull = true;            cars.Columns.Add(manufactor);            DataRow dr = cars.NewRow();            dr[0] = "123456789ABCDE";            dr[1] = "VW";            cars.Rows.Add(dr);            Console.WriteLine(GetDataRowInfo(dr, "Vin"));                        cars.AcceptChanges();            dr.BeginEdit();            dr[0] = "987654321ABCDE";            dr[1] = "NISSAN";            Console.WriteLine(GetDataRowInfo(dr, "Vin"));            dr.EndEdit();            Console.WriteLine(GetDataRowInfo(dr, "Vin"));        }         static string GetDataRowInfo(DataRow row, string columnName)        {            string retVal = string.Format("RowState: {0} \t\n", row.RowState);            foreach (string versionString in Enum.GetNames(typeof(DataRowVersion)))            {                DataRowVersion version = (DataRowVersion)Enum.Parse(typeof(DataRowVersion), versionString);                if (row.HasVersion(version))                    retVal += string.Format("Version: {0} Value: {1} \r\n", version, row[columnName, version]);                else                    retVal += string.Format("Version: {0} does not exist. \r\n", version);            }            return retVal;        }    }}

运行结果:

RowState: Added
Version: Original does not exist.
Version: Current Value: 123456789ABCDE
Version: Proposed does not exist.
Version: Default Value: 123456789ABCDE

RowState: Unchanged
Version: Original Value: 123456789ABCDE
Version: Current Value: 123456789ABCDE
Version: Proposed Value: 987654321ABCDE
Version: Default Value: 987654321ABCDE

RowState: Modified
Version: Original Value: 123456789ABCDE
Version: Current Value: 987654321ABCDE
Version: Proposed does not exist.
Version: Default Value: 987654321ABCDE

RowState和DataRowVersion使用示例