无法连接到SQL Server CE数据库 [英] Can't connect to SQL Server CE database

查看:89
本文介绍了无法连接到SQL Server CE数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码,只是为了测试连接:

I have the following code, just to test connection:

public void Test()
{
    SqlCeConnection conn = new SqlCeConnection(@"Data Source=/Application/Database.sdf;");
    try
    {
        conn.Open();
        label1.text = "Connection!";
    }
    catch (Exception ee)
    {
        label1.text = "No connection!";
    }
}

当尝试连接到该数据库时,应用程序在 conn.Open()上抛出

When trying to connect to this database, the application throws an exception at conn.Open() saying

SqlCeException未处理

SqlCeException was unhandled

,仅此而已.异常消息为空,所以我很难找出问题出在哪里.

and nothing more. The exception message is blank, so I'm having a hard time figuring out what went wrong.

数据库文件在那里,应用程序使用

The database file is there, and the application returns true with

File.Exist(@"/Application/Database.sdf");

因此它确实有权访问该文件.

so it does have access to the file.

我可能在这里做错了什么,有人可以帮我吗?

我在Windows CE 5上使用Compact Framework 2.0,并且该应用程序是现有的应用程序.我正在尝试向其中添加数据库,以便可以更轻松地加载大量数据.

I'm using Compact Framework 2.0 on Windows CE 5, and the application in question is an existing one. I'm trying to add a database to it so I can load large amounts of data much more easier.

推荐答案

Erik所说的是将您的代码更改为此:

What Erik is saying is change your code to this:

public void Test()
{
  SqlCeConnection conn = new SqlCeConnection(@"Data Source=/Application/Database.sdf;");
  try
  {
    conn.Open();
    label1.text = "Connection!";
  }
  catch (SqlCeException ee)  // <- Notice the use of SqlCeException to read your errors
  {
    SqlCeErrorCollection errorCollection = ee.Errors;

    StringBuilder bld = new StringBuilder();
    Exception inner = ee.InnerException;

    if (null != inner) 
    {
      MessageBox.Show("Inner Exception: " + inner.ToString());
    }
    // Enumerate the errors to a message box.
    foreach (SqlCeError err in errorCollection) 
    {
      bld.Append("\n Error Code: " + err.HResult.ToString("X")); 
      bld.Append("\n Message   : " + err.Message);
      bld.Append("\n Minor Err.: " + err.NativeError);
      bld.Append("\n Source    : " + err.Source);

      // Enumerate each numeric parameter for the error.
      foreach (int numPar in err.NumericErrorParameters) 
      {
        if (0 != numPar) bld.Append("\n Num. Par. : " + numPar);
      }

      // Enumerate each string parameter for the error.
      foreach (string errPar in err.ErrorParameters) 
      {
        if (String.Empty != errPar) bld.Append("\n Err. Par. : " + errPar);
      }

    }
    label1.text = bld.ToString();
    bld.Remove(0, bld.Length);
  }
}

您现在捕获的通用 Exception 无法提供 SqlCeException 的详细信息.

The generic Exception you are catching right now can not give you the details of the SqlCeException.

这篇关于无法连接到SQL Server CE数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆