By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,780 Members | 1,523 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,780 IT Pros & Developers. It's quick & easy.

problem creating a sql table

P: n/a
Hi,

I am getting a list text files and readig each of them and breaking down
each file string into a number of varibles. I want to create each table, if
it does not exist and add or update the data each time the program is ran in
an existing database.

The following code runs fine, except dsManager.Tables.a.Add( dt ) on the
second pass.

private void sql_update( string file_name, string var_type, int
core_items_count, string[] core_items )
{
// conn and reader declared outside try
// block for visibility in finally block
SqlConnection conn = null;
SqlDataReader reader = null;

try
{
// instantiate and open connection
conn = new
SqlConnection(
"Server=server;Database=testing;User=excalibur;Pwd =test" );
conn.Open();
}
finally
{
// close reader
if (reader != null)
{
reader.Close();
}

// close connection
if (conn != null)
{
conn.Close();
}
}
// check for table, create and build table if it does not exist -->
ex: passpc_form; name[ 1 ]_form

// catch error if database does not exist, create it!!!

DataTable dt = new DataTable( file_name );
DataColumn dc;

// add "var_type" Column
dc = dt.Columns.Add( "var_type", typeof( String ) );
dc.AllowDBNull = true;

// add "var_name" Column
dc = dt.Columns.Add( "var_name", typeof( String ) );
dc.AllowDBNull = true;

// Add "length" Column
dc = dt.Columns.Add( "length", typeof( int ) );
dc.AllowDBNull = true;

// Add "x" Column
dc = dt.Columns.Add( "x", typeof( int ) );
dc.AllowDBNull = true;

// Add "y" Column
dc = dt.Columns.Add( "y", typeof( int ) );
dc.AllowDBNull = true;

// if( dsManager.Tables.Count == 0 )
// why does "if( dsManager != null )" FAILS on the second pass?
if( dsManager != null )
{
dsManager.Tables.a.Add( dt );
}
// add data to table
switch( core_items_count )
{
case 1: dt.Rows.Add( new Object[] { core_items[ 0 ].Trim(), null,
null, null, null } );
break;
case 2: dt.Rows.Add( new Object[] { var_type, null, null,
core_items[ 0 ], core_items[ 1 ] } );
break;
case 3: dt.Rows.Add( new Object[] { var_type, null, core_items[ 0 ],
core_items[ 1 ], core_items[ 2 ] } );
break;
case 4: dt.Rows.Add( new Object[] { var_type, core_items[ 0 ],
core_items[ 1 ], core_items[ 2 ], core_items[ 3 ] } );
break;
}
}

What am i missing or doing wrong?

Thanks,

Jerry
Jan 30 '07 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.