2011-02-02 17 views
10

Desidero connettermi a SQL Server Compact 4.0 nella mia applicazione ASP.NET.Come connettersi a SQL Server Compact 4.0 in ASP.NET?

Ecco esempio di codice:

protected void Page_Load(object sender, EventArgs e) 
{ 
    string connStr = "Data Source=D:\\MyDB.sdf;"; 
    string sqlStr = "select * from tblMyTable"; 

    var sqlDataSrc = new SqlDataSource(connStr, sqlStr); 

    GridWithUrls.DataSource = sqlDataSrc; 
    GridWithUrls.DataBind(); 
} 

ma ho l'errore successivo: "Si è verificato un errore relativo alla rete o specifica dell'istanza mentre stabilisce una connessione al server SQL server.The non è stato trovato o non era accessibile Verificare che il nome dell'istanza sia corretto e che SQL Server è configurato per consentire le connessioni remote.. (provider: SQL interfacce di rete, errore: 26 - errore del server/dell'istanza specificata)"

lo SqlDataSource ha costruttore con tre parametri, uno di questi è 'providerName' quindi, come specificare che I vuoi sicuramente usare il provider Sql Server Compact? Anche io ho aggiunto riferimento System.Data.SqlServerCe ..

risposta

22

Prova:

providerName = "System.Data.SqlServerCe.4.0" 
+0

grazie che ha senso – Yara

0

Dai un'occhiata alla nell'esempio qui:

http://connectionstrings.com/sql-server-2005-ce

Se si desidera specificare la posizione provare esplicitamente:

Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyDB.sdf;Persist Security Info=False; 
0

Si potrebbe tirare dati in un datatable prima:

 using (SqlCeConnection c = new SqlCeConnection(
      Properties.Settings.Default.DataConnectionString)) 
     { 
      c.Open(); 

      // Create new DataAdapter 
      using (SqlCeDataAdapter a = new SqlCeDataAdapter(
       "SELECT * FROM tblMyTable", c)) 
      { 

       // Use DataAdapter to fill DataTable 
       DataTable t = new DataTable(); 
       a.Fill(t); 

       // Render data onto the screen 
       dataGridView1.DataSource = t; 
      } 
     } 
2

Posizionare il file di database singolo (ad es. MySite.sdf) nella cartella App_Data.

aggiungere una voce connectionStrings-web.config per consentire la connessione al database:

web.config:

<configuration> 
    <connectionStrings> 
     <add name="db" 
      connectionString="Data Source=|DataDirectory|\MySite.sdf" 
      providerName="System.Data.SqlServerCe.4.0" /> 
    </connectionStrings> 
    ... 
</configuration> 

e quindi è possibile creare una connessione, se lo desideri attraverso la stringa di connessione namedb:

public static DbConnection CreateConnection() 
{ 
    //Get connection string named "db" 
    String csName = "db"; 
    ConnectionStringSettings cs = ConfigurationManager.ConnectionStrings[csName]; 
    if (cs == null) 
     throw new ConfigurationErrorsException("Could not find connection string \"" + csName + "\""); 

    //Get a factory based on the "ProviderName" in the connection string 
    DbProviderFactory factory = DbProviderFactories.GetFactory(cs.ProviderName); 
    if (factory == null) 
     throw new Exception("Unable to locate factory for " + cs.ProviderName); 

    //Have the factory create us a connection object 
    DbConnection conn = factory.CreateConnection(); 

    //Open the connection with the connection string from web.config 
    conn.ConnectionString = cs.ConnectionString; 
    conn.Open(); 

    //Give the ready connection to the user 
    return conn; 
} 

Note: Any code is released into the public domain. No attribution required.

Problemi correlati