【问题标题】:Invalid connection using C# on windows ce sqlconnection在 Windows ce sqlconnection 上使用 C# 的无效连接
【发布时间】:2016-04-15 20:49:54
【问题描述】:

我正在使用VS2008 为带有.Net Compact Framework 3.5WIndows CE 6.0 设备创建一个智能设备项目

这是我的代码:

        string queryString = "SELECT id, name, insert_date, activity FROM dbo.[my Table]";
        StringBuilder errorMessages = new StringBuilder();

        using (SqlConnection connection = new SqlConnection("Data Source=dba;Initial Catalog=myDb;Persist Security Info=True;User ID=usern;Password=paswd"))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            try
            {
                connection.Open();
                //command.Connection.Open();
                //command.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                for (int i = 0; i < ex.Errors.Count; i++)
                {
                    errorMessages.Append("Index #" + i + "\n" +
                        "Message: " + ex.Errors[i].Message + "\n" +
                        "LineNumber: " + ex.Errors[i].LineNumber + "\n" +
                        "Source: " + ex.Errors[i].Source + "\n" +
                        "Procedure: " + ex.Errors[i].Procedure + "\n");
                }
                Debug.WriteLine(errorMessages.ToString());

            }
        }

请注意用户名 ant 表名称已更改,但我在 command.Connection.Open(); 上收到以下错误

Index #0
Message: Invalid connection.
LineNumber: 0
Source: .Net SqlClient Data Provider
Procedure: ConnectionOpen (Invalid Instance()).

为什么以及如何解决它?如果有用,设备会轻松 ping 服务器计算机。

【问题讨论】:

  • 有人遇到同样的问题吗?

标签: c# visual-studio-2008 windows-ce


【解决方案1】:

在 command.Connection.Open(); 使用 connection.open() 并在执行查询后 关闭连接...

【讨论】:

  • 试过了,但问题仍然存在于 windows CE 设备上,但如果我创建一个 Windows 窗体应用程序就可以了。(计算机和智能设备都在同一个网络中)
【解决方案2】:

是的,在创建命令对象之前打开连接,您不需要关闭连接,因为它在 using{}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 2018-07-14
    • 1970-01-01
    • 2021-12-29
    • 2021-10-16
    相关资源
    最近更新 更多