
May 9th, 2008, 02:23 PM
|
|
|
DataView has one less "row" than DataTable?
I have the following code:
PHP Code:
1 private void UpdateDatabase(string csvFilePath, string delimiterName, string tableName)
2 {
3 TableDataAccess dataAccess = new TableDataAccess();
4 DataTable dataTable = dataAccess.GetTableInfo("SELECT * FROM " + tableName, tableName, null);
5 DataView dataView = dataTable.DefaultView;
6
7 string fileLine;
8 IList<string> lines = new List<string>();
9 IList<string> parameters = new List<string>();
10 string delimiter = (delimiterName == "Comma") ? "," : @"\t";
11 int columnCount = 0;
12
13 // Open file
14 if (File.Exists(@csvFilePath))
15 {
16 // Open file
17 FileInfo csvFile = new FileInfo(@csvFilePath);
18
19 // Create resource stream
20 StreamReader reader = csvFile.OpenText();
21
22 // traverse each line of file
23 while (!reader.EndOfStream)
24 {
25 fileLine = reader.ReadLine().Trim();
26 if (!String.IsNullOrEmpty(fileLine) && !lines.Contains(fileLine))
27 {
28 string[] columnData = Regex.Split(fileLine, delimiter);
29 DataRowView drv = dataView.AddNew();
30 columnCount = columnData.Length;
31
32 for (int i = 0; i < columnCount; i++)
33 {
34 drv[i] = columnData[i];
35 }
36
37 lines.Add(fileLine);
38 }
39 }
40
41 // Close stream
42 reader.Close();
43
44 // Construct INSERT query
45 StringBuilder sql = new StringBuilder();
46 sql.Append("INSERT INTO ");
47 sql.Append(tableName);
48 sql.Append(" VALUES (");
49 for (int i = 0; i < columnCount; i++)
50 {
51 parameters.Add("Value" + i);
52 sql.Append("@Value" + i);
53
54 if (i != columnCount - 1)
55 {
56 sql.Append(",");
57 }
58 else
59 {
60 sql.Append(")");
61 }
62 }
63
64 dataAccess.UpdateTableInfo(sql.ToString(), tableName, dataTable, parameters);
65 }
66 }
I'm basically taking the contents of a delimited text file (i.e., .csv file or tab-delimited text file) and uploading it to a database table. The data access layer is hidden here, but you don;t need to see it (because the problem is not in there).
For some crazy reason "dataTable" has one less row than "dataView" (even though DataViews don't technically have rows?), immediately before the dataAccess.UpdateTableInfo() method is executed (verified this in the debugger). The data count should be the same, but it isn't. As a result, my updated database table (getting its data from "dataTable" via method dataAccess.UpdateTableInfo() ) is missing the very last line of data in the delimited text file.
I can only presume that the last DataRowView created in my while loop (used to iterate through the lines of my delimited text file), while added to the DataView, is never "committed" to the accompanying DataTable. The final value of "fileLine" in that loop IS the value of the last line in the text file (verified in debugger).
So why is the the last line of the file, contained in the final, newly-added DataRowView in my DataView, never added to the corresponding DataTable. Is there a way to "commit" changes to the DataTable before I send it to my data access logic, to be subsequently added to my database table?
What exactly is going on here? ...Thanks.
|