J
Joe
I have a function which takes a table and calls DA.Update with that table. I
can't seem to get my new ids returned back to me.
My second problem is I don't like using the select * for the SelectCommand.
I was thinking of adding where '1' = '2' but think that will definitly not
return any records back to me.
public void UpdateTable(DataTable dt)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = sqlConnection1.CreateCommand();
SqlCommandBuilder cb = new SqlCommandBuilder();
cmd.CommandText = string.Format("select * from {0}", dt.TableName);
da.SelectCommand = cmd;
cb.DataAdapter = da;
DataColumn dc = null;
foreach (DataColumn c in dt.Columns)
{
if (c.AutoIncrement == true)
{
dc = c;
break;
}
}
da.InsertCommand = cb.GetInsertCommand();
if (dc != null)
da.InsertCommand.CommandText += " SELECT SCOPE_IDENTITY() As " +
dc.ColumnName;
da.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;
da.UpdateCommand = cb.GetUpdateCommand();
da.DeleteCommand = cb.GetDeleteCommand();
da.Update(dt);
}
can't seem to get my new ids returned back to me.
My second problem is I don't like using the select * for the SelectCommand.
I was thinking of adding where '1' = '2' but think that will definitly not
return any records back to me.
public void UpdateTable(DataTable dt)
{
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand cmd = sqlConnection1.CreateCommand();
SqlCommandBuilder cb = new SqlCommandBuilder();
cmd.CommandText = string.Format("select * from {0}", dt.TableName);
da.SelectCommand = cmd;
cb.DataAdapter = da;
DataColumn dc = null;
foreach (DataColumn c in dt.Columns)
{
if (c.AutoIncrement == true)
{
dc = c;
break;
}
}
da.InsertCommand = cb.GetInsertCommand();
if (dc != null)
da.InsertCommand.CommandText += " SELECT SCOPE_IDENTITY() As " +
dc.ColumnName;
da.InsertCommand.UpdatedRowSource = UpdateRowSource.Both;
da.UpdateCommand = cb.GetUpdateCommand();
da.DeleteCommand = cb.GetDeleteCommand();
da.Update(dt);
}