简体   繁体   English

如何在ASP.NET中连接到SQL Server Compact 4.0?

[英]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. 我想在我的ASP.NET应用程序中连接到SQL Server Compact 4.0。

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)" 但我有下一个错误:“建立与SQL Server的连接时发生网络相关或特定于实例的错误。找不到服务器或无法访问服务器。验证实例名称是否正确以及SQL Server是否配置为允许远程连接。(提供程序:SQL网络接口,错误:26 - 错误定位指定的服务器/实例)“

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 ? SqlDataSource有三个参数的构造函数,其中一个是'providerName'所以,如何指定我想要使用Sql Server Compact提供程序? Also I have added System.Data.SqlServerCe reference.. 另外我添加了System.Data.SqlServerCe引用..

尝试:

providerName = "System.Data.SqlServerCe.4.0"

Place the single database file (eg MySite.sdf ) in the App_Data folder. 将单个数据库文件(例如MySite.sdf )放在App_Data文件夹中。

Add a connectionStrings entry to web.config to allow connection to the database: connectionStrings条目添加到web.config以允许连接到数据库:

web.config : 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 : 然后,您可以根据需要通过连接字符串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 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;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM