I want connect to SQL Server Compact 4.0 in my ASP.NET application.
Here is example of code:
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();
}
But I have the next error: "A network-related or instance-specific error occurred while establishing a connection to SQL Server.The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allo开发者_如何转开发w remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
The SqlDataSource has constructor with three parameters, one of them is 'providerName' so, how to specify that I want definitely use Sql Server Compact provider ? Also I have added System.Data.SqlServerCe reference..
Try:
providerName = "System.Data.SqlServerCe.4.0"
Place the single database file (e.g. MySite.sdf
) in the App_Data
folder.
Add a connectionStrings
entry to web.config
to allow connection to the database:
web.config:
<configuration>
<connectionStrings>
<add name="db"
connectionString="Data Source=|DataDirectory|\MySite.sdf"
providerName="System.Data.SqlServerCe.4.0" />
</connectionStrings>
...
</configuration>
And then you can create a connection as desired through the connection string name
db:
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.
Have a look at the example here:
http://connectionstrings.com/sql-server-2005-ce
If you want to specify the location explicitly try:
Data Source=" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\MyDB.sdf;Persist Security Info=False;
You could pull the data into a datatable first:
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;
}
}
精彩评论