locked
Connect to a databse in windows CE RRS feed

  • Question

  • Hello,

    I need to develop a application on Windows CE .net frame work 2.0 (Language is C#) . This application needs to be connected with a database (either SQl Server or MY SQL). The device which I user to run the application has Wi-Fi and I'm supposed to use Wi-Fi to connect with the database. This is the very first time I'm dealing with windows CE and please let me know how can I achieve this goal in a better way.

    1. What is the initial code I need to develop to connect to the database ?

    2. What are the best practices that I need to follow when accessing a database using a windows CE application (In a mobile device) ?

     

    Thanks

    Shanaka

    Sunday, September 5, 2010 5:50 AM

Answers

  • Hi Shanaka,

    It is like desktop, we need sqlConnection to connect to a remote device via its IP or computer name, then use SqlDataAdapter to get the data to fill a dataset. Here is FAQ:
    How do I connect to a desktop SQL Server database? 

    For wi-fi, it is just a connection path, if device doesn't connection to the database server af first, we can connect WIFI programmatically:
    http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/2c30d5f0-0921-483f-b813-0ddf6adf9bd0

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Tuesday, September 7, 2010 6:04 AM
  • Hi,

    if u want to interact to the SQL server then use Webservice to interact SQL Server and call the web service in ur mobile device.

    Steps to do:

    1) Open VS 2005/2008 instance --> new website--> choose webservice project and say OK.

    2) In the web service expose ur DB interaction methods.

      Like ,

    [WebMethod]
    Public DataTable GetEmployeDetails()
    {
     string connectionString = "Data Source= local; Initial Catalog= Employees; User ID= sa; password = ******";
     SqlConnection con = new SQLConnection(connectionString);
     con.Open();
     SqlCommand cmd =new SqlCommand("Select * from employees", con );
     SqlDataReader dr = cmd.ExecuteReader();
     DataTable dtEmployes = null;
     if(dr != null)
     {
       dtEmployess.Load(dr);
      }
     con.Close();
     return dtEmployes;
    }
    

    3) build the web service and deploy the webservice to ur IIS.

    4) Once it is deployed then check and test through ur browser.

        if u run from ur VS 2005/2008 then it runs on localhost with some port assigned.

        if u run through IIS. so, u can call the service with ur HostName/IPAddress with default port(80).

    5) Now take a new VS 2005/2008 instance --> new project --> Smat Device Application.

    6) Right click on ur Smart device application project Add Web Reference --> choose the service from local machine or past the URL of ur service and provide      some name to the reference to identify.

    7) Once u ve done u can call ur web service in this way.

    private TestWebService.Service objService = null;
    
    private void CreateWebServiceConnection()
    {
     // URL of ur web service
     string conURL = "http:\\192.168.84.200\TestWebService\Service.asmx"
    
     // IPaddress of ur machine where ur service is hosted.
     string ipaddress = "192.168.84.200";
    
     //domain of the machine 
     string domain = "XYZ";
     //default port
     string port = "80";
    
     objService = new TestWebService.Service();
     objService .Url = conURL;
     objService .Proxy = new System.Net.WebProxy(ipaddress, int.Parse(port));
     ICredentials credentials = new
     NetworkCredential(userName, password, domain);
     objService .Credentials = credentials;     
    }
    
    
    private btnGetEmployes_Click(object sender, EventArgs e)
    {
      if(objService == null) 
       CreateWebServiceConnection();
     DataTable dtEmployes = objService.GetEmployeDetails();
     dataGridview1.DataSource = dtEmployes.DefaultView;
    }
    
    
    8) build the project and run the application.

    Hope this helps to u.

    feel free to ask question,

     


    Nagarjuna Dilip
    Tuesday, September 7, 2010 6:29 AM

All replies

  • Hi Shanaka,

    It is like desktop, we need sqlConnection to connect to a remote device via its IP or computer name, then use SqlDataAdapter to get the data to fill a dataset. Here is FAQ:
    How do I connect to a desktop SQL Server database? 

    For wi-fi, it is just a connection path, if device doesn't connection to the database server af first, we can connect WIFI programmatically:
    http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/2c30d5f0-0921-483f-b813-0ddf6adf9bd0

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Tuesday, September 7, 2010 6:04 AM
  • Hi,

    if u want to interact to the SQL server then use Webservice to interact SQL Server and call the web service in ur mobile device.

    Steps to do:

    1) Open VS 2005/2008 instance --> new website--> choose webservice project and say OK.

    2) In the web service expose ur DB interaction methods.

      Like ,

    [WebMethod]
    Public DataTable GetEmployeDetails()
    {
     string connectionString = "Data Source= local; Initial Catalog= Employees; User ID= sa; password = ******";
     SqlConnection con = new SQLConnection(connectionString);
     con.Open();
     SqlCommand cmd =new SqlCommand("Select * from employees", con );
     SqlDataReader dr = cmd.ExecuteReader();
     DataTable dtEmployes = null;
     if(dr != null)
     {
       dtEmployess.Load(dr);
      }
     con.Close();
     return dtEmployes;
    }
    

    3) build the web service and deploy the webservice to ur IIS.

    4) Once it is deployed then check and test through ur browser.

        if u run from ur VS 2005/2008 then it runs on localhost with some port assigned.

        if u run through IIS. so, u can call the service with ur HostName/IPAddress with default port(80).

    5) Now take a new VS 2005/2008 instance --> new project --> Smat Device Application.

    6) Right click on ur Smart device application project Add Web Reference --> choose the service from local machine or past the URL of ur service and provide      some name to the reference to identify.

    7) Once u ve done u can call ur web service in this way.

    private TestWebService.Service objService = null;
    
    private void CreateWebServiceConnection()
    {
     // URL of ur web service
     string conURL = "http:\\192.168.84.200\TestWebService\Service.asmx"
    
     // IPaddress of ur machine where ur service is hosted.
     string ipaddress = "192.168.84.200";
    
     //domain of the machine 
     string domain = "XYZ";
     //default port
     string port = "80";
    
     objService = new TestWebService.Service();
     objService .Url = conURL;
     objService .Proxy = new System.Net.WebProxy(ipaddress, int.Parse(port));
     ICredentials credentials = new
     NetworkCredential(userName, password, domain);
     objService .Credentials = credentials;     
    }
    
    
    private btnGetEmployes_Click(object sender, EventArgs e)
    {
      if(objService == null) 
       CreateWebServiceConnection();
     DataTable dtEmployes = objService.GetEmployeDetails();
     dataGridview1.DataSource = dtEmployes.DefaultView;
    }
    
    
    8) build the project and run the application.

    Hope this helps to u.

    feel free to ask question,

     


    Nagarjuna Dilip
    Tuesday, September 7, 2010 6:29 AM
  • hi,

    If you want the WIFI connect and disconnect then follow this link

     

    http://social.msdn.microsoft.com/Forums/en-US/windowsmobiledev/thread/0b73fb4f-bbe4-49e4-b3bd-a1fb185bab26


    Nagarjuna Dilip
    Tuesday, September 7, 2010 6:37 AM
  • Thanks Guang for the reply. I'l try it :)
    Thursday, September 9, 2010 8:37 AM
  • Thanks a lot for the reply Nagarjuna.
    Thursday, September 9, 2010 8:39 AM