Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How connect to SQL Server Compact 4.0 in ASP.NET?

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 allow 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..

like image 370
Yara Avatar asked Feb 02 '11 12:02

Yara


People also ask

Can ASP Net connect to SQL Server?

ASP.NET Database Connection can connect with most databases, including Oracle, Microsoft SQL Server, MongoDB, and MySQL. Here we are going to use Microsoft SQL Server as our database. Microsoft SQL Server is free database software, and one can easily download and install Microsoft SQL Server Express Edition.

How do I connect to an existing SQL database in asp net?

Select >new Project from web option use ASP.NET empty Web Application. After loading project, select click Tools at top then select connect to Database option. Add connection dialogue box will appear click at change option and select the Microsoft SQL Server. Now enter your server name.

How does .NET connect to SQL Server?

Right-click Data Connections, and then click Add connection. In the Data Link Properties dialog box, click localhost in the Select or enter a server name box. Click Windows NT Integrated Security to log on to the server. Click Select the database on the server, and then select Northwind database from the list.


1 Answers

Try:

providerName = "System.Data.SqlServerCe.4.0"
like image 109
onof Avatar answered Sep 22 '22 14:09

onof