简体   繁体   English

在Windows CE sqlconnection上使用C#的无效连接

[英]Invalid connection using C# on windows ce sqlconnection

I am creating a Smart Device project using VS2008 for a WIndows CE 6.0 device with .Net Compact Framework 3.5 我正在使用VS2008为带有.Net Compact Framework 3.5WIndows CE 6.0设备创建智能设备项目

Here is my code: 这是我的代码:

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

            }
        }

Mind that usernames ant table names are changed but I am getting the following errors on command.Connection.Open(); 请注意,用户名和表名已更改,但是在command.Connection.Open();上出现以下错误command.Connection.Open();

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

Why and how to fix it? 为什么以及如何解决? The device easly pings the server machine if that is useful. 如果有用,设备将轻松ping服务器服务器。

at place of command.Connection.Open(); 在command.Connection.Open()的位置; use connection.open() and after execute query close the connection... 使用connection.open()并在执行查询后关闭连接...

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

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

相关问题 Windows CE 5.0使用C#以编程方式进行GPRS连接? - Making GPRS Connection programmatically using C# for windows CE 5.0? C# Windows 窗体:不支持 SqlConnection 关键字,连接超时 - C# Windows Forms: SqlConnection keyword not supported, connection timeout 使用 C# 在 Windows CE 6.0 中使用 USB HID 设备 - Consuming a USB HID device in Windows CE 6.0 using C# 使用C#将文件复制到Windows CE设备 - Copying file to windows ce device using c# 使用C#强制关闭Windows CE 5应用程序 - Force close Windows CE 5 app using C# 在 using() 语句中使用它之前声明的 SqlConnection() 完成后是否仍会关闭连接? (C#/SQL服务器) - Will a SqlConnection() declared before using it in a using() statement still close the connection when done? (C#/SQL Server) 连接泄漏 (C#/ADO.NET) 即使使用使用创建的 SqlConnection - Connection leak (C#/ADO.NET) even though SqlConnection created with using C#使用一个SqlConnection进行多个查询 - C# Using one SqlConnection for multiple queries C#using语句,SQL和SqlConnection - The C# using statement, SQL, and SqlConnection C#:连接错误在Parallel.ForEach中打开SqlConnection - C#: Connection Errors opening SqlConnection inside Parallel.ForEach
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM