Sam Mettenmeyer

Sam Mettenmeyer

  • NA
  • 108
  • 10.6k

Connect to Microsoft SQL Server 2012 with WCF web service

Nov 20 2018 10:18 AM
I am trying to connect an Microsoft SQL Server 2012 database to an Xamarin.Android app.

As internet researches told me that it is not possible to connect directly to an external database in Xamarin and that I need a layer between my app and the database, I tried to implement that needed WebService with the help of
https://www.youtube.com/watch?v=xnO9MbferQo

Unfortunately, when I try to run (pressing F5 in Visual Studio) this web service, he opens the WCF-TestClient (as it should be), but after maybe after half of the progress bar ("is adding"), an error view is showed, saying:

Warnung: Es wurde kein Code generiert. Wenn Sie versucht haben, einen Client zu generieren, könnte die Ursache hierfür sein, dass die Metadatendokumente keine gültigen Verträge oder Dienste enthielten oder dass erkannt wurde, dass sich alle Verträge/Dienste in /reference-Assemblys befinden. Stellen Sie sicher, dass alle Metadatendokumente an das Tool übergeben wurden. Warnung: Wenn Datenverträge aus Schemas generiert werden sollen, müssen Sie sicherstellen, dass die Option /dataContractOnly verwendet wird.

Sorry for this German. I try to translate it for you:

Warning: No code has been generated. If you tried to generate a client, a possible reason for this could be, that the meta data documents do not contain valid contracts or services or that all contracts/services are lying in /reference-Assemblys. Please make sure, that all meta data documents are transfered to the tool.
Warning: If data contracts are generated from a schema, you have to make sure, that the option /dataContractOnly is used.

Please let me share my code:

Service1.svc.cs:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8. using System.Data;  
  9. using System.Data.SqlClient;  
  10.   
  11. namespace TestWcfService  
  12. {  
  13.     public class Service1 : IService1  
  14.     {  
  15.         // Connection string   
  16.         private readonly string conStr = "data source = [Server-IP]\\[instance_name]; initial catalog = [catalog_name]; user id = [dbusername]; password = [dbpw]; Connect Timeout = 60"";  
  17.   
  18.         public List<AdrSuche1> GetAdrSuche1()  
  19.         {  
  20.             List<AdrSuche1> adrList = new List<AdrSuche1>();  
  21.             SqlConnection con = new SqlConnection(conStr);  
  22.             con.Open();  
  23.             SqlCommand cmd = new SqlCommand("SELECT * FROM adressen", con)  
  24.             {  
  25.                 CommandType = CommandType.Text  
  26.             };  
  27.   
  28.             SqlDataReader reader = cmd.ExecuteReader();  
  29.   
  30.             while (reader.Read())  
  31.             {  
  32.                 AdrSuche1 adrSuche1 = new AdrSuche1  
  33.                 {  
  34.                     Adrsuche1 = reader["adrsuche1"].ToString()  
  35.                 };  
  36.   
  37.                 adrList.Add(adrSuche1);  
  38.             }  
  39.   
  40.             return adrList.ToList();  
  41.         }  
  42.     }  
  43.   
  44.   
  45.   
  46. }  
IService1.cs:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Runtime.Serialization;  
  5. using System.ServiceModel;  
  6. using System.ServiceModel.Web;  
  7. using System.Text;  
  8.   
  9. namespace TestWcfService  
  10. {  
  11.     public interface IService1  
  12.     {  
  13.   
  14.   
  15.         [OperationContract]  
  16.         [WebGet(UriTemplate = "GetAdrSuche1")]  
  17.         List<AdrSuche1> GetAdrSuche1();  
  18.     }  
  19.   
  20.     [DataContract]  
  21.     public class AdrSuche1  
  22.     {  
  23.         string adrsuche1;  
  24.   
  25.         [DataMember]  
  26.         public string Adrsuche1  
  27.         {  
  28.             get { return adrsuche1; }  
  29.             set { adrsuche1 = value; }  
  30.         }  
  31.     }  
  32.   
  33.   


What is the problem? Can anyone maybe help me integrating Web Api or WCF Service so I can read data from external database and display them on my app?

Best regards


Answers (4)