简体   繁体   English

无法连接到SQL Server CE数据库

[英]Can't connect to SQL Server CE database

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

When trying to connect to this database, the application throws an exception at conn.Open() saying 当尝试连接到该数据库时,应用程序在conn.Open()引发异常

SqlCeException was unhandled SqlCeException未处理

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 数据库文件在那里,应用程序返回true

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

so it does have access to the file. 因此它确实有权访问该文件。

I'm probably doing something really wrong here, can anyone help me out with this? 我可能在这里做错了什么,有人可以帮我吗?

I'm using Compact Framework 2.0 on Windows CE 5, and the application in question is an existing one. 我在Windows CE 5上使用Compact Framework 2.0,并且该应用程序是现有的应用程序。 I'm trying to add a database to it so I can load large amounts of data much more easier. 我正在尝试向其中添加数据库,以便可以更轻松地加载大量数据。

What Erik is saying is change your code to this: Erik的意思是将您的代码更改为此:

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

The generic Exception you are catching right now can not give you the details of the SqlCeException . 您现在正在捕获的通用Exception无法提供SqlCeException的详细信息。

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

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