在ASP.NET中对SQL Compact 4.0执行/查询

时间:2010-09-30 16:46:27

标签: asp.net asp.net-mvc-2 sql-server-ce

我正在编写一个小的小实用程序MVC应用程序,我需要能够针对我的单表SQL Compact 4.0 .sdf文件执行即席查询以进行管理(Web Matrix不适合开发,它将无法在PC上运行,最终将继续运行)。使用实体框架代码优先,一切都运行正常,但是为了做一个临时查询,我想我需要按照我在EF之前的日子里的方式连接它(见下文)

            cn = new SqlConnection("Data Source=|DataDirectory|LocalScanData.sdf");
            SqlCommand cmd = new SqlCommand(query, cn);

            if (cn.State != ConnectionState.Open) cn.Open();

            if (query.ToUpper().StartsWith("INSERT") || query.ToUpper().StartsWith("UPDATE") || query.ToUpper().StartsWith("DELETE"))
            {
                TempData["RowsAffected"] = cmd.ExecuteNonQuery();
                return RedirectToAction("SQL");
            }
            else
            {
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();

                da.Fill(dt);

                return RedirectToAction("SQL", dt);
            }

但是当我尝试时,我得到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: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)。那么,问题是,我如何以老式的方式连接到SQL CE 4.0数据库?我也尝试过使用System.Data.SqlServerCe,但后来我发现错误让我相信只适用于CE 3.5数据库。

任何帮助?

1 个答案:

答案 0 :(得分:1)

它也发生在我身上。这是sdf ddbb的工作方式,你不能对它执行查询。您必须创建数据集,dataadapter等。

相关问题