auto incr

  • Thread starter Thread starter frazer
  • Start date Start date
Yes, whichever DataColumn in whatever DataTable, set it's AutoIncrement
Property to true. Then there's a AutoIncrementStep value which sets the
amount that each increment will be and theAutoIncrementSeed which is the
starting point. To Avoid colisions if this is a multiuser app, set the Step
to -1 and the seed to 0 that way the RDBMS will always automatically choose
an available value and you won't have any collisions.

HTH,

Bill

--
W.G. Ryan MVP Windows - Embedded

http://forums.devbuzz.com
http://www.knowdotnet.com/dataaccess.html
http://www.msmvps.com/williamryan/
 
frazer said:
i have created a dataset manually.
is ther any way to have auto increment in it?

Hi frazer,

look at the following code:

private void button1_Click(object sender, System.EventArgs e)
{
//create a new DataSet
DataSet ds = new DataSet();
//add a table
DataTable dt = ds.Tables.Add();
//add a column
DataColumn dc = dt.Columns.Add("ID");
//this shall be an autoincrement column
dc.AutoIncrement = true;
dc.AutoIncrementSeed = 1;
dc.AutoIncrementStep = 1;

//another column
DataColumn dc2 = dt.Columns.Add("test");
dc2.DataType = typeof(string);
//fill the table
for (int i = 0; i < 10; i++)
{
DataRow dr = dt.NewRow();
dr["test"] = "test" + i;
dt.Rows.Add(dr);
}

//show autogenerated ids
foreach (DataRow dr in dt.Rows)
{
System.Diagnostics.Debug.WriteLine("ID: " + dr["ID"].ToString() +
", test: " + dr["test"].ToString());
}
}

Cheers

Arne Janning
 
Back
Top