C# Error in accessing an Access Database over LAN - c#

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;

Related

Unable to load database from local server location in C# Connection String (windows application)

I'm trying to do application to access the database from local file server, but the connection string does not recognize the server location. This is a windows form application, using sqlite. Kindly help me on this one.
File server location will be like this:
\\fileserver\Testdb\maindb.db
Code used:
string server_database_path = #"\\fileserver\Testdb\maindb.db";
string connection_data = "Data Source=" + server_database_path ;
using (var conn = new SQLiteConnection(connection_data))
{
conn.Open();
SQLiteCommand insert_Rec = new SQLiteCommand(query_text, conn);
insert_Rec.ExecuteNonQuery();
conn.Close();
}
Error:
Unable to open database file
I may be wrong but I dont think that directly specifying the .db is correct. When using a normal SQL Server I would specify the instance (or just the server hosting it if it was the default instance).
So, your connection string should look something like
string connectionString = "Data Source=192.168.0.1; User ID=administrator; Password=YOURPASSWORD"
or if you are connecting the machine you are on it should be
string connectionString = "Data Source=127.0.0.1; User ID=administrator; Password=YOURPASSWORD"
You could substitute the 127.0.0.1 for \\localhost
I was confused in doing this, but by changing the slash "\" to "/" it really worked.
When i changed the slash in the path string it started to work fine and everything goes well.
Example: #"//fileserver/Testdb/maindb.db"

Using Oledb connection string for Localhost c#

When use Oledb c# connection
I noticed that a lot connection using file.
But how to connect to localhost using oledb?
I created database and tables using Microsoft SQL Server Management that connect with SQL Express and using window authentication
When using this function i don't know how should convert to connect to localhost
//Want the connString to connect localhost instead of file
public static string connString = #"Provider=Microsoft.JET.OLEDB.4.0;data source=" + Path + "\\database\\errDB.mdb";
public static OleDbConnection connection;
public myFunction()
{
string sqlString = "SELECT name,contact,accessLevel,Crudential_ID FROM errors where Crudential_ID =#ID";
connection = new OleDbConnection(connString);
OleDbCommand command = new OleDbCommand(sqlString, connection);
//Open connection
connection.Open();
command.Parameters.Add("#ID", OleDbType.VarChar);
command.Parameters["#ID"].Value = "test";
//Read from database
OleDbDataReader reader = command.ExecuteReader();
if(reader.HasRows)
{
.....
}
connection.Close();
}
connectionstrings.com - true to its name - is indispensable when you frequently need to construct connection strings. For your specific case, this would be the relevant section.
Based on that, your connection string should look something like this:
Provider=SQLNCLI11;Server=.\SQLEXPRESS;Database=SOMEDATABASE;Trusted_Connection=yes;
To break it down:
SQLNCLI11 is the SQL Native Client OLEDB provider. You can see available providers in SQL Management Studio, under Server Objects > Linked Servers > Providers.
.\SQLEXPRESS is your servername and instance. The . is shorthand for localhost (you can also use localhost if you prefer), and SQLEXPRESS is the default instance name that SQL Express installs under.
SOMEDATABASE - whatever your database name is.
Trusted_Connection=yes - Use windows authentication. Sometime you see it as Integrated Security=SSPI. They are one and the same.
If you are using SQL Express then I would suggest using a System.Data.SqlClient.SqlConnection object to make your connection. You will only need your server name to connect.
Server=ServerName\SQLEXPRESS;Database=Blah;User ID=user;Password=pw

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.

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;"

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