[英]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.5
的WIndows 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.