Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. using (SqlConnection sqlconnection2 = new SqlConnection())
  2.  
  3. private void ImportToTempTable()
  4. {
  5. this.GetProgramInfoForSQLQuery();
  6. this.GetInstallFolder();
  7. string config = this._InstallFolder + @"" + this._Version + @"" + this._brandName + @".exe.config";
  8. GetInstanceName(config);
  9.  
  10. string connStr = "<proprietary conn string parameters>";
  11. bool ConnSucceeds = false;
  12.  
  13. SqlConnection sqlConnection = new SqlConnection();
  14. StringBuilder errorMessages = new StringBuilder();
  15.  
  16. if (!ConnSucceeds)
  17. {
  18. try
  19. {
  20. sqlConnection.ConnectionString = connStr;
  21. sqlConnection.Open();
  22. this.WriteNote("SQL Connection Succeeded");
  23. this.WriteNote("");
  24. ConnSucceeds = true;
  25. }
  26. catch (Exception ex)
  27. {
  28. ProjectData.SetProjectError(ex);
  29. int num = (int)Interaction.MsgBox((object)(@"Unable to connect to SQL Server:" + sqlConnection.ConnectionString + @"
  30. Does the " + this._brandName + " Database Live on this Machine?"), MsgBoxStyle.Exclamation, (object)"SQL Connection Error");
  31. ProjectData.ClearProjectError();
  32. }
  33. }
  34.  
  35. if (ConnSucceeds)
  36. {
  37. string filename = @"C:Program FolderDC_Importsdc_raw.txt";
  38.  
  39. try
  40. {
  41. StreamReader s = new StreamReader(filename);
  42. string fileContents = s.ReadToEnd();
  43. int removeHeader = fileContents.IndexOf('n');
  44. string contentsNoHeader = fileContents.Substring(removeHeader);
  45. string contentsFixed = contentsNoHeader.Replace("'", "''");
  46. string delim = "n";
  47. string[] Rows = contentsFixed.Split(delim.ToCharArray());
  48.  
  49. foreach (string row in Rows)
  50. {
  51. string query = @"USE DBName IF (NOT EXISTS (SELECT * FROM tempdb.sys.tables WHERE name LIKE '%#DCImportTable%'))
  52. BEGIN
  53. CREATE TABLE #DCImportTable (Main varchar (8000));
  54. INSERT INTO #DCImportTable (Main) VALUES ('" + row + @"');
  55. END
  56. ELSE
  57. INSERT INTO #DCImportTable (Main) VALUES ('" + row + "');";
  58.  
  59. SqlCommand command = new SqlCommand(query, sqlConnection);
  60. command.ExecuteNonQuery();
  61. this.WriteNote(row);
  62. }
  63.  
  64. this.WriteNote("Check Table");
  65. this.WriteNote("");
  66. }
  67. catch (SqlException ex)
  68. {
  69. for (int i = 0; i < ex.Errors.Count; i++)
  70. {
  71. errorMessages.Append("Error n" +
  72. "Message: " + ex.Errors[i].Message + "n");
  73. }
  74.  
  75. this.WriteNote(errorMessages.ToString());
  76. sqlConnection.Close();
  77. this.WriteNote("SQL Connection Terminated");
  78. }
  79. }
  80. else
  81. {
  82. this.WriteNote("SQL Login Incorrect");
  83. sqlConnection.Close();
  84. this.WriteNote("SQL Connection Terminated");
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement