samedi 27 juin 2015

Error in connecting Local Database (.sdf)

//SqlCeConnection con = new SqlCeConnection( "Data Source="
  + System.IO.Path.Combine( Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location ), "DBThree.sdf" ) );

//SqlCeConnection con = new SqlCeConnection( @"Data Source=|DataDirectory|\DBThree.sdf" );

//SqlCeConnection con = new SqlCeConnection( ConfigurationManager.ConnectionStrings["DBThreeConnectionString"].ConnectionString );

SqlCeConnection con = new SqlCeConnection( @"Data Source=""C:\Users\Visual Studio 2012\Projects\ForTesting2\ForTesting2\DBThree.sdf""" ); 
con.Open();
string comnd = "SELECT COUNT (*) FROM Mytable";
SqlCeCommand com1 = new SqlCeCommand( comnd, con );

string test1 = com1.ExecuteScalar().ToString() ;
label1.Text = "AAA\t" + test1;
con.Close();

I am getting the following error:

System.Data.SqlServerCe.SqlCeException (0x80004005): There was an error parsing the query. [ Token line number = 1,Token line offset = 23,Token in error = Mytable ]

I also tried the connection strings which are commented in above code. The connection string in App.config looks like

<connectionStrings>
    <add name="DBThreeConnectionString"
        connectionString="Data Source=|DataDirectory|\DBThree.sdf"
        providerName="Microsoft.SqlServerCe.Client.4.0" />
</connectionStrings>

Aucun commentaire:

Enregistrer un commentaire