Hello,
I have been struggling trying to update the schema of a database. All I am trying to do is set the primary key of a table. However, I will set the column, but it does not set it. When I open the DB in the viewer the PK has not been set. I am using the following code. Is there a special way I need to send the update sice I'm dealing with the schema and not just data? I am developing in VS 2008/Win7 x64 Ultimate/.Net 3.5 SP1/C# -- The database is MS Sql (*.mdf) 2005.
I ran a few tests and I am able to add rows to my table successfully, so I am wondering, coudl there possibly be something with my connection string that is preventing me from updating the schema?
string strCon = @"Data Source=.\SQLEXP RESS;AttachDbFi lename=""" + DBPathAndName + @""";Integra ted Security=True;C onnect Timeout=30;User Instance=True";
DataTable table = new DataTable();
SqlDataAdapter dataAdapter = new SqlDataAdapter( "SELECT * FROM ROOM", strCon);
SqlCommandBuild er commandBuilder = new SqlCommandBuild er(dataAdapter) ;
table.Locale = System.Globaliz ation.CultureIn fo.InvariantCul ture;
dataAdapter.Fil lSchema(table, SchemaType.Sour ce);
dataAdapter.Fil l(table);
try
{
DataColumn[] cols = { table.Columns["ROOM_ID"] };
table.PrimaryKe y = cols;
dataAdapter.Upd ate(table);
dataAdapter.Dis pose();
commandBuilder. Dispose();
}
catch(Exception ex)
{
Console.WriteLi ne(ex.ToString( ));
}
I have been struggling trying to update the schema of a database. All I am trying to do is set the primary key of a table. However, I will set the column, but it does not set it. When I open the DB in the viewer the PK has not been set. I am using the following code. Is there a special way I need to send the update sice I'm dealing with the schema and not just data? I am developing in VS 2008/Win7 x64 Ultimate/.Net 3.5 SP1/C# -- The database is MS Sql (*.mdf) 2005.
I ran a few tests and I am able to add rows to my table successfully, so I am wondering, coudl there possibly be something with my connection string that is preventing me from updating the schema?
string strCon = @"Data Source=.\SQLEXP RESS;AttachDbFi lename=""" + DBPathAndName + @""";Integra ted Security=True;C onnect Timeout=30;User Instance=True";
DataTable table = new DataTable();
SqlDataAdapter dataAdapter = new SqlDataAdapter( "SELECT * FROM ROOM", strCon);
SqlCommandBuild er commandBuilder = new SqlCommandBuild er(dataAdapter) ;
table.Locale = System.Globaliz ation.CultureIn fo.InvariantCul ture;
dataAdapter.Fil lSchema(table, SchemaType.Sour ce);
dataAdapter.Fil l(table);
try
{
DataColumn[] cols = { table.Columns["ROOM_ID"] };
table.PrimaryKe y = cols;
dataAdapter.Upd ate(table);
dataAdapter.Dis pose();
commandBuilder. Dispose();
}
catch(Exception ex)
{
Console.WriteLi ne(ex.ToString( ));
}