using System;
using System.Data;
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable("Table-1");
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Field1", typeof(string)).MaxLength = 50;
UniqueConstraint uc1 = new UniqueConstraint("UniqueConstraint", dt.Columns["Field1"]);
dt.Constraints.Add(uc1);
try
{
AddRow(dt, 1, "Value 1");
AddRow(dt, 2, "Value 1");
AddRow(dt, 3, "Value 1");
}
catch (Exception ex)
{
Console.WriteLine("Error: {0}", ex.Message);
}
}
private static void AddRow(DataTable dt, int id, string field1)
{
Console.WriteLine("\nAdding row: {0}, {1}", id, field1);
dt.Rows.Add(new object[] { id, field1 });
Console.WriteLine("Row added.");
}
}
|