Extracting Excel data using Sql connection string - sql

My intention here is to render the excel data in a table, using a datatable, so opened an odbc connection and loaded the datatable like this:
System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection(Dsn=Excel Files;Driver={Microsoft Excel Driver (*.xls)};Driverid=790;Dbq=C:\Users\*******\Documents\Book1.xlsx;DefaultDir=C:\Users\******\Documents;HDR=YES);
conn.Open();
DataTable dataTable = new DataTable();
string con = "select * from [sheet1$]";
System.Data.Odbc.OdbcCommand cmd = new System.Data.Odbc.OdbcCommand(con, conn);
System.Data.Odbc.OdbcDataReader dr = cmd.ExecuteReader();
dataTable.Load(dr);
But insted getting an error like this
ERROR [IM014] [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application
So actually whats' the root cause ?

Please use the following link as a possible solution to your problem:
ERROR [IM014] [Microsoft][ODBC Driver Manager] The specified DSN contains an architecture mismatch between the Driver and Application

This suggests
"changing the Platform target project setting from Any CPU to x86."
will help. The root cause is, as the error says, a mismatch between the database driver and your application.
These docs say
If you use the 64-bit odbcad32.exe to configure or remove a DSN that
connects to a 32-bit driver, for example, Driver do Microsoft Access
(*.mdb), you will receive the following error message: The specified
DSN contains an architecture mismatch between the Driver and
Application To resolve this error, use the 32-bit odbcad32.exe to
configure or remove the DSN.

Related

DISTINCT AND UNION keywords cause timeout exception in iDB2Command

I'm trying to query some date from DB2/IBM AS400. for this purpose I've tested theses two libraries: Oledb and iDB2Command that iDB2Command is released by IBM in their iSerires package for .Net
when I execute the following command with iDB2Command, I get a command timeout exception:
using (iDB2Connection connection = new iDB2Connection("DATA SOURCE=0.0.0.0;USER ID=TEST;PASSWORD=TEST;CheckConnectionOnOpen=true;EnablePreFetch=false;"))
{
connection.Open();
iDB2Command cmd = new iDB2Command("SELECT DISTINCT * FROM LIB.Table1", connection);
cmd.CommandTimeout = 0;
DataTable dt = new DataTable();
iDB2DataAdapter da = new iDB2DataAdapter(cmd);
da.Fill(dt);
}
But when I use Oledb to execute the same query there is no problem and the query will be execute and returns some data.this is while if I remove "DISTINCT" keyword, the query will be executed with iDB2Command too. Similar to this problem is exists for UNION keywords and nested queries.
I even checked and config theses things, but the problem was not solved:
Increase QRYTIMLMT using CHGQRYA command
Uncheck "Allow Query Timeout" in "Odbc Datasource Administrator" for iSeries DSN.

Exception "Data source name not found" while connecting to an existing DSN

I have created an ODBC connections (both 32/64 bit) with configuration given below:
Microsoft SQL Server ODBC Driver Version 10.00.14393
Data Source Name: ODBCMSSQL
Data Source Description:
Server: .\SQLEXPRESS
Database: MedicalMarketting
Language: (Default)
Translate Character Data: Yes
Log Long Running Queries: No
Log Driver Statistics: No
Use Regional Settings: No
Prepared Statements Option: Drop temporary procedures on disconnect
Use Failover Server: No
Use ANSI Quoted Identifiers: Yes
Use ANSI Null, Paddings and Warnings: Yes
Data Encryption: No
I want to connect to local MsSQL server as given in below code snippet:
string connectionString = "Data Source=ODBCMSSQL;Initial Catalog=MedicalMarketting;Integrated Security=True";
con = new OdbcConnection(connectionString);
cmd = new OdbcCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
try
{
this.con.Open();
this.tr = con.BeginTransaction();
this.cmd.Transaction = tr;
}
catch (Exception ex)
{
this.RollBack();
}
This throws an exception which has an error message as below:
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Sorry if this was too basic, but had to post for a clue because the same configuration for different ODBC connections works perfectly.
I found a quick fix to the problem by changing the connection string to,
string connectionString = "DSN=ODBCMSSQL";// best practice is to store this in a seperate config file.
In fact, other attributes specified (Initial Catalog, Integrated Security) are not ODBC connection string attributes, hence ignored. A full list of ODBC connection attributes can be found below.
https://msdn.microsoft.com/en-us/library/ee275047(v=bts.10).aspx

R Connect to SQL Database using RODBC

I use the following specs to connect to db from Matlab and it works:
% Matlab code
spec.dbName = 'xxxyyy';
spec.login = 'uid';
spec.pwd = 'mypwd';
spec.driver = 'net.sourceforge.jtds.jdbc.Driver';
spec.url = 'jdbc:jtds:sqlserver://vmsqlprod7:1234/xxxyyy';
conn = database(spec.dbName, spec.login, spec.pwd, spec.driver, spec.url);
I'm almost sure this is all I need to connect from R. But I can't seem to make it work with the following code:
## R code
require(RODBC)
con <- odbcDriverConnect(connection=
"driver=net.sourceforge.jtds.jdbc.Driver;
server=jdbc:jtds:sqlserver://vmsqlprod7:1234/xxxyyy;
database=xxxyyy;
uid=uid;
pwd=mypwd")
It throws out this error:
[ODBC Driver Manager] Data source name not found and no default driver specified
I've read this and I suspect it's about the format of the url or driver string, but I don't know exactly how to make it recognizable by R.
I can really use some help here!
Environment: Windows 7, 64 bit.
R version: 3.3.3

Clean and reset table in mvc 4 Web application

I am wanting to delete all data from a table and reset the id column to 1. I want to do this in my controller, but i want to know the best way to do that. I have tried the SQLConnection/SQLCommand route, but have been unable to connect to the database successfully to do that. Is there a way like running a db.Clean function or something like that?
Updated
Here is how far it gets in the code:
string connectionString = "Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\\Webs\\MvcFFL\\MvcFFL\\App_Data\\Players.mdf;Integrated Security=True";
string queryString = "Truncate table Players;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(queryString, connection);
connection.Open(); <- Fails here opening the connection
}
Then when it hits the connection.Open() here is the error:
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code Additional information: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
What can i do instead of this method?
You have an unescaped \in (LocalDB)\v11.0
You can change it to (LocalDB)\\v11.0, or else change the whole connection string to
#"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\Webs\MvcFFL\MvcFFL\App_Data\Players.mdf;Integrated Security=True" (notice the # and the lack of \\)

SQL Anywhere connection error: The driver doesn't support the version of ODBC behavior that the application requested

I'm trying to connect to a SQL Anywhere 5 database (I know it's olllld!) with a .NET 3.5 app in WinXP and it works fine from a stand alone console app. But when I run the exact code in a plugin ,running off a separate AppDomain, (the only difference I can tell between the two) with the following code, I get the errors further below.
BTW Both are executed as the SAME user.
using (OdbcConnection connection =
new OdbcConnection(strConnect))
{
OdbcCommand command = new OdbcCommand(query, connection);
command.CommandType = CommandType.Text;
DataTable posRecordsTable = new DataTable();
connection.Open();
OdbcException Exception returns the following errors:
Index #0
Message: [Sybase][ODBC Driver]Unable to connect to database server: database engine not running
Index #1
Message: [Sybase][ODBC Driver]Invalid connection string attribute
Index #2
Message: [Sybase][ODBC Driver]Invalid connection string attribute
Index #3
Message: [Microsoft][ODBC Driver Manager] The driver doesn't support the version of ODBC behavior that the application requested (see SQLSetEnvAttr).
Does the driver on your AppDomain have the same configuration as your stand alone box? I think checking the similarity of the environments will help you. Usually such errors are resolved by looking at the config files from where the driver reads its information. Dont know much about SQL Anywhere, but in general, I've come across such issues and I fixed them by altering the connection information or the configuration file.