Hi everyone,
I have a database file that I want to access from C#. I want to be able to read from it, write to it and generally do SQLy things with it.
I have used SQL before, so I am not to worried about the actual language bit. What I can't get to work is the interface between C# and my file.
My code so far is:
using System.Data.SqlClient;
private void Connect_Click(object sender, EventArgs e){
string connect = "connection timeout=45" + ";" + "Data Source=|DataDirectory|\\CsTestDataBase.mdb"; SqlConnection myConnection = new SqlConnection(connect);myConnection.Open();
SqlCommand myCommand = new SqlCommand("INSERT INTO Info (MyDate, FileName) " + "Values ('string', 'C:\\Config.sys')", myConnection);myCommand.ExecuteNonQuery();
myConnection.Close();
}
I know that the timeout part of the connection string works, because it does control how long it takes to get an error back! The DataSource bit comes from my App.Config file. This also included:
Provider=Microsoft.Jet.OLEDB.4.0;, but this caused an error, so I removed it!!!!![]()
The code errors on myConnection.Open() with the following message.
<< An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified) >>
Can anyone out there tell me what I am doing wrong I have spent most of today trawling through the help system and the web, but can find nothing which fixes things.
Many thanks for being there.
John Woodiwiss

Talk to SQL database from C#
nzwpfnewbie
Hi,
It is all so easy when it is explained!
With the help form all the responses, I am on the way.
May I ask a secondary question. My Insert command is
insertString =
"Insert into Info(MyDate,Value) values ( '18/03/1952' , '123.45' )";I get a SYNTAX error, which I just can't understand.
I saw mention of using ( @18/03/1952 , @123.45 ), but this created the same error.
What am I doning wrong
John
jheddings
Hi,
Thanks for your help.
With this and everyone elses help, I am now on the way.
Many thanks
John
paraGOD
Hi,
Many thanks for your reply. With everyone's help, I have finally managed to talk to my database!
There is so much to learn!
Thanks
John
khamlon
You need to use OleDbConnection, OleDbCommand and so on. Sqlxxx is for SQL Server.
Do get a correct connectionstring: create an empty file with extension udl. E.g. myAccess.udl
Double click, choose provider for Access, and configure the connection. Click Test Connection, and if successfull, close the dialog. Open myAccess.udl in a text editor and you will get the connection string in that file.
--
SvenC
D&#225;maso
Alex_Petrovic
Denis Pitcher
the connection string for a SQL Server Express is like this:
data source=.\SQLExpress;Trusted_Connection=true;AttachDbFilename=path\file.mdf;database=databaseName
that is typically what it looks like. If you are connecting to a remote SQL Express, be sure to enable remote connections and broker service I believe