从移动设备(Windows mobile 6.5)连接到sql server 2008的问题

本文关键字:sql 连接 server 2008 问题 移动 Windows mobile | 更新日期: 2023-09-27 17:51:09

我一直在互联网上到处搜索以下问题,但似乎没有答案。

我正在Visual Studio 2008中创建一个非常简单的移动应用程序。它应该连接到远程sql数据库来执行简单的读取操作。sql server是2008。物理设备在这里,所以我可以部署到它并在设备本身上运行(不使用模拟器)。

我使用的连接字符串是:(我试过不同的)

Data Source=[ServerIP];Initial Catalog = [DatabaseName]; User ID = [ID]; Password = [Password];

我修改了段落,只是为了粘贴在这里。

连接数据库的实际代码是:

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。在System.Data.SqlClient.SqlInternalConnection上OnError(sqllexception异常,TdsParserState状态)。OnError(sqlException exception, TdsParserState state) at system . data . sqlclient . tdsparserer . throwexceptionandwarning ().....System.Data.SqlClient.SqlInternatlConnection.OpenAndLogin().....

我确保system.data.sqlclient引用是针对compact框架的。C:'Program Files (x86)'Microsoft SQL Server精简版'v3.5'Devices'Client'System.Data.SqlClient.dll

代码也被复制到一个标准的winform中,效果很好。sql server可以远程访问,因为它每天都使用与我使用的相同的凭据。该设备通过wifi连接到互联网,并通过网页浏览进行测试。

希望我已经把到目前为止所做的事情和全部情况都记下来了。

那么这里的问题是什么??困惑我。

从移动设备(Windows mobile 6.5)连接到sql server 2008的问题

我终于找到了答案。对我来说,这是一个奇怪的,因为我认为没有它也可以工作。问题出在连接字符串上。我在服务器地址后面缺少了端口号1433。这似乎是。net紧凑框架的要求。

将SQL数据源控件放到页面上,使用GUI可以连接到DB并拉回一些数据吗?如果是这样,您可以保存连接信息(web.config)并使用ConfigurationManager从那里使用它。ConnectionStrings

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

连接字符串必须更改为您的设置。在打开连接之前,不要将连接分配给命令。

连接字符串:参见https://www.connectionstrings.com/sql-server-2008/

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;