I have a string array in a C# program that I need to load into a temp table in SQL Server. I am currently using a foreach loop to run the insert query. When the SQL connection closes, the temp table disappears so I end up with a table that only has 1 row when there are hundreds in the array.
I have tried adding
using (SqlConnection sqlconnection2 = new SqlConnection())
statement at the top of the method (removing the existing connection line, inside the If(ConnSucceeds)
statement, just inside the try block, and inside the foreach loop. when its inside the foreach loop i have the same issue. Putting it anywhere else I get an error stating that the connection isn't open.
Ultimately I will need to add at least one more foreach loop to run another SQL query to manipulate the data and then find some way to export it to a text file all using the same connection.
private void ImportToTempTable()
{
this.GetProgramInfoForSQLQuery();
this.GetInstallFolder();
string config = this._InstallFolder + @"\" + this._Version + @"\" + this._brandName + @".exe.config";
GetInstanceName(config);
string connStr = "<proprietary conn string parameters>";
bool ConnSucceeds = false;
SqlConnection sqlConnection = new SqlConnection();
StringBuilder errorMessages = new StringBuilder();
if (!ConnSucceeds)
{
try
{
sqlConnection.ConnectionString = connStr;
sqlConnection.Open();
this.WriteNote("SQL Connection Succeeded");
this.WriteNote("");
ConnSucceeds = true;
}
catch (Exception ex)
{
ProjectData.SetProjectError(ex);
int num = (int)Interaction.MsgBox((object)(@"Unable to connect to SQL Server:" + sqlConnection.ConnectionString + @"
Does the " + this._brandName + " Database Live on this Machine?"), MsgBoxStyle.Exclamation, (object)"SQL Connection Error");
ProjectData.ClearProjectError();
}
}
if (ConnSucceeds)
{
string filename = @"C:\Program Folder\DC_Imports\dc_raw.txt";
try
{
StreamReader s = new StreamReader(filename);
string fileContents = s.ReadToEnd();
int removeHeader = fileContents.IndexOf('\n');
string contentsNoHeader = fileContents.Substring(removeHeader);
string contentsFixed = contentsNoHeader.Replace("'", "''");
string delim = "\n";
string[] Rows = contentsFixed.Split(delim.ToCharArray());
foreach (string row in Rows)
{
string query = @"USE DBName IF (NOT EXISTS (SELECT * FROM tempdb.sys.tables WHERE name LIKE '%#DCImportTable%'))
BEGIN
CREATE TABLE #DCImportTable (Main varchar (8000));
INSERT INTO #DCImportTable (Main) VALUES ('" + row + @"');
END
ELSE
INSERT INTO #DCImportTable (Main) VALUES ('" + row + "');";
SqlCommand command = new SqlCommand(query, sqlConnection);
command.ExecuteNonQuery();
this.WriteNote(row);
}
this.WriteNote("Check Table");
this.WriteNote("");
}
catch (SqlException ex)
{
for (int i = 0; i < ex.Errors.Count; i++)
{
errorMessages.Append("Error \n" +
"Message: " + ex.Errors[i].Message + "\n");
}
this.WriteNote(errorMessages.ToString());
sqlConnection.Close();
this.WriteNote("SQL Connection Terminated");
}
}
else
{
this.WriteNote("SQL Login Incorrect");
sqlConnection.Close();
this.WriteNote("SQL Connection Terminated");
}
}
Any help would be greatly appreciated! This is probably the most complex thing I've ever tried to code and I have to use temp tables to do it.