 |
|
 |
Hi! I have an application which saves its data in an excel sheet. There are about 4000 rows to add to the sheet, each containing 14 columns. Each insertion is done by creating a new instance of an OleDbCommand object, setting the connection (which is already open) to it, setting the command text and performing ExecuteNonQuery.
My problem is that the whole process takes more than 1 minute, which is too much. I have identified that the reason for slowing the whole thing down is in the following code that actually does the insertion (most probably in the execution itself of the query):
System.Data.OleDb.OleDbCommand objCmd = new System.Data.OleDb.OleDbCommand(); objCmd.Connection = _oleConn; objCmd.CommandText = sql; // A preset string objCmd.ExecuteNonQuery(); the 'sql' variable is pre-set to a string that has the following format more or less (the column and sheet names were replaced with generic names): "Insert into [Worksheetname$A1:R65536] ([Index], [Column1], [Column2], [Column3], [Column4], [Column5], [Column6], [Column7], [Column8], [Column9], [Column10], [Column11], [Column12], [Column13], [Column14]) values ('II54959', 'II54970', 'NTI295', 'TTI347', 'TI226', 1569, 1, 'Tx', '', '10840', 'NO', 'NA', 'UK', 'NO', '')"
I didn't write this code myself, and I don't know why each time the INSERT is done to Row 65536. I guess that it automatically pushes it to the last row. Maybe this is the cause of the bad performance?! Any suggestions?
Thanks for the help
while (i == live) { have(fun); }
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Hi,
I am doing oledb connection with excel from my c# application. I am writting query to select some coulmn. Query is like this.
Select [Fund],[statement$ Details], [Currency Hit$] From Sheet1 $ A1:N30
Instead of coulmn name, can i give the coulmn name ???
Ex:- Select A1, K1,P1,N1 From Sheet1 $ A1:N30
I don't want to give coulmn name because it has special characters.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Is it possible to Read the Formula behind the Cell? Like =A1+A2 ??? If yes, how can i do this? I already implemented the Code, and it works! but i need the Formula behind the Cells. Can anybody Help me?
|
| Sign In·View Thread·PermaLink | 1.50/5 (2 votes) |
|
|
|
 |
|
 |
Hi All, Im working with excel and ado.net now. I faced the following problem : Whenever I try to open the sheet "CLEAVEDB62305offer_0529163742Ge" , it opens with no problem. But when I try to set a range for it, like "CLEAVEDB62305offer_0529163742Ge$A1:B1", Jet shows me an error telling me it cannot be found.
Can anyone help me?
Its killing me.. =/
plz, mail me : rmoreirao@ixlink.com
Rodrigo Goes Moreirao
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
 |
Yet another BUG in this class: when a sheet name has spaces the name returns within ''. So here is the fix:
// Add the sheet name to the string array. foreach(DataRow row in dt.Rows) { string strSheetTableName = row["TABLE_NAME"].ToString(); strSheetTableName = strSheetTableName.Trim(new char[]{'\''}); excelSheets[i] = strSheetTableName.Substring(0,strSheetTableName.Length-1); i++; }
Enjoy,  Cabbi
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
The 'SetValue' method had a BUG by setting string values. So here my little contribution to this class nice but BUGGY:
private bool SetSheetQuerySingelValUpdate(object value) { try { if (m_Conn == null) { throw new Exception("Connection is unassigned or closed."); }
if (m_SheetName.Length ==0) throw new Exception("Sheetname was not assigned.");
m_CmdUpdate = new OleDbCommand(@"Update ["+m_SheetName+"$"+m_SheetRange+"] set F1=@value", m_Conn);
OleDbParameter dbParam = m_CmdUpdate.CreateParameter(); dbParam.DbType = DbType.Object; dbParam.ParameterName = "@value"; dbParam.Value = value; m_CmdUpdate.Parameters.Add(dbParam);
return true; } catch (Exception ex) { throw ex; } }
Enjoy,  Cabbi
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
|
 |
|
|
 |
|
 |
I have my Excel sheet which has 15 column n some has 10..i don't know how to read with respect to no of columns required and read the excel file respectively..I'm reading excel file with three columns only and its not taking more than three columns. can anyone plz help me with codes..coz m really new to it. Plz can anyone help me out. Thanks Ravi
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
I have my Excel sheet which has 15 column n some has 10..i don't know how to read with respect to no of columns required and read the excel file respectively..I'm reading excel file with three columns only and its not taking more than three columns. can anyone plz help me with codes..coz m really new to it. Plz can anyone help me out. Thanks Ravi
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
In SetSheetQueryAdapter method: Line 303:
for (int iPK=0;iPK{ strWhere = AddWithAnd(strWhere,dt.Columns[iPK].ColumnName + "=?"); } Should be changed to:
for (int iPK=0;iPK{ strWhere = AddWithAnd(strWhere,dt.Columns[PKCols[iPK]].ColumnName + "=?"); }
Line 337:
for (int iPK=0;iPK{ oleParUpd = new OleDbParameter("?",dt.Columns[iPK].DataType.ToString()); oleParUpd.SourceColumn =dt.Columns[iPK].ColumnName; oleParUpd.SourceVersion = DataRowVersion.Original; oleda.UpdateCommand.Parameters.Add(oleParUpd); } Should be changed to:
for (int iPK=0;iPK{ oleParUpd = new OleDbParameter("?",dt.Columns[PKCols[iPK]].DataType.ToString()); oleParUpd.SourceColumn =dt.Columns[PKCols[iPK]].ColumnName; oleParUpd.SourceVersion = DataRowVersion.Original; oleda.UpdateCommand.Parameters.Add(oleParUpd); }
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
If I set SheetRange "A10:C15", then I get an error when call GetTable(); I found this error is due to the row number "10" which is greater than 1 digit, stemming from the following code: string FirstCol = _strSheetRange.Substring(0,_strSheetRange.IndexOf(":")-1); in GetTable(string strTableName) method, which gets a wrong column name "A1".
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
It's basically because the input start field is expected to be within the first nine lines of the spreadsheet. This should be more stable:
// pick out the column name from the input int intIlen =0; for (int intI = 0; intI < _strSheetRange.Substring(0, _strSheetRange.IndexOf(":")).Length; intI++) { // we read until we find a non-alfabetic value, i.e a number if (Char.IsLetter(_strSheetRange[intI])) intIlen = intI; else break;
} string FirstCol = _strSheetRange.Substring(0,intIlen+1);
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
FYI
I had a strange problem where the first column of the spreadsheet I was reading contained document heading numbers (e.g. 1.1, 1.2.8, etc.) Entries like 1.2.8 would be returned as is but entries like 1.1 would return null. Apparently, the OLEDB driver decided 1.1 was a number and 1.2.8 was a string and only wanted to return strings. I tried setting the MixedData property but this did not solve the problem. Upon further investigation, I determined that the a value of IMEX=1 solves the problem whereas setting MixedData property to true sets IMEX=2.
|
| Sign In·View Thread·PermaLink | 2.40/5 (5 votes) |
|
|
|
 |
|
 |
I've done something similar in my application (wish I'd found this sample before - it would have saved me a lot of work).
I'm getting a problem where the text being read from each cell is truncated at 255 characters - but only on certain PCs. I have tried this sample project and it too suffers the same problem.
Has anyone else experienced this, and do you know the solution?
|
| Sign In·View Thread·PermaLink | 2.00/5 (2 votes) |
|
|
|
 |
|
|
 |
|
 |
You might consider using ODBC instead of OLEDB which will eliminate the problem of 255 symbols. In my case I am not able to access every customer registry and I might not even have right for that.
OdbcConnection con = new OdbcConnection(@"Driver={Microsoft Excel Driver (*.xls)};DBQ=c:\temp\testbook.xls"); OdbcCommand cmd = new OdbcCommand(@"SELECT * FROM [Workbook1$]"); cmd.Connection = con; OdbcDataAdapter da = new OdbcDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds);
At least this solved my problem with importing XLS files with fields with more than 255 symbols.
gl & hf
[tridy] Stockholm, Sweden
modified on Monday, May 26, 2008 7:46 AM
|
| Sign In·View Thread·PermaLink | 2.00/5 (1 vote) |
|
|
|
 |
|
 |
Is there any way to create a new file using this data access technology ? It's great idea to be able to read and write excel files without having excel installed, but how to create excel file ?
big thanx for any responce
Slawomir
|
| Sign In·View Thread·PermaLink | 1.42/5 (7 votes) |
|
|
|
 |
|
 |
Congratulations for this article, fantastic!
Need develop a Professional Software with low cost? Contact me!
|
| Sign In·View Thread·PermaLink | 1.00/5 (1 vote) |
|
|
|
 |
|
 |
Very useful utility - thanks for your article. After successfully reading in the data, I'm trying to build a new datatable and export all data to a named worksheet. I've tried updating using the SetTable method, which does work in a fashion, however every time I run the utility it adds a number of blank rows (z) before inserting the records (interestingly as many blank rows as is in the dataset). The second time I try = 2 * z blank rows ; 3rd time I try 3* z blank rows etc etc
heres my code snippet for exporting ... _dtPrev is what gets sucked in using GetTable() ... DataTable dtTest = _dtPrev.Clone(); exr = new ExcelReader(); exr.KeepConnectionOpen = false; exr.ExcelFilename = "C:\\temp\\a.xls"; exr.Headers = false; exr.MixedData = true; exr.SheetName = "TASK"; exr.SheetRange = "A1:U65535"; for (int i = 0; i < _dtPrev.Rows.Count; i++) { dtTest.ImportRow(_dtPrev.Rows[i]); dtTest.Rows[i].SetAdded(); }
int[] intPKCols = new int[] { 0 }; exr.PKCols = intPKCols;
exr.SetTable(dtTest);
Any ideas would be greatly appreciated
|
| Sign In·View Thread·PermaLink | 1.67/5 (3 votes) |
|
|
|
 |
|
 |
Using the demo, I read from an excel file. The problem is if the row has a numeric value it is returning me a db null. If I change the type of the column from General to Text that it returns the value if the value is atleast 2 digits longs. If the value is 1 digit it still returns null. It absolutely isn't making any sense to me since I am doing a select * on the sheet.
Is there something wrong with the demo? As a matter fact, when you look at the introduction, it seems like even single digit numbers are read withno problem. Something fishy going on. Please help!
|
| Sign In·View Thread·PermaLink | 2.25/5 (4 votes) |
|
|
|
 |
|
 |
Change Source code like :
private string ExcelConnectionOptions() { string strOpts = ""; if (this.MixedData == true) strOpts += "Imex=2;"; if (this.Headers == true) strOpts += "HDR=Yes;"; else strOpts += "HDR=No;"; return strOpts; }
to :
private string ExcelConnectionOptions() { string strOpts = ""; if (this.MixedData == true) strOpts += "Imex=1;"; if (this.Headers == true) strOpts += "HDR=Yes;"; else strOpts += "HDR=No;"; return strOpts; }
Reference : http://msdn2.microsoft.com/en-us/library/ms254500.aspx[^]
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
It appears that if the Excel sheet you are reading has errors in the cells, the JET ISAM driver does not handle the data but instead swaps in a NULL for the cells it doesn't like.
I have seen a number of similar problems like this from other users and I suspect it is related to a limitation. After searching MSDN and the Internet I have found no way to tell the OleDbDataAdapter to simply return the data verbatim (as-is).
If anyone know hows to do this, please email me at mike_luster@hotmail.com.
This could not be much simpler, I only wish that it worked!
string query = string.Format( "SELECT * FROM [{0}${1}]", tableName, range ); _oleCmdSelect = new OleDbCommand( query, _oleCon );
//Fill table OleDbDataAdapter oleAdapter = new OleDbDataAdapter( ); oleAdapter.SelectCommand = _oleCmdSelect; DataSet ds = new DataSet( ); int n = oleAdapter.Fill( ds );
Note I can accomplish reading the spreadsheet no problem if I use the Excel Object Model but that's a lot more code to maintain so unless I have to go that route I'd rather avoid it.
Mike Luster CTI/IVR/Telephony SME
|
| Sign In·View Thread·PermaLink | 3.38/5 (5 votes) |
|
|
|
 |
|
 |
Hi
Unless I select a small range for the Range: parameter, when I click on SaveData I get an error message: "These columns don't currently have unique values". I have put in all the fixes described on this forum, and I have the column names correctly displayed as they are on the Excel worksheet. Regardless of which column I use for Primary Key Col, I get the same result. Also, I don't understand how the primary key works. Can someone please explain it to me - like why it's needed.
UPDATE: I've now figured out the primary key bit, and I'm no longer getting the above error - however I'm now getting a "One or more required parameters do not have values" error.
-- modified at 2:51 Tuesday 7th March, 2006
|
| Sign In·View Thread·PermaLink | 1.40/5 (5 votes) |
|
|
|
 |