C# 从移动设备(Windows mobile 6.5)到sql server 2008的连接问题
我一直在互联网上搜索以下问题,但似乎没有答案 我正在VisualStudio2008中创建一个非常简单的移动应用程序。它应该连接到远程sql数据库以进行简单的读取。sql server是2008。物理设备在这里,因此我可以部署到它并在设备本身上运行(不使用emulator) 我使用的连接字符串是:(我尝试了不同的) 我修改了段落只是为了粘贴在这里 im用于连接数据库的实际代码为:C# 从移动设备(Windows mobile 6.5)到sql server 2008的连接问题,c#,sql-server,sql-server-2008,windows-mobile,C#,Sql Server,Sql Server 2008,Windows Mobile,我一直在互联网上搜索以下问题,但似乎没有答案 我正在VisualStudio2008中创建一个非常简单的移动应用程序。它应该连接到远程sql数据库以进行简单的读取。sql server是2008。物理设备在这里,因此我可以部署到它并在设备本身上运行(不使用emulator) 我使用的连接字符串是:(我尝试了不同的) 我修改了段落只是为了粘贴在这里 im用于连接数据库的实际代码为: SqlConnection sqlConnection1 = new SqlConnection("MyConnec
SqlConnection sqlConnection1 = new SqlConnection("MyConnectionString as above");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
string SKU = "";
cmd.Parameters.Add(new SqlParameter("@barcode", Barcode));
cmd.CommandText = "SELECT SKU, Quantity FROM Catalog.Barcodes WHERE Barcode = @barcode";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
while (reader.Read())
{
SKU = reader.GetString(0);
}
sqlConnection1.Close();
例外情况是在打开连接时:
System.Data.SqlClient.SqlConnection.OnError(SqlException异常,TdsParserState)System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,TdsParserState)System.Data.SqlClient.TdsParser.ThroweExceptionAndWarning()…System.Data.SqlClient.SqlInternetConnection.OpenAndLogin()
我确保system.data.sqlclient参考用于compact框架。
C:\Program Files(x86)\Microsoft SQL Server Compact Edition\v3.5\Devices\Client\System.Data.SqlClient.dll
代码也被复制到一个标准的winform中,并且可以正常工作。sql server是可远程访问的,因为它每天使用的凭据与我使用的凭据相同。该设备通过wifi连接到互联网,并通过网络浏览进行测试
希望我能充分注意到我到目前为止所做的事情和整个情况
那么这里有什么问题??我很困惑。在页面上放置一个SQL数据源控件,使用GUI可以连接到数据库并提取一些数据吗?如果是这样,您可以保存连接信息(web.config)并使用ConfigurationManager.ConnectionString从那里使用它
string connStr = ConfigurationManager.ConnectionStrings["mySavedConnectionStringName"].ConnectionString;
我认为你的连接设置错误。逐步改进设置:
try{
string connStr = "Data Source=192.168.0.2;Initial Catalog=myDataBase/myInstance;
Integrated Security=SSPI; User ID=myDomain\myUsername;Password=myPassword;";
//new connection
SqlConnection sqlConnection1 = new SqlConnection(connStr);
sqlConnection1.Open();
//new sqlCommand
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
string SKU = "";
cmd.Parameters.Add(new SqlParameter("@barcode", Barcode));
cmd.CommandText = "SELECT SKU, Quantity FROM Catalog.Barcodes WHERE Barcode = @barcode";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
//create reader
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
while (reader.Read())
{
SKU = reader.GetString(0);
}
sqlConnection1.Close();
}catch (Exception ex){
System.Diagnostics.Debug.WriteLine("Exception in sql code:" + ex.Message);
}
必须根据您的设置更改连接字符串。在连接打开之前,不要将连接指定给命令
连接字符串:请参见
我终于找到了答案。对我来说,这是一个奇怪的问题,因为如果没有它,我会认为它是可行的。问题在于连接字符串。服务器地址后缺少端口号1433。这似乎是.net compact framework的一个要求。Hi saj。我相信在这种情况下,连接线不会出现问题。我在其他地方读到过,它看起来像是sql server排序错误(我认为这很奇怪),或者是设备上没有安装sql server compact。但当从vs直接部署到设备时,应自动安装。在这个问题上丢失了。当您单步执行并附加了调试器时,在哪个代码行出现异常?我不确定是在连接处还是cmd处。从异常情况看,它似乎是用于命令的。可能缺少引号(取决于条形码字段类型)。只是为了找到真正的原因。顺便说一句:对于[Database],您必须使用sql server数据库实例名称。它看起来是连接而不是命令。有人知道有什么东西可能会阻塞连接吗?阻塞可能发生在设备上,而不是前面提到的sql server上
try{
string connStr = "Data Source=192.168.0.2;Initial Catalog=myDataBase/myInstance;
Integrated Security=SSPI; User ID=myDomain\myUsername;Password=myPassword;";
//new connection
SqlConnection sqlConnection1 = new SqlConnection(connStr);
sqlConnection1.Open();
//new sqlCommand
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
string SKU = "";
cmd.Parameters.Add(new SqlParameter("@barcode", Barcode));
cmd.CommandText = "SELECT SKU, Quantity FROM Catalog.Barcodes WHERE Barcode = @barcode";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
//create reader
reader = cmd.ExecuteReader();
// Data is accessible through the DataReader object here.
while (reader.Read())
{
SKU = reader.GetString(0);
}
sqlConnection1.Close();
}catch (Exception ex){
System.Diagnostics.Debug.WriteLine("Exception in sql code:" + ex.Message);
}
Trusted Connection from a CE device
A Windows CE device is most often not authenticated and logged in to a domain but it is
possible to use SSPI or trusted connection and authentication from a CE device using
this connection string.
Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;
User ID=myDomain\myUsername;Password=myPassword;