Can not seem to get Csharp to talk to the sql compact 3.5 database

M

Mike

Hi I am new to csharp. I am trying to get my Csharp windows app to connect
to the sql compact database.
I am using Visual Csharp Express 2008 and I created the database using the
Csharp express IDE. It is in the project folder. I created the tables and
added elements to the table manually with the IDE. So the IDE seems to
connect to the database. I can also contact the database using the SQL
Server management studio. When I created the database I had the connection
string placed in the config.app file. A string does appear to exist there.

When I run the app. I can not seem to get a connection to the data base. I
get some kind of error29
Any idea how to write a connection string to connect to a database that is
in the project directory? I would like to do this two ways. First manually
entering the string into the connection object and second by grabbing it out
of the configuration file.

For the first method I have tried a lot of connection strings that I found
by searhing on line. I also tried copying and pasting the string out of the
config.app file.

sqlconnection conn = new sqlconnection();
conn.connectionstring = "string";
conn.open();

However, I still get the same error 29 connection problem. I would also like
to try bringing the string out of the config.app file, but I am not sure
how.

Any suggestions?
Thanks,
mike
 
A

Arne Vajhøj

Hi I am new to csharp. I am trying to get my Csharp windows app to connect
to the sql compact database.
I am using Visual Csharp Express 2008 and I created the database using the
Csharp express IDE. It is in the project folder. I created the tables and
added elements to the table manually with the IDE. So the IDE seems to
connect to the database. I can also contact the database using the SQL
Server management studio. When I created the database I had the connection
string placed in the config.app file. A string does appear to exist there.

When I run the app. I can not seem to get a connection to the data base. I
get some kind of error29
Any idea how to write a connection string to connect to a database that is
in the project directory? I would like to do this two ways. First manually
entering the string into the connection object and second by grabbing it out
of the configuration file.

For the first method I have tried a lot of connection strings that I found
by searhing on line. I also tried copying and pasting the string out of the
config.app file.

sqlconnection conn = new sqlconnection();
conn.connectionstring = "string";
conn.open();

However, I still get the same error 29 connection problem. I would also like
to try bringing the string out of the config.app file, but I am not sure
how.

C# is case sensitive.

sqlconnection conn = new sqlconnection();
conn.connectionstring = "string";
conn.open();

does not compile.

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "string";
conn.Open();

should compile.

But it connects to an SQLServer database - not to a SQLServer CE
database.

SqlCeConnection conn = new SqlCeConnection();
conn.ConnectionString = "string";
conn.Open();

can connecte to a SQLServer CE database.

Where the connection string can look like:
"Data Source=foobar.sdf; Password=xxxxx"

Arne
 
M

Mike

Thanks Mark,

I sould have given you the whole error message. I was also being sloppy with
case in my email.
You are correct I had the wrong namespace and class.

SqlCeConnection conn = new SqlCeConnection();
conn.ConnectionString = "string";
conn.Open();

worked perfect.

I let the Csharp Expess IDE place the string in the app.config filewhen i
created the database, so it looks like your example. I will try that
technique next.

I looked at the string:

Data Source=MyData.sdf;Persist Security Info=False;

For "Data Source" I set it equal to the database.sdf file name this works. I
have vo password.
It seems to work without "Persist Security Info=False" . Do I need or sould
I use this?

Thanks
Mike




Mark Rae said:
When I run the app. I can not seem to get a connection to the data base.
I get some kind of error29

This is a technical peer-to-peer newsgroup. If you've got a problem for
which there is an error message, it REALLY HELPS everybody if you describe
specifically what that error is - copy and paste would be ideal...

sqlconnection conn = new sqlconnection();
conn.connectionstring = "string";
conn.open();

That's never going to work.

1) You're trying to use the SQL Server provider to connect to SQL Compact
Edition - you need the SqlCe provider:
http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceconnection.aspx

2) C# is case-sensitive - e.g. it's ConnectionString, not
connectionstring:
http://msdn.microsoft.com/en-us/lib...nt.sqlconnection.connectionstring(VS.71).aspx

3) What exactly is "string" above? As you've written it, that is telling
ADO.NET to connect to a database using the string literal "string" as a
connection string. Connection strings for SqlCe look like this:
http://connectionstrings.com/sql-server-2005-ce

I would also like to try bringing the string out of the config.app file,
but I am not sure how.

Assuming you've placed the connection string in the default location in
the config file e.g.
<configuration>
<connectionStrings>
<add name="SqlCeConnectionString" connectionString="xxxx"
providerName="System.Data.SqlServerCe.3.5" />
</connectionStrings>
</configuration>

(replace xxxx with real connection string, obviously!)

Then the code to retrieve it is:
string strConnectionString =
System.Configuration.ConfigurationManager.ConnectionStrings["SqlCeConnectionString"].ConnectionString;
 
M

Mike

Arne Vajhøj said:
C# is case sensitive.

sqlconnection conn = new sqlconnection();
conn.connectionstring = "string";
conn.open();

does not compile.

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "string";
conn.Open();

should compile.

But it connects to an SQLServer database - not to a SQLServer CE
database.

SqlCeConnection conn = new SqlCeConnection();
conn.ConnectionString = "string";
conn.Open();

can connecte to a SQLServer CE database.

Where the connection string can look like:
"Data Source=foobar.sdf; Password=xxxxx"

Arne

Hi Arne
C# is case sensitive.
sorry I was being sloppy when I wrote the email. I am indeed using the
correct case and it does compile.
It just did not make the connection.
But it connects to an SQLServer database - not to a SQLServer CE
database.

SqlCeConnection conn = new SqlCeConnection();
conn.ConnectionString = "string";
conn.Open();

Perfect! Works Great! Always helps to use the correct Classes and Namespaces
:)

Thanks,
Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top