Why ConnectionString fails for SqlConnection using MSN example in C#? - 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;"

Related

Connect with my local .mdf database (correct code)

I have to finalize a software with local database automatically installed when any one setup the final.exe program, but I cannot connect and design code with my local database.
class Class1
{
internal static string x = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=F:\Prog\Try\StrorPro_v1.3\StrorPro_v1.3\StoreProData_v1.2.mdf;Integrated Security=True;Connect Timeout=30";
}
using (SqlConnection cn = new SqlConnection(Class1.x)) {
cn.Open();
string cm = "select id from item_new_customer where cust='" + textBox2.Text +
"' order by id";
using (SqlCommand cmd = new SqlCommand(cm, cn)) {
...
Your connection string should look similar to the example below. It isn’t necessary to reference an MDF or other file. Data Source refers to the SQL Server instance where the database is hosted. Initial Catalog will be the database that all SQL statements without the USE keyword or a three part identifier, i.e. Database.Schema.Table, are sent to. When SSPI is used for Integrated Security the Windows credentials that are used to run the application will be used in the authentication process. True is equivalent to SSPI, however it’s recommended to use SSPI. To use SQL Server authentication specify a user ID and password for the User ID and Password properties, and set Integrated Security to false. For more details on additional connection string properties you can refer to the documentation here.
“Data Source=YourSQLServerInstance;Initial Catalog=YourDatabase;Integrated Security=SSPI;"

How do I get provider name to open Ole DB connection?

So I have a client program and a database on sql server that's on my pc (localdb). I'm trying to set connection on, but it seems like provider is wrong. My connection string looks like this:
connection.ConnectionString =
"Data Source=(localdb)\\Projects;" +
"Provider=MSIDXS;" +
"Initial Catalog=TermPaperWork;" +
"User id=DAZZBOURGH\\Dazzbourgh;" +
"Integrated Security=True;" +
"Password=;";
So everything is OK there, except provider. I don't get what it is and how to use it and how to know which provider suits my database.
How do I find out my provider for exact base?
I always use http://www.connectionstrings.com when I have questions on the format of a connection string.

C# Error in accessing an Access Database over LAN

Here's my connection string:
sConnection = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\" + lstNet.SelectedItem.ToString() + "\SharedDocs\Documents\Debug\App_File\ggbase.mdb;Jet OLEDB:Database Password=g3n3r4l;";
lstNet is a listbox that contains all computers found in the network.
I'm assuming something else is wrong with my connection string.
According to Connection Strings website, to access a database over LAN, the following connection string format is used:
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\\serverName\shareName\folder\myDatabase.mdb;User Id=admin;Password=;
I'm assuming that shareName is where my Connection String fails. What is a shareName? And what are the shareNames of Windows XP and Windows Vista / 7 if, say, I placed my database in their Shared Documents / Public Files?
I've tried modifying my connection string into the following:
\C$\Users\Public\Documents\Debug\App_File\ggbase.mdb;Jet OLEDB:Database Password=g3n3r4l;";
And I still get the following error:
"Format of Initialization string does not conform to specification"
May I have some help on this, please?
EDIT: Tried accessing the database in the Public\Documents section of a Windows Vista PC on my network with the following connection string:
\Public\Documents\Debug\App_File\ggbase.mdb;Jet OLEDB:Database Password=g3n3r4l;";
I also tried to access my own (Windows 7 PC) local Public\Documents section using the same connection string, since the serverName can be changed using the program.
Still nothing.
Try this:
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=\\serverName\shareName\folder\myDatabase.mdb; Jet OLEDB:Database Password=g3n3r4l;Persist Security Info=False;"
You must need to test first if you can access the shared path folder on client P.C. And if it can access it there will be no problem.Make sure also that the client user is administrator so it can do CRUD using you app.
Regards
Well, I actually solved it. Wow.
It turns out, there was an extra " at the very end of the connection string from the .ini file.
//Try This One...
string constr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=.\\PIS(ACU).mdb;";
Con = new OleDbConnection(#constr);
Con.Open();
Com = new OleDbCommand();
Com.Connection = Con;

Connection String Problem

I am a programmer trying to teach myself C#. I am trying to connect the the Northwind.mdf database in a form. I have used the Database Explorer to attach the database to the form, and the test connection button worked. For the connection string I am using "server=.\\sqlexpress; Trusted_Connection=yes; database=Northwind" This connection fails in SqlDataAdapter dataAdapter = new SqlDataAdapter(selectCommand, connectionString); Google has been no help. Any ideas?
Try this?
Data Source=.\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=SSPI;
Right click on the connection in the "Database Explorer".
Click on "Properties".
See the "Connection String" in the properties window, with its value on the right.
Is this the same connection string, as the one you posted?
Here is the connection string, I could see with a new mdf file I created
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\kalpesh\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True
Assuming that the connection string is correct, you will have to escape it in c# (if it contains any of the characters that requires it. for e.g. the backslash character)
string connectionstring = #"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\kalpesh\Documents\test.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
In your case, it should be the path to the Northwind.mdf located on your machine.
Does this work?

Provider needed to be specify on a connectionstring?

I have a very funny problem on my application, I get an error as follow:
System.ArgumentException: An OLE DB Provider was not specified in the ConnectionString. An example would be, 'Provider=SQLOLEDB;'.
However, when I tried to speicify the provider on my connection as Provider=SQLOLEDB.1 or Provider=SQLOLEDB, then I get another error saying invalid keyword 'Provider'.
But one thing I noticed, the computer that I am targeting to had 2 different database system, will that cause this error?
Any idea how to solve this problem?
Regards
Assuming that you are using ADO.NET, if you want to use distinct database systems, then you need to correct the DbConnection too, not only the connection string.
Note that you can't use an SqlConnection for OLEDB, you need to use System.Data.OleDb.OleDbConnection instead.
Looks like your missing some bits of the connection string - try these
General Connection String:
strConnect = _T("Provider=sqloledb;Data Source=MyServerName;"
"Initial Catalog=MyDatabaseName;"
"User Id=MyUsername;Password=MyPassword;");
Named Instance Connection String:
strConnect = _T("Provider=sqloledb;Data Source=MyServerName\MyInstanceName;"
"Initial Catalog=MyDatabaseName;User Id=MyUsername;Password=MyPassword;");
Trusted Security:
strConnect = _T("Provider=sqloledb;Data Source=MyServerName;"
"Initial Catalog=MyDatabaseName;"
"Integrated Security=SSPI;");
From here http://www.codeproject.com/KB/database/connectionstrings.aspx#OLE DB SqlServer

Categories

Resources