I am pulling in a excel file to my project everything was working fine but not I have grab all of the cell that contain WMI. I am having trouble with stbQuery does any one know the correct syntax for grabbing information that only contains certain characters.
OleDbConnection con = new OleDbConnection ("provider=Microsoft.Jet.OLEDB.4.0;data source=" + txtFileName.Text + ";Extended Properties=Excel 8.0;");
StringBuilder stbQuery = new StringBuilder();
stbQuery.Append("Select [Wireless Number (uneditable)], [* Last Name] FROM [" + txtSheets.Text + "$] WHERE [* Last Name] = LIKE '%WMI%' ");
This is an SQL Syntax - LIKE Issue.
The LIKE Keyword doesn't need an = in Structured Query Language.
Example:
SELECT * FROM Customers
WHERE [* Last Name] LIKE 's%';
UPDATE
Look at D Stanley Comment.
Related
I am building an extension to an existing Access database and an accompanying front end programmed in C#. The original Access database was not designed very well and certainly not designed with future expansion in mind. For simplicity's sake, lets say the legacy DB has 2 tables: tblEmployee [empId(AutoNumber), empName(Text)] and tblProjects [prjId(AutoNumber), prjName(Text), prjEmps(Number/Lookup)]. Both tables have an AutoNumber primary key. The Projects table has a multi-value lookup field that allows users to assign multiple employees to a project. When I query the tblProjects table in Access SELECT prjId, prjName, prjEmps FROM tblProject;, the prjEmps field lists all the employees' names separated by commas. However, the problem is when I use the same query in C#, the prjEmps returns a string version of a number that is not the empId of the employee(s). I am not sure if it makes a difference, but I am using the System.Data.OleDb and System.Data namespaces in C#. Here is the gist my C# code:
string connStr = #"Provider = Microsoft.ACE.OLEDB.12.0; " +
#"Data Source=" + dbFilePath;
string query = "SELECT prjId, prjName, prjEmps FROM tblProject;";
OleDbConnection dbConn = new OleDbConnection(connStr);
OleDbCommand Cmd = new OleDbCommand(query, dbConn);
OleDbDataAdapter adp = new OleDbDataAdapter(query, dbConn);
DataTable dt = new DataTable();
adp.Fill(dt);
dbConn.Close();
foreach (DataRow row in dt.Rows)
{
int prjId = row.Field<int>("prjId");
string prjName = row.Field<string>("prjName");
string prjEmps = row.Field<string>("prjEmps");
MessageBox.Show("Project ID: " + prjId.ToString() + "\n" +
"Project Name: " + prjName + "\n" +
"Employees: " + prjEmps);
}
I would be happy if I could just get the concatenated list of names, but I would prefer an array of integer keys or the like. Any ideas on how to fix this?
Use ODBC provider, OLEDB does not supports multi-value lookup field and you get garbage values if you use it to read multi-value lookup field , using ODBC you will get ";" separated values which can then be split into individual values or replace with ",".
I am using OleBb objects to read excel file into data table
System.Data.OleDb.OleDbConnection myCon = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;data source='" + FileName + "';Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1\" ");
System.Data.OleDb.OleDbDataAdapter cImport = new system.Data.OleDb.OleDbDataAdapter("select * from [ Gravity$A3:ZZ]", myCon );
cImport.Fill(dt)
Executing above query shows Error :
Invalid bracketing of name ' Gravity$A3:ZZ'.
I think this error is due to one space at starting of sheet name " Gravity".
If I remove this space in query & write like "select * from [Gravity$A3:ZZ]", then it shows invalid object name.
How to deal with this issue only using OleDb objects?
try putting the name between quotes
System.Data.OleDb.OleDbDataAdapter cImport = new system.Data.OleDb.OleDbDataAdapter("select * from [\" Gravity$A3:ZZ\"]", myCon );
Second: as #PanagiotisKanavos says
Α3:ZZ doesn't seem like a valid range
Or even try
select * from [' Gravity$A3:ZZ']
I am using OLEDB to read excel file into datatable. But the problem is, some values are missing(Empty). In my excel sheet one column datatype is General, it has mixed values like string and integers. Most of the cell values are integers. Why OLEDB is skipping string values.
OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + "; Extended Properties=\"Excel 12.0;IMEX=1\";";
OleDbCommand myAccessCommand = new OleDbCommand();
myAccessCommand.CommandText = "Select * from [" + sheetName + "]";
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(myAccessCommand);
myDataAdapter.Fill(myDataSet);
Check following link and see points under "RESOLUTION":
http://support.microsoft.com/kb/194124
Please see point 2 NOTE.
Setting IMEX=1 is entirely dependent on your registry settings. By default, first 8 rows are checked to determine the data type. IMEX=1 can give unpredictable behaviors, such as skipping string values. There is also one small workaround for this problem. Just add single quote (') before every cell value in excel. Every cell will be treated as string.
Add IMEX=1 to the connection string as below:
string con = string.Format(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};" + #"Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'", fileName);
I am trying to read an Excel file via a SSIS ScriptTask to check for certain cell values in that worksheet.
In the code example you can see that the strSQL is set to "H4:H4" to only read one cell. This cell can only have a true or false value.
Since I also need to check for a certain string value in B1 I wanted to extend this version.
string filePath = "c:\\test\\testBoolean.XLSX";
string tabName = "testSheet$";
string strSQL = "Select * From [" + tabName + "H4:H4]";
String strCn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source="
+ filePath + ";Extended Properties=\"Excel 12.0;HDR=NO;IMEX=1\";";
OleDbConnection cn = new OleDbConnection(strCn);
int iCnt = 0;
OleDbDataAdapter objAdapter = new OleDbDataAdapter(strSQL, cn);
DataSet ds = new DataSet();
objAdapter.Fill(ds, tabName);
DataTable dt = ds.Tables[tabName];
foreach (DataRow row in dt.Rows)
{
iCnt = iCnt + 1;
// some processing....
}
What I don't understand is why I get a boolean value with the above strSQL statement or with any statment containing the same row number like so:
string strSQL = "Select * From [" + tabName + "F4:H4]";
Debug-Output:
row.ItemArray[2] false object {bool}
But when I set a different range like this one:
string strSQL = "Select * From [" + tabName + "F1:H4]";
I loose the recognition of the bool value:
row.ItemArray[2] "FALSE" object {string}
I'd much rather like to use the bool value for other processing tasks.
How can I fix this in addition to also reading the B2 value?
Your connection string specified IMEX=1, which tells the driver to treat intermixed data types as text. (See the "Usage Considerations" section of the MSDN article Excel Connection Manager.)
Thus, when you specified a single row
string strSQL = "Select * From [" + tabName + "F4:H4]";
there was only one possible data type for the third column, and the driver was able to correctly infer it. However, when you specified multiple rows
string strSQL = "Select * From [" + tabName + "F1:H4]";
and any value in the range H1:H4 was not a bool, the driver translated all values in that column to strings.
Assuming that you do in fact have mixed data types in column H and only care about the values in two particular cells, the simplest solution is to query each cell individually. See Import a single Excel cell into SSIS for some ideas on how to do that.
I would clone most of the code to produce two separate SELECT statements to query the two different cells you are after with separate SQL statements.
Actually I would probably go further and shred the whole script into SSIS components e.g. Execute SQL Tasks or Data Flow Tasks.
I know this topic is done to death but I am at wits end.
I need to parse a csv. It's a pretty average CSV and the parsing logic has been written using OleDB by another developer who swore that it work before he went on vacation :)
CSV sample:
Dispatch Date,Master Tape,Master Time Code,Material ID,Channel,Title,Version,Duration,Language,Producer,Edit Date,Packaging,1 st TX,Last TX,Usage,S&P Rating,Comments,Replace,Event TX Date,Alternate Title
,a,b,c,d,e,f,g,h,,i,,j,k,,l,m,,n,
The problem I have is that I get various errors depending on the connection string I try.
when I try the connection string:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source="D:\TEST.csv\";Extended Properties="text;HDR=No;FMT=Delimited"
I get the error:
'D:\TEST.csv' is not a valid path. Make sure that the path name is spelled correctly and that you are connected to the server on which the file resides.
When I try the connection string:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\TEST.csv;Extended Properties=Excel 12.0;
or the connection string
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\TEST.csv;Extended Properties=Excel 8.0;
I get the error:
External table is not in the expected format.
I am considering throwing away all the code and starting from scratch. Is there something obvious I am doing wrong?
You should indicate only the directory name in your connection string. The file name will be used to query:
var filename = #"c:\work\test.csv";
var connString = string.Format(
#"Provider=Microsoft.Jet.OleDb.4.0; Data Source={0};Extended Properties=""Text;HDR=YES;FMT=Delimited""",
Path.GetDirectoryName(filename)
);
using (var conn = new OleDbConnection(connString))
{
conn.Open();
var query = "SELECT * FROM [" + Path.GetFileName(filename) + "]";
using (var adapter = new OleDbDataAdapter(query, conn))
{
var ds = new DataSet("CSV File");
adapter.Fill(ds);
}
}
And instead of OleDB you could use a decent CSV parser (or another one).
Alternate solution is to use TextFieldParser class (part of .Net framework itself.) https://learn.microsoft.com/en-us/dotnet/api/microsoft.visualbasic.fileio.textfieldparser
This way you do not have to rely on other developer who has gone for holidays. I have used it so many times and have not hit any snag.
I have posted this from work (hence I cannot post an example snippet. I will do so when I go home this evening).
It seems your first row contains the column names, so you need to include the HDR=YES property, like this:
Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\TEST.csv;Extended Properties="Excel 12.0;HDR=YES";
Try the connection string:
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\TEST.csv;Extended Properties=\"Excel 8.0;IMEX=1\""
var s=#"D:\TEST.csv";
string dir = Path.GetDirectoryName(s);
string sConnection = "Provider=Microsoft.Jet.OLEDB.4.0;"
+ "Data Source=\"" + dir + "\\\";"
+ "Extended Properties=\"text;HDR=YES;FMT=Delimited\"";