Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>this worked</p> <pre><code>void ConvertCsv(string sourcePath, string ResultPath) { #region using (StreamReader sr = new StreamReader(sourcePath)) { using (StreamWriter sw = new StreamWriter(ResultPath)) { sw.WriteLine(@"DECLARE @er NVARCHAR(MAX)='',@i INT=0 BEGIN TRY"); if (sr.Peek() &gt;= 0) { string currentLine = sr.ReadLine(); while (currentLine.Trim() == "" &amp;&amp; sr.Peek() &gt;= 0) currentLine = sr.ReadLine(); if (currentLine.Trim() == "") { //error :the file is empty } sw.WriteLine(currentLine); } while (sr.Peek() &gt;= 0) { string currentLine = sr.ReadLine(); if (currentLine.Trim() == "") continue; if (currentLine.Trim().StartsWith("INSERT")) { while (!currentLine.Trim().StartsWith("INSERT")) currentLine += sr.ReadLine(); currentLine = @"END TRY BEGIN CATCH SELECT @er+=','+LTRIM(RTRIM(STR(ERROR_LINE()))),@i+=1 END CATCH BEGIN TRY" + Environment.NewLine + currentLine.Trim(); } sw.WriteLine(currentLine.Trim()); } sw.WriteLine(@"END TRY BEGIN CATCH SELECT @er+=','+LTRIM(RTRIM(STR(ERROR_LINE()))) END CATCH SELECT @er AS errorLines,@i AS errorCount"); sw.Close(); sr.Close(); } } #endregion ExecuteConvertedFile(ResultPath,Server.MapPath(@"~/Data/Uploads/csv/ErrorLogs/ErrorLog.sql")); } void ExecuteConvertedFile(string ResultPath, string errorResultPath) { string wholeQuery = System.IO.File.ReadAllText(ResultPath); using (SqlCommand cmd = new SqlCommand { CommandType = CommandType.Text, CommandText = wholeQuery, Connection = new SqlConnection(((SqlConnection)((EntityConnection)new NezaratEntities().Connection).StoreConnection).ConnectionString) }) { cmd.Connection.Open(); var dr = cmd.ExecuteReader(); dr.Read(); WriteErrorLogs(ResultPath,errorResultPath,dr["errorLines"].ToString().Trim()); Label1.Text = dr["errorCount"].ToString()+"unsuccessful transactions"; } } </code></pre> <p>to save unsuccessful sql commands:</p> <pre><code>void WriteErrorLogs(string sourcePath, string ResultPath,string errorLinesTolog) { string[] lines = File.ReadAllLines(sourcePath); string ErrorLog=""; errorLinesTolog=errorLinesTolog.Remove(0, 1); foreach (var line in errorLinesTolog.Split(',')) { ErrorLog += lines[int.Parse(line)-1] + Environment.NewLine; } System.IO.File.WriteAllText(ResultPath, ErrorLog); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload