Order of procedures to add a table
-
So, I've got an open connection to my DB, I've got a dataSet, I've made my table and added it to the dataSet, I've got a SqlDataAdapter, constructed with a SELECT command string. I would like to use the SqlCommandBuilder to get the rest of the SQL commands in the DataAdapter, but I can't seem to find the correct order to do everything. I need to add the table to the dataset, initialize the adapter for the table, build the other commands for the adapter, and finally save everything back to the DB on disk. Below is my code, and I'm sure hoping someone can point me in the right direction. I know it seems long, but there are lots of comments and extra lines. I really need help on this, been working all hours for 2 days trying to figure this out.
private void CheckDDBTables() { DataTable table; DataColumn column; DirectoryInfo dir = new DirectoryInfo( Application.StartupPath + "\\forms\\" ); bool needNewtable = true; CustomFormat frm = new CustomFormat(); SqlDataAdapter tempDA; SqlCommandBuilder builder; string selStr = ""; // SELECT "column_name" FROM "table_name" DBConn.Open(); // a global SqlConnection, intialized with the correct connection string // loop through all files in the directory foreach( FileInfo fli in dir.GetFiles( "*.*" ) ) { // for every form file, make a table, to make the DataAdapter // load the file into the form, function also inits LoadFile( "\\forms\\" + fli.Name, out frm ); // init the selStr selStr = "SELECT "; // make a new table for this file table = new DataTable( frm.name.Substring( 0, frm.name.Length - 4 ) ); // identity index column column = new DataColumn( "Listing", typeof( int ) ); column.AllowDBNull = false; column.AutoIncrement = true; column.Unique = true; column.AutoIncrementSeed = 1000; table.Columns.Add( column ); table.PrimaryKey = new DataColumn[] { table.Columns[ 0 ] }; // add column to selStr selStr += column.ColumnName + ","; // begin adding columns, for each item in frm foreach( Item gc in frm.ItemList ) { // new column column = new DataColumn( gc.name, typeof( string ) ); // add the column to the table table.Columns.Add( column ); // add column to selStr selStr += column.ColumnName + ","; } // en