0

I want to create a empty local database inside my project's folder and I've error:

An unhandled exception of type 'System.Data.SqlServerCe.SqlCeException' occurred in System.Data.SqlServerCe.dll

on the line

conn.Open();

I'm lost with what I need to do. Any idea what is causing this? I tried a few solutions - this one of them...

SqlCeConnection conn = null;

try
{
    conn = new SqlCeConnection("Data Source = bd-avalia.sdf; Password ='<asdasd>'");
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "create table cliente(id_cliente int identity not null primary key, nome varchar not null, password not null int)";
    cmd.ExecuteNonQuery();
}
finally
{
    conn.Close();
}

the error: enter image description here

7
  • 2
    Try the password without the angled brackets or quotes and see if you fare any better Commented Aug 7, 2013 at 15:35
  • when creating the table you also set password to be an int...I'm assuming you would want it to be a varchar Commented Aug 7, 2013 at 15:38
  • 2
    Does the file bd-avalia.sdf really exist in the current run directory? If you're running this inside Visual Studio, the app is run inside the (project-directory)\bin\debug folder - is that .sdf file available there? Try using a full, complete path - does that work? Commented Aug 7, 2013 at 15:43
  • Why dont you declare the conection in the web.config then just call it in your sql connection? Commented Aug 7, 2013 at 15:43
  • Is the file bd-avalia.sdf a part of your project? The connection string to data in your project is "Data Source=|DataDirectory|\bd-avalia.sdf; ...". Commented Aug 7, 2013 at 16:09

3 Answers 3

1

If you debug into the code (or output it somehow) you can check the NativeError property of the SqlCeException, which should tell you the cause of the exception.

See http://technet.microsoft.com/en-us/library/aa237948(v=sql.80).aspx for the explanation of the code.

Sign up to request clarification or add additional context in comments.

Comments

0

Change the cmd.CommandText as follows:

SqlCeConnection conn = null;

try
{
    conn = new SqlCeConnection("Data Source = bd-avalia.sdf; Password ='<asdasd>'");
    conn.Open();

    SqlCeCommand cmd = conn.CreateCommand();
    cmd.CommandText = "CREATE TABLE dbo.[cliente] ([id_cliente] [int] IDENTITY(1,1)  NOT NULL PRIMARY KEY, 
                       nome [varchar] NOT NULL, password [int] NOT NULL)";

    cmd.ExecuteNonQuery();
}
finally
{
    conn.Close();
}

1 Comment

Welcome to StackOverflow: if you post code, XML or data samples, please highlight those lines in the text editor and click on the "code samples" button ( { } ) on the editor toolbar to nicely format and syntax highlight it!
0

Change the cmd.CommandText as follows:

"CREATE TABLE dbo.[cliente] ([id_cliente] [int] IDENTITY(1,1) NOT NULL PRIMARY KEY, nome [varchar] NOT NULL, password [int] NOT NULL)";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.