English 中文(简体)
• 如何与伙伴关系中的服务器契约4.0连接。 NET?
原标题:How connect to SQL Server Compact 4.0 in ASP.NET?

我想与我的伙伴关系中的服务器契约4.0联系起来。 NET应用软件。

守则的例子如下:

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

最佳回答

Try:

providerName = "System.Data.SqlServerCe.4.0"
问题回答

将单一数据库文档(例如MySite.sdf)列入App_Data

增加<条码>连接<条码>进入<条码>。

web.config:

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

之后,你可以通过显示<条码>的链接(<><>>>><>条码>查询<>db>/strong>:

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;
}

<>:任何法规均公布于公共领域。 不需要归属。

举一个例子:

http://linkionstrings.com/sql-server-2005-ce”rel=“nofollow> http://linkionstrings.com/sql-server-2005-ce

如果你想明确指明地点:

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

您可以首先将数据输入数据表:

        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;
            }
        }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签