2014-12-04 80 views
1

我尝试使用Windows Phone 8.1操作系统连接到移动设备上的MySQL数据库。 我使用WCF创建连接的(只是用于测试):在Windows Phone 8.1中连接到MySQL

public class Service1 : IService1 
{ 

    public string PobierzHandlowca() 
    { 
     String sql = "SELECT nazwa FROM Handlowcy WHERE id_handlowcy=1"; 
     MySqlConnection connection = new MySqlConnection("Server=localhost; Database=msoh; Uid=root; Pwd=;"); 

     try 
     { 
      connection.Open(); 
      MySqlCommand command = new MySqlCommand(sql, connection); 
      MySqlDataReader reader = command.ExecuteReader(); 

      while (reader.Read()) 
      { 
       return (string)reader["nazwa"]; 
      } 
     } 
     catch { } 
     finally 
     { 
      connection.Dispose(); 
     } 

     return null; 
    } 
} 

然后在我的Windows Phone Silverlight应用程序我添加一个服务引用。然后,我只是单纯地做到这一点:

public partial class MainPage : PhoneApplicationPage 
{ 
    ServiceReference1.Service1Client proxy; 


    public MainPage() 
    { 
     InitializeComponent(); 

     proxy = new Service1Client(); 
     proxy.PobierzHandlowcaCompleted += proxy_PobierzHandlowcaCompleted;   
    } 


    void proxy_PobierzHandlowcaCompleted(object sender, PobierzHandlowcaCompletedEventArgs e) 
    { 
     if (e.Result != null) 
     { 
      tbHandlowiec.Text = e.Result; // tbHandlowiec is a TextBox control 
     } 
     else 
     { 
      tbHandlowiec.Text = "Error!"; 
     } 
    } 

    // button 
    private void btnPobierzHandlowca_Click(object sender, RoutedEventArgs e) 
    { 
     proxy.PobierzHandlowcaAsync(); 
    } 
} 

当我点击该键过了一会儿,我得到这个错误 - 型“System.ServiceModel.CommunicationException”发生在System.ServiceModel.ni.dll的例外但在用户代码

处理的什么是错的代码?我错过了什么?

+1

绝对不是一个好主意,有一个空的catch块。如果服务器上出现故障,您无法知道发生了什么问题。 – Steve 2014-12-04 23:51:40

+0

定义一段时间,如果时间足够长,可能会暂停一段时间,这意味着您实际上并没有实现自己的服务。 – 2014-12-04 23:55:40

+0

@ Mr.B - 大约5秒钟。 – XardasLord 2014-12-04 23:57:17

回答

0

好的我找到了一个解决方案。

我已经在一个Local IIS创建一个WCF服务:

enter image description here

A也必须安装.NET一些,HTTP和IIS在打开功能上或关闭Windows功能(控制面板 - >节目功能)。现在

我可以从位于我的电脑上的数据库得到我的Windows Phone的数据。