Storing Array Of String in Queue in C# AsynC - c#

enter image description hereI am reading from MySql DB using C# but because I am doing some fata processing for the data which is coming out from DB so I had to store these data in order using Queue<> Class.
I am trying to store the data as ( String[] arr ) format in the Queue
but I found the Queue memory is ecpanding per each time storing data but the last group of array data will be repeated in side queue memory which I believe I am doing something wrong.
I believe it is clear in the attached image.
Notices:
I am calling Queue.Enqueue(arr) method inside another Async method which I believe it is very problematic way as in below
`
public async void DBReadAsync(Calculations.RetStartEndDates f, DataBufferCls.DataBuffer dt)
{
int i;
Task t1 = Task.Factory.StartNew(() =>
{
DB.ReadFromDB3(f, dt);
}
) ;
await t1;
}
public string[] ReadFromDB3(RetStartEndDates WeekStrtEnd, DataBuffer dt)
{
string s;
string[] arr = new string[8];
string fromtime = WeekStrtEnd.StartTime_Str;/// "8:40:30";"10:09:00"; //
string FromDate = WeekStrtEnd.StartDateDBFormate_Str; //"2022-10-14 "; //"2022-11-06 "; //
string FromTD = FromDate + " " + fromtime;
string Totime = WeekStrtEnd.EndTime_Str; //"11:19:22"; //
string ToDate = WeekStrtEnd.EndDateDBFormate_Str; //"2022-11-07 "; //
string ToTD = ToDate + " " + Totime;
Console.WriteLine($"Start date is {FromTD}");
Console.WriteLine($"End Date is {ToTD}");
string query = "SELECT * FROM data_46";
//Open connection
if (this.IsConnect())
{
StartReadingFromDB_Event("Hold");
dt.Monitortest();
//Create Command
MySqlCommand cmd = new MySqlCommand(query, Connection);
//Create a data reader and Execute the command
MySqlDataReader dataReader = cmd.ExecuteReader();
//Read the data and store them in the list
while (dataReader.Read())
{
arr[0] = dataReader[0].ToString();
arr[1] = dataReader[1].ToString();
arr[2] = dataReader[2].ToString();
arr[3] = dataReader[3].ToString();
arr[4] = dataReader[4].ToString();
arr[5] = dataReader[5].ToString();
arr[6] = dataReader[6].ToString();
arr[7] = dataReader[7].ToString();
dt.WrToBuffer3(arr);
Thread.Sleep(10);
}[enter image description here](https://i.stack.imgur.com/aAJnB.png)`
`
`
I am trying to understand the reason for that

Related

c# Base64 in email attachment

I know that i'm stuck in a silly problem...and now i am here with hope, that you can help me. Simple situation: I need to send email with attachment from database. I have done it by myself, but...i don't know why, but attachment in email what was send is incorrect. Preciously attachment contents code, what i get from my database.
My code:
static void IncomeMessage()
{
SqlConnection conn = new SqlConnection("Data Source=xxx;Initial Catalog=xxx;User ID=xxx;Password=xxx");
SqlCommand cmd = new SqlCommand("Here is my SQL SELECT", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
foreach (DataRow dr in dt.Rows)
{
try
{
var AID = dr["ID"].ToString();
var Ids = dr["MessageID"].ToString();
var SuperOrganizationTitle = dr["SuperOrganizationTitle"].ToString();
var Date = dr["Date"].ToString();
var Title = dr["Title"].ToString();
var Description = dr["Description"].ToString();
var DBFile = dr["File"].ToString();
var Filename = dr["FileName"].ToString();
MailMessage oMail = new MailMessage("test#test.com", "guntisvindels#gmail.com", "New message with theme: " + Title, "Message Income: " + Date + " From " + SuperOrganizationTitle + ". Message Content: " + Description);
DBFile = GetBase64(DBFile);
var bytes = Encoding.ASCII.GetBytes(DBFile);
MemoryStream strm = new MemoryStream(bytes);
Attachment data = new Attachment(strm, Filename);
ContentDisposition disposition = data.ContentDisposition;
data.ContentId = Filename;
data.ContentDisposition.Inline = true;
oMail.Attachments.Add(data);
SmtpClient oSmtp = new SmtpClient();
SmtpClient oServer = new SmtpClient("test.test.com");
oServer.Send(oMail);
}
catch (Exception ex)
{
//TraceAdd("Error=" + ex.Message.ToString());
}
}
}
For removing unnessessary data ( tags) from database File field i use this code
public static string GetBase64(string str)
{
var index1 = str.IndexOf("<content>");
index1 = index1 + "<content>".Length;
var index2 = str.IndexOf("</content>");
var kek = Slice(str, index1, index2);
return kek;
}
public static string Slice(string source, int start, int end)
{
if (end < 0)
{
end = source.Length + end;
}
int len = end - start;
return source.Substring(start, len);
}
When launching my code I can send email to me, and I'll recieve it. But message content attachment with my Base64 code from database File field. Here is a question - Where I got wrong, and how can I recieve email with correct attachment that has been written into database. Thank you for your attention
P.S Attachment in database is written correctly, because K2 integration platform can correctly display it
Here is example what database contain in File column (DBFile)
<file><name>zinojums.html</name><content>PGh0bWw+PGhlYWQ+PG1ldGEgaHR0cC1lcXVpdj0iQ29udGVudC1UeXBlIiBjb250ZW50PSJ0ZXh0L2h0bWw7IGNoYXJzZXQ9dXRmLTgiIC8+PHN0eWxlPmh0bWwsYm9keXtmb250LWZhbWlseTpBcmlhbCxIZWx2ZXRpY2Esc2Fucy1zZXJpZjtmb250LXNpemU6MTBwdDt9aDF7Zm9udC1zaXplOjE0cHQ7fWgye2ZvbnQtc2l6ZToxMnB0O31we2JhY2tncm91bmQtY29sb3I6I0Y3RUVEQTttYXJnaW46MDtwYWRkaW5nOjhweDt9aHJ7aGVpZ2h0OjFweDsgYm9yZGVyOjAgbm9uZTsgY29sb3I6ICM1RjlCRDQ7IGJhY2tncm91bmQtY29sb3I6ICM1RjlCRDQ7fTwvc3R5bGU+PC9oZWFkPjxib2R5PjxoMT5JbmZvcm3EgWNpamEgbm8gVmFsc3RzIGllxYbEk211bXUgZGllbmVzdGE8L2gxPjxoMj5Eb2t1bWVudHMgcGllxYZlbXRzPC9oMj48cD5Ob2Rva8S8dSBtYWtzxIF0xIFqYSBOci4gPGI+TEFUVklKQVMgVkFMU1RTIFJBRElPIFVOIFRFTEVWxKpaSUpBUyBDRU5UUlMgQVMgKDQwMDAzMDExMjAzKTwvYj4gaWVzbmllZ3RhaXMgZG9rdW1lbnRzICI8Yj5aacWGYXMgcGFyIGRhcmJhIMWGxJNtxJNqaWVtPC9iPiIgTnIuIDxiPjU1NDYyNTY5PC9iPiBwaWXFhmVtdHMgdW4gaWVrxLxhdXRzIFZJRCBkYXR1YsSBesSTLjxiciAvPjwvcD48IS0tY29udGVudC0tPjxociAvPsWgaXMgZS1wYXN0cyBpciBpenZlaWRvdHMgYXV0b23EgXRpc2tpLCBsxatkemFtIHV6IHRvIG5lYXRiaWxkxJN0LjxiciAvPlBpZXNsxJNndGllcyBWSUQgRWxla3Ryb25pc2vEgXMgZGVrbGFyxJPFoWFuYXMgc2lzdMSTbWFpOiA8YSBocmVmPSJodHRwczovL2Vkcy52aWQuZ292Lmx2Ij5lZHMudmlkLmdvdi5sdjwvYT4uPC9ib2R5PjwvaHRtbD4=</content></file>
Right now the method you're using to create binary data from your string takes no account of the fact the string is base64-encoded specifically.
You need to replace
var bytes = Encoding.ASCII.GetBytes(DBFile);
with
var bytes = Convert.FromBase64String(DBFile);

I am trying to add data from word doc to access database but it give me operator missing syntax

I am reading word file and add data after it find Favour. Just wont to add data after favour. First record using this code added perfect record but second time it give Syntax error(operator missing). Please help me to correctly add all records
private void button1_Click(object sender, EventArgs e)
{
try
{
Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
object nullobj = System.Reflection.Missing.Value;
object file = openFileDialog1.FileName;
Document doc = app.Documents.Open(#"C:\Users\juilee Raut\Downloads\ITCL-CAES 1 (1).docx");
doc.ActiveWindow.Selection.WholeStory();
doc.ActiveWindow.Selection.Copy();
IDataObject da = Clipboard.GetDataObject();
string text = da.GetData(DataFormats.Text).ToString();
richTextBox1.Text = text;
string data = string.Empty;
string[] data1 = richTextBox1.Lines;
List<string> Info = new List<string>();
int i = 0;
int j = 0;
int m = 0;
while (i < data1.Length)
{
if (data1[i].StartsWith("FAVOUR:"))
{
j++;
if (m == 0)
{
data = data + data1[i].ToString() + Environment.NewLine;
string inf = string.Join(Environment.NewLine, Info.ToArray());
con.Open();
OleDbCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT into AllData(FullJud) VALUES('" + inf + "')";
cmd.ExecuteNonQuery();
con.Close();
Info.Clear();
inf = string.Empty;
m = 1;
}
}
else
{
m = 0;
if (data1[i] != "")
{
if (data1[i].EndsWith("2017") && data1[i].Length == 10 || data1[i].EndsWith("2016") && data1[i].Length == 10)
{
data = data + data1[i].ToString() + Environment.NewLine + "##ln##" + Environment.NewLine;
Info.Add(data1[i]);
Info.Add("##ln##");
}
else if(data1[i].StartsWith("SECTION:") || data1[i].StartsWith("Section:") || data1[i].StartsWith("SECTION-") || data1[i].Contains("SUBJECT:") || data1[i].StartsWith("Subject:") || data1[i].StartsWith("SUBJECT-") || data1[i].StartsWith("SUBJECTS:"))
{
data = data + data1[i].ToString() + Environment.NewLine;
}
else if(data1[i].EndsWith("Respondent.") || data1[i].EndsWith("Petitioner.") || data1[i].EndsWith("Appellant.") || data1[i].EndsWith("Appellant") || data1[i].EndsWith("Respondent") || data1[i].EndsWith("Counsel,"))
{
data = data + data1[i].ToString() + Environment.NewLine;
}
else
{
data = data + data1[i].ToString() + Environment.NewLine;
Info.Add(data1[i]);
}
}
}
i++;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This is the error I get:
"Syntax error (missing operator) in query expression ''391 ITR 382 (BOM): 88 TAXMANN.COM 556\r\nHIGH COURT OF BOMBAY \r\nM.S. SANKLECHA AND A.K. MENON, JJ.\r\nMalay N. Sanghvi v/s. Income Tax Officer\r\nIT APPEAL NO. 1342 OF 2014\r\n31.01.2017\r\n##ln##\r\nSection 80-IB of the Income-tax Act, 1961 - Deductions - Profits a'."
What if Info contains several values, let's say:
Info = { "value1", "value2" }
Then, inf would be:
inf = "value1\r\nvalue2"
Therefore, cmd.CommandText would be:
cmd.CommandText = "INSERT into AllData(FullJud) VALUES('value1\r\nvalue2')";
and I'm quite sure this is not the wanted behaviour.
Edit
What if one value in Info contained a ' character?
Info = { "val'ue" }
Then, inf would be:
inf = "val'ue"
Therefore, cmd.CommandText would be:
cmd.CommandText = "INSERT into AllData(FullJud) VALUES('val'ue')";
// SQL won't understand that part --------------------------|||
and that's where you get an error.
Moreover, what if Info had the following value instead:
Info = { "value1');DROP TABLE [anytable];--" }
That's typical SQL Injection.
Some questions/comments:
What is j used for?
What is the purpose of inf = string.Empty;? It is a local variable and will be garbage collected.
What is the purpose of data? Will you even use it at some point?
You are using a while loop when you could be using a for(int i=0;i<data1.Length;i++) loop.
What if data1 contains two consecutive strings starting with "FAVOUR:"? Why would you insert only the first one, and not the second one?
else
{
data.Replace("'", "/");
data1[i] = data1[i].Replace("'","/");
Info.Add(data1[i]);
data = data + data1[i].ToString() + Environment.NewLine;
}
In else part I just replace the ' to / and my problem is solve

Parallel data processing confuses some information

I am trying to develop an application with some parallel data processing and the use of MySQL. Here is a piece of code where I ran into a problem
public ConcurrentDictionary<string, Info> GetDatabaseForCurrentDay(System.DateTime day)
{
string[] date = day.ToShortDateString().Split('.');
string sqlQuery = "SELECT * FROM testtable WHERE Date ='" + date[2] + "-" + date[1] + "-" + date[0] + "';";
ConcurrentDictionary<string, Info> info = new ConcurrentDictionary<string, Info>();
Info[] dayInfo = null;
Parallel.ForEach(ReadData(ConnectionString, sqlQuery), data =>
{
int num = 2;
string[] dataPieces = data.Split(new char[] { ',' }, num);
FileHelpers.FileHelperEngine<Info> engine = new FileHelpers.FileHelperEngine<Info>();
dayInfo = engine.ReadString(dataPieces[1], int.MaxValue);
info.TryAdd(dataPieces[0], dayInfo[0]);
});
return info;
}
Apart from this fragment, the function ReadData(ConnectionString, sqlQuery) is also worth being mentioned, since it provides an argument for the loop Parallel.ForEach.
public IEnumerable<string> ReadData(string connectionString, string queryString)
{
using (MySqlConnection conn = new MySqlConnection(connectionString))
{
using (MySqlCommand comm = new MySqlCommand(queryString, conn))
{
conn.Open();
string command2 = "USE testdatabase;";
MySqlCommand commandUse = new MySqlCommand(command2, conn);
commandUse.ExecuteNonQuery();
comm.CommandTimeout = 0;
MySqlDataReader reader = comm.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
StringBuilder sb = new StringBuilder();
sb.Append(reader.GetString(0) + ",");
sb.Append(reader.GetDateTime(1).ToString("yyyy-MM-dd") + ",");
sb.Append(reader.GetDouble(2).ToString().Replace(',', '.') + ",");
sb.Append(reader.GetDouble(3).ToString().Replace(',', '.') + ",");
sb.Append(reader.GetDouble(4).ToString().Replace(',', '.') + ",");
sb.Append(reader.GetDouble(5).ToString().Replace(',', '.') + ",");
sb.Append(reader.GetUInt64(6) + ",");
sb.Append(reader.GetDouble(7).ToString().Replace(',', '.'));
yield return sb.ToString();
}
}
}
}
}
Now, let us move back to the problem. The code compiles and works, but the results it returns are incorrect. I noticed that ConcurrentDictionarycontains keys with wrong values -- in a nutshell, info.TryAdd(dataPieces[0], dayInfo[0]) may insert a key from one thread and the value from another thread and, therefore, the data may be corrupted. I understand that this behaviour is the setback of the parallel processing, but this method can't be omitted. I tried different ways to fix this problem, but nothing worked, and the data was still wrong. Are there any solutions to this problem which maintain the speed of execution for this code and save the data?
You need to move dayInfointo your parallel for loop. Basically this is a shared variable that keeps getting written over by each of the tasks giving you garbage results. If you put it into the delegate, then it will be a different private variable for each iteration and not get clobbered:
// Info[] dayInfo = null; <--Remove this
Parallel.ForEach(ReadData(ConnectionString, sqlQuery), data =>
{
int num = 2;
string[] dataPieces = data.Split(new char[] { ',' }, num);
FileHelpers.FileHelperEngine<Info> engine = new FileHelpers.FileHelperEngine<Info>();
//declare dayInfo locally within this scope instead
var dayInfo = engine.ReadString(dataPieces[1], int.MaxValue);
info.TryAdd(dataPieces[0], dayInfo[0]);
});

How can I get values from a csv file where some of the cells contain commas?

I have a script that imports a csv file and reads each line to update the corresponding item in Sitecore. It works for many of the products but the problem is for some products where certain cells in the row have commas in them (such as the product description).
protected void SubmitButton_Click(object sender, EventArgs e)
{
if (UpdateFile.PostedFile != null)
{
var file = UpdateFile.PostedFile;
// check if valid csv file
message.InnerText = "Updating...";
Sitecore.Context.SetActiveSite("backedbybayer");
_database = Database.GetDatabase("master");
SitecoreContext context = new SitecoreContext(_database);
Item homeNode = context.GetHomeItem<Item>();
var productsItems =
homeNode.Axes.GetDescendants()
.Where(
child =>
child.TemplateID == new ID(TemplateFactory.FindTemplateId<IProductDetailPageItem>()));
try
{
using (StreamReader sr = new StreamReader(file.InputStream))
{
var firstLine = true;
string currentLine;
var productIdIndex = 0;
var industryIdIndex = 0;
var categoryIdIndex = 0;
var pestIdIndex = 0;
var titleIndex = 0;
string title;
string productId;
string categoryIds;
string industryIds;
while ((currentLine = sr.ReadLine()) != null)
{
var data = currentLine.Split(',').ToList();
if (firstLine)
{
// find index of the important columns
productIdIndex = data.IndexOf("ProductId");
industryIdIndex = data.IndexOf("PrimaryIndustryId");
categoryIdIndex = data.IndexOf("PrimaryCategoryId");
titleIndex = data.IndexOf("Title");
firstLine = false;
continue;
}
title = data[titleIndex];
productId = data[productIdIndex];
categoryIds = data[categoryIdIndex];
industryIds = data[industryIdIndex];
var products = productsItems.Where(x => x.DisplayName == title);
foreach (var product in products)
{
product.Editing.BeginEdit();
try
{
product.Fields["Product Id"].Value = productId;
product.Fields["Product Industry Ids"].Value = industryIds;
product.Fields["Category Ids"].Value = categoryIds;
}
finally
{
product.Editing.EndEdit();
}
}
}
}
// when done
message.InnerText = "Complete";
}
catch (Exception ex)
{
message.InnerText = "Error reading file";
}
}
}
The problem is that when a description field has commas, like "Product is an effective, preventative biofungicide," it gets split as well and throws off the index, so categoryIds = data[8] gets the wrong value.
The spreadsheet is data that is provided by our client, so I would rather not require the client to edit the file unless necessary. Is there a way I can handle this in my code? Is there a different way I can read the file that won't split everything by comma?
I suggest use Ado.Net, If the field's data are inside quotes and it will parse it like a field and ignore any commas inside this..
Code Example:
static DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)
{
string header = isFirstRowHeader ? "Yes" : "No";
string pathOnly = Path.GetDirectoryName(path);
string fileName = Path.GetFileName(path);
string sql = #"SELECT * FROM [" + fileName + "]";
using(OleDbConnection connection = new OleDbConnection(
#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathOnly +
";Extended Properties=\"Text;HDR=" + header + "\""))
using(OleDbCommand command = new OleDbCommand(sql, connection))
using(OleDbDataAdapter adapter = new OleDbDataAdapter(command))
{
DataTable dataTable = new DataTable();
dataTable.Locale = CultureInfo.CurrentCulture;
adapter.Fill(dataTable);
return dataTable;
}
}

import from text file to SQL Server Database, is ADO.NET too slow?

My program is now still running to import data from a log file into a remote SQL Server Database. The log file is about 80MB in size and contains about 470000 lines, with about 25000 lines of data. My program can import only 300 rows/second, which is really bad. :(
public static int ImportData(string strPath)
{
//NameValueCollection collection = ConfigurationManager.AppSettings;
using (TextReader sr = new StreamReader(strPath))
{
sr.ReadLine(); //ignore three first lines of log file
sr.ReadLine();
sr.ReadLine();
string strLine;
var cn = new SqlConnection(ConnectionString);
cn.Open();
while ((strLine = sr.ReadLine()) != null)
{
{
if (strLine.Trim() != "") //if not a blank line, then import into database
{
InsertData(strLine, cn);
_count++;
}
}
}
cn.Close();
sr.Close();
return _count;
}
}
InsertData is just a normal insert method using ADO.NET. It uses a parsing method:
public Data(string strLine)
{
string[] list = strLine.Split(new[] {'\t'});
try
{
Senttime = DateTime.Parse(list[0] + " " + list[1]);
}
catch (Exception)
{
}
Clientip = list[2];
Clienthostname = list[3];
Partnername = list[4];
Serverhostname = list[5];
Serverip = list[6];
Recipientaddress = list[7];
Eventid = Convert.ToInt16(list[8]);
Msgid = list[9];
Priority = Convert.ToInt16(list[10]);
Recipientreportstatus = Convert.ToByte(list[11]);
Totalbytes = Convert.ToInt32(list[12]);
Numberrecipient = Convert.ToInt16(list[13]);
DateTime temp;
if (DateTime.TryParse(list[14], out temp))
{
OriginationTime = temp;
}
else
{
OriginationTime = null;
}
Encryption = list[15];
ServiceVersion = list[16];
LinkedMsgid = list[17];
MessageSubject = list[18];
SenderAddress = list[19];
}
InsertData method:
private static void InsertData(string strLine, SqlConnection cn)
{
var dt = new Data(strLine); //parse the log line into proper fields
const string cnnStr =
"INSERT INTO LOGDATA ([SentTime]," + "[client-ip]," +
"[Client-hostname]," + "[Partner-Name]," + "[Server-hostname]," +
"[server-IP]," + "[Recipient-Address]," + "[Event-ID]," + "[MSGID]," +
"[Priority]," + "[Recipient-Report-Status]," + "[total-bytes]," +
"[Number-Recipients]," + "[Origination-Time]," + "[Encryption]," +
"[service-Version]," + "[Linked-MSGID]," + "[Message-Subject]," +
"[Sender-Address]) " + " VALUES ( " + "#Senttime," + "#Clientip," +
"#Clienthostname," + "#Partnername," + "#Serverhostname," + "#Serverip," +
"#Recipientaddress," + "#Eventid," + "#Msgid," + "#Priority," +
"#Recipientreportstatus," + "#Totalbytes," + "#Numberrecipient," +
"#OriginationTime," + "#Encryption," + "#ServiceVersion," +
"#LinkedMsgid," + "#MessageSubject," + "#SenderAddress)";
var cmd = new SqlCommand(cnnStr, cn) {CommandType = CommandType.Text};
cmd.Parameters.AddWithValue("#Senttime", dt.Senttime);
cmd.Parameters.AddWithValue("#Clientip", dt.Clientip);
cmd.Parameters.AddWithValue("#Clienthostname", dt.Clienthostname);
cmd.Parameters.AddWithValue("#Partnername", dt.Partnername);
cmd.Parameters.AddWithValue("#Serverhostname", dt.Serverhostname);
cmd.Parameters.AddWithValue("#Serverip", dt.Serverip);
cmd.Parameters.AddWithValue("#Recipientaddress", dt.Recipientaddress);
cmd.Parameters.AddWithValue("#Eventid", dt.Eventid);
cmd.Parameters.AddWithValue("#Msgid", dt.Msgid);
cmd.Parameters.AddWithValue("#Priority", dt.Priority);
cmd.Parameters.AddWithValue("#Recipientreportstatus", dt.Recipientreportstatus);
cmd.Parameters.AddWithValue("#Totalbytes", dt.Totalbytes);
cmd.Parameters.AddWithValue("#Numberrecipient", dt.Numberrecipient);
if (dt.OriginationTime != null)
cmd.Parameters.AddWithValue("#OriginationTime", dt.OriginationTime);
else
cmd.Parameters.AddWithValue("#OriginationTime", DBNull.Value);
//if OriginationTime was null, then insert with null value to this column
cmd.Parameters.AddWithValue("#Encryption", dt.Encryption);
cmd.Parameters.AddWithValue("#ServiceVersion", dt.ServiceVersion);
cmd.Parameters.AddWithValue("#LinkedMsgid", dt.LinkedMsgid);
cmd.Parameters.AddWithValue("#MessageSubject", dt.MessageSubject);
cmd.Parameters.AddWithValue("#SenderAddress", dt.SenderAddress);
cmd.ExecuteNonQuery();
}
How can my program run faster?
Thank you so much!
Use SqlBulkCopy.
Edit: I created a minimal implementation of IDataReader and created a Batch type so that I could insert arbitrary in-memory data using SqlBulkCopy. Here is the important bit:
IDataReader dr = batch.GetDataReader();
using (SqlTransaction tx = _connection.BeginTransaction())
{
try
{
using (SqlBulkCopy sqlBulkCopy =
new SqlBulkCopy(_connection, SqlBulkCopyOptions.Default, tx))
{
sqlBulkCopy.DestinationTableName = TableName;
SetColumnMappings(sqlBulkCopy.ColumnMappings);
sqlBulkCopy.WriteToServer(dr);
tx.Commit();
}
}
catch
{
tx.Rollback();
throw;
}
}
The rest of the implementation is left as an exercise for the reader :)
Hint: the only bits of IDataReader you need to implement are Read, GetValue and FieldCount.
Hmmm, let's break this down a little bit.
In pseudocode what you did is the ff:
Open the file
Open a connection
For every line that has data:
Parse the string
Save the data in SQL Server
Close the connection
Close the file
Now the fundamental problems in doing it this way are:
You are keeping a SQL connection open while waiting for your line parsing (pretty susceptible to timeouts and stuff)
You might be saving the data line by line, each in its own transaction. We won't know until you show us what the InsertData method is doing
Consequently you are keeping the file open while waiting for SQL to finish inserting
The optimal way of doing this is to parse the file as a whole, and then insert them in bulk. You can do this with SqlBulkCopy (as suggested by Matt Howells), or with SQL Server Integration Services.
If you want to stick with ADO.NET, you can pool together your INSERT statements and then pass them off into one large SQLCommand, instead of doing it this way e.g., setting up one SQLCommand object per insert statement.
You create the SqlCommand object for every row of data. The simplest improvement would therefore to create a
private static SqlCommand cmdInsert
and declare the parameters with the Parameters.Add() method. Then for each data row, set the parameter values using
cmdInsert.Parameters["#paramXXX"].Value = valueXXX;
A second performance improvement might be to skip creation of Data objects for each row, and assign Parameter values directly from the list[] array.

Categories

Resources