Difference between SqlConn and ODBC? - c#

I have a control which requires SQCN connection. So I supplied it with proper credentials and there's nothing wrong with it.
string connection = "Data Source=(local);Initial Catalog=test;user ID=sa; Password=sa12345;";
SqlConnection sqcn = new SqlConnection(connection);
sqcn.Open();
Now due to requirements I need to change the dbConnection from SqlConnection to OdbcConnection. I used the same connection and replace Sql with Odbc but it shows an error
string connection = "Data Source=(local);Initial Catalog=test;user ID=sa; Password=sa12345;";
OdbcConnection odbc= new OdbcConnection(connection);
odbc.Open();
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Can someone pls tell me if im missing something.. Tnx!

You should install the proper driver for ODBC and define the data source. You can see data sources in administrative tools -> ODBC Data Source, also you can define data source here and reference it in your program via its Name.

Related

Trying to connect to SQL Server using C#

I am trying to connect to SQL Server and get data. This is what I did, but it's not working:
string connectionString;
SqlConnection cnn;
connectionString = #"Data Source=(IP)\PC-NAME\SQLEXPRESS,3306;Network Library=DBMSSOCN;Initial Catalog=dbase;User ID=sa;Password=password";
cnn = new SqlConnection(connectionString);
cnn.Open();
MessageBox.Show("Connection Open !");
cnn.Close();
Your Code is Correct, except your connection string i think
So, first, connect to your database via server Explorer in VisualStudio\View menu
Then right-click on your database and select properties and check the connection string and copy that for test
I think you have a problem with your connection string.
Check your connection string using this given example:
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;Initial
Catalog=myDataBase;User ID=myUsername;Password=myPassword;

OdbcConnection DataName and DataSource not found

I am using ASP.net to create a web app. I use OdbcConnection to connect to MySQL. But it seems to create the object but not it doesn't fill up the datasource and dataname fields. I have the famous error
[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified
Here is my code failing:
OdbcConnection MyConnection;
string MyConString = "DRIVER={MySQL ODBC 5.1 Driver};" + "SERVER=localhost;" + "DATABASE=test;" + "UID=debug_user;" + "PASSWORD=password;" + "OPTION=3";
MyConnection = new OdbcConnection(MyConString);
MyConnection.Open();
I also use to have a connectionstring in my web.config:
<connectionStrings>
<add name="UTRN_DB_ConnectionString" connectionString="DRIVER={MySQL ODBC 5.1 Driver};Database=cjr_db;Server=localhost;UID=cjr_db_user;PWD=password;"/>
</connectionStrings>
I have the driver set in my Data Source Administrator with MySQL. I've uninstall and reinstall everything. I keep getting this error. I Just don't understand why I get this error while I still specify everything (port, etc...) in the connection string and it keep failing
EDIT:
Here is my web.config
<connectionStrings>
<add name="UTRN_DB_ConnectionString" connectionString="Driver={MySQL ODBC 5.1 Driver};Database=cjr_db;Server=localhost;Uid=cjr_db_user;Pwd=password;providerName=(System.Data.Odbc)"/>
</connectionStrings>
and this is how i read it:
string conString = WebConfigurationManager.ConnectionStrings["UTRN_DB_ConnectionString"].ConnectionString;
string conString2 = ConfigurationManager.ConnectionStrings["UTRN_DB_ConnectionString"].ConnectionString;
using (OdbcConnection connection = new System.Data.Odbc.OdbcConnection(conString2))
{
connection.Open();
...
}
The connection string must contain Provider and Data Source as:
string MyConString = "Provider=(Data Provider);Data Source=(Path of source file)"
You have some spelling mistakes in your connection string in the web.config file. It should be Driver instead of DRIVER, Pwd instead of PWD, Uid instead of UID and so on.
Please change your code to the one below
Driver={SQL Server};Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;
I found the answer of my problem here:
tadaaaam
Basically, you have to seek the difference of your 64bits and 32bits installed on your machine. By default VS is seeking for the 32. And in your ODBC Sources Manager it shows up the 64.
So you either configure your project to seek for the 64 OR install the 32 bits version of MySQL ODBC.

ADO.NET : Error occurred while establishing a connection to SQL Server

i am trying to bind data to Gridview and getting this error
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)
this is my simple code
string cs = "data source =.; initial catalog= MyDB; integrated security= SSPI";
SqlConnection con = new SqlConnection(cs);
SqlCommand cmd = new SqlCommand("select * from tbl_Dept", con);
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Close();
in SQl management studio i have rechecked db name and its same as mentioned in code
I found some related questions but those did not work for me so posting mine one , Please help me with it,
You can define SqlConnection string in your web.config(in web applications) or app.config(in windows form application).
Simply define like this -
<configuration>
<connectionStrings>
<add name="Connection" providerName="System.Data.SqlClient" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=DBName;Trusted_connection=yes;User ID=''; Password=''"/
</connectionStrings>
</configuration>
then access this code in you .cs form using
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Connection"].ConnectionString.ToString());
and after this you can use SqlConnection object con in you whole form.
When you install SQL Server Express the setup propose to create a named instance, and by default, this named instance, is called SQLEXPRESS.
When you want to connect to this named instance it is required to specify the name.
So, in your case your connection string should be changed to
"Data Source =.\SQLEXPRESS;Initial Catalog=MyDB;Integrated Security=SSPI";

Why ConnectionString fails for SqlConnection using MSN example in C#?

I am using the following tutorial example verbatim:
http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcomboboxcolumn.aspx
The error message is that the connection failed. "Modify it to connect to a Norhtwinf=d database accessible to your system."
string connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=localhost";
SqlConnection northwindConnection = new SqlConnection(connectionString);
northwindConnection.Open();
As far as Northwind Database, I downloaded it from this website and I ran it.
http://www.microsoft.com/download/en/details.aspx?id=23654
Would you be able to tell what am I doing wrong?
Data Source property needs to point to your SQL instance name, and if your SQL instance is the default one.
I know that the next suggestion is a little weird and looks like the same that you are using, but try and let me know what happened:
string connectionString =
"Integrated Security=SSPI;Persist Security Info=False;" +
"Initial Catalog=Northwind;Data Source=.";
note that I've modified the Data Source value from 'localhost' to a (dot).
Make sure the account has access to that database, and try using this connection string:
connectionString="Server=MACHINE-NAME\SQLEXPRESS;Database=Northwind;Trusted_Connection=True;"

Data source name not found and no default driver specified

I'm getting this error when trying to open the connection in code as follows:
string queryString = "Insert into Table;
OdbcConnection connection = new OdbcConnection();
connection.ConnectionString = Settings.Default.STIMConnectionString;
OdbcCommand command = new OdbcCommand(queryString,connection);
connection.Open();
command.ExecuteNonQuery();
My Appconfig is as follows:
<add name="WindowsFormsApplicationTransducer.Properties.Settings.STIMConnection"
connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Data Source="D:\Development\SS Observer II Decoder.mdb"" />
What am i doing wrong?
Since you are using the OdbcConnection, I think you need to include the "Driver" information in your connection string.
Data Source is not a valid connection string property. ODBC originally used a data source name, or DSN=dsnname, where the DSN was configured separately on the system. However you can alternatively specify the driver and driver-specific parameters, which in the case of the Microsoft Access driver is at the minimum the file name: Driver={Microsoft Access Driver (*.mdb)};DBQ=D:\Development\SS Observer II Decoder.mdb.
See http://connectionstrings.com/ to see what you need to have.

Categories

Resources