C# .NET - Connect client WPF application, WCF and SQL SERVER

Asked By Dom Afonso on 28-Jun-12 05:45 AM
Earn up to 20 extra points for answering this tough question.
I need a c# code to connect a remote wpf application to SQL Server. Basically I wish to use sql server service like WCF in other to retrieve data from internet.
[)ia6l0 iii replied to Dom Afonso on 28-Jun-12 12:30 PM
In order to access a SQL Server database from WPF application, create a SQLConnection object, and point it to the database that you need to use. 
SqlConnection con = new SqlConnection("Data Source=servername;initial catalog=databasename, userid=u; pwd=p;");
And continue your code as usual. 

If you want to connect to a WCF service, right-click on the project and choose "Add Service reference" and browse for the WCF service. And then add a service reference. Then  you would start coding as if you are working with a normal class. 

Hope this helps.
Jitendra Faye replied to Dom Afonso on 29-Jun-12 12:35 AM
For this you need to simply use connection string for that remote sql server ,

means you have to set connection string attribute like this-


use connectionstring tag like this-

<connectionStrings>
  <add
    name="conname"
    connectionString="Data Source=RemoteserverName or IP address;Initial
    Catalog=DataBaseName;Persist Security Info=True;User
    ID=userName;Password=password"
    providerName="System.Data.SqlClient"
  />
</connectionStrings>


bharti odedra replied to Dom Afonso on 29-Jun-12 07:39 AM

 Simple Steps to Connect SQL Server using WCF from SilverLight

1: Create the Service and Data Service Contract

[DataContract]
    public class clsCustomer
    {
      private string _strCustomer;
      private string _strCustomerCode;
 
      [DataMember]
      public string Customer
      {
        get { return _strCustomer; }
        set { _strCustomer = value; }
      }
 
      [DataMember]
      public string CustomerCode
      {
        get { return _strCustomerCode; }
        set { _strCustomerCode = value; }
      }
    }

2: Code the WCF Service

public class ServiceCustomer : IServiceCustomer
    {
      public clsCustomer getCustomer(int intCustomer)
      {
        SqlConnection objConnection = new SqlConnection();
        DataSet ObjDataset = new DataSet();
        SqlDataAdapter objAdapater = new SqlDataAdapter();
        SqlCommand objCommand = new SqlCommand
         ("Select * from Customer where CustomerId=" + intCustomer.ToString());
        objConnection.ConnectionString = 
         System.Configuration.ConfigurationManager.ConnectionStrings
         ["ConnStr"].ToString();
        objConnection.Open();
        objCommand.Connection = objConnection;
        objAdapater.SelectCommand = objCommand;
        objAdapater.Fill(ObjDataset);
        clsCustomer objCustomer = new clsCustomer();
        objCustomer.CustomerCode = ObjDataset.Tables[0].Rows[0][0].ToString();
        objCustomer.Customer = ObjDataset.Tables[0].Rows[0][1].ToString();
        objConnection.Close();
        return objCustomer;
      }
    }

3: Copy the CrossDomain.xml and ClientAccessPolicy.XML File

<?xml version="1.0"?>
    <!DOCTYPE cross-domain-policy SYSTEM 
      "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd">
    <cross-domain-policy>
    <allow-http-request-headers-from domain="*" headers="*"/>
   </cross-domain-policy>

4: Change the WCF Bindings to ‘basicHttpBinding’

<endpoint address="" binding="basicHttpBinding" 
      contract="WCFDatabaseService.IServiceCustomer">

5: Add Service Reference

 right click the Silverlight project and select add service reference.

6: Define the Grid for Customer Name and Customer Code

<Grid x:Name="LayoutRoot" Background="White">
      <Grid.ColumnDefinitions>
          <ColumnDefinition></ColumnDefinition>
          <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
      <Grid.RowDefinitions>
          <RowDefinition Height="20"></RowDefinition>
          <RowDefinition Height="20"></RowDefinition>
        </Grid.RowDefinitions>
        <TextBlock x:Name="LblCustomerCode" Grid.Column="0" 
         Grid.Row="0" Text="Customer Code"></TextBlock>
        <TextBlock x:Name="TxtCustomerCode" Grid.Column="1" 
         Grid.Row="0" Text="{Binding Path=CustomerCode}"></TextBlock>
        <TextBlock x:Name="LblCustomerName" Grid.Column="0" 
         Grid.Row="1" Text="Customer Name"></TextBlock>
        <TextBlock x:Name="TxtCustomerName" Grid.Column="1" 
         Grid.Row="1" Text="{Binding Path=Customer}"></TextBlock>
    </Grid>

7: Bind the WCF Service with the GRID

public partial class Page : UserControl
{
    public Page()
    {
      InitializeComponent();
      ServiceCustomerClient obj = new ServiceCustomerClient();
      obj.getCustomerCompleted += new EventHandler<getCustomerCompletedEventArgs>
                 (DisplayResults);
      obj.getCustomerAsync(1);
    }
    void DisplayResults(object sender, getCustomerCompletedEventArgs e)
    {
      LayoutRoot.DataContext = e.Result;
    }
}
bharti odedra replied to Dom Afonso on 29-Jun-12 07:59 AM
try this 
SqlConnection con = new SqlConnection("Data Source=servername;initial catalog=databasename, userid=username; pwd=password;"); 
help
Scenario: WPF and ASP.NET clients connect to WCF. WCF connects to SQL Server. System: WCF Server = 8 cores / 32 GB RAM, Windows 2008, .NET3.5 Problem: The Windows Communications Foundation (WCF) Service is started and used approximately 8% - 13% of the CPU resources for each of 2 hours.User load is light (less than 10 users). Thanks! .NET Web Services Discussions SQL Server (1) ASP.NET (1) WCF (1) WPF (1) Database (1) Consumes (1) Windows (1
Just curious, can SQLCLR managed stored procedures be written using .NET 3.X, or just 2.0? Does SQL Server 2005 host the .NET FX 3.X? Will SQL Server 2008 host multiple versions of the CLR, or just 2.0? TIA, KJ SQL Server Programming Discussions SQL Server 2008 (1) SQL Server 2005 (1) WPF (1) WCF (1) SQLskills (1) SQLCLR (1) Bobb (1) WWF (1) There is really no .NET 3
hi all, i m new on silver light. . . . . . .app i have tried to connect silver app to sql server using WCF services but error is occuring plz help. . . . . . and provide step by step process tnx for KB / silverlight / Silverlight3_SQL_WCF.aspx http: / / www.dotnetspark.com / kb / 652-7-simple-steps-to-connect-sql-server-using.aspx keywords: SilverlightSilverlight WPF, WPF, WCF, SIlverlight application, Check description: code for connection from WCF to SIlverlight application usin sqlserver hi
We're building a three tier architecture whereas the client is a .net Windows Client (WPF), database is SQL Server 2008 and for the middle-tier we're using a WCF service. As for the business logic we're thinking (for different reasons) about putting it CLR). We're thinking about passing objects from the GUI through the service to the SQL Server CLR. For transport we will serialize the objects in binary format and pass them ll have main parts of our logic in the CLR but doing CRUDs using Transact SQL. Has anyone out there experince with this approach and can cover about pros and cons? SQL Server Programming Discussions SQL Server 2008 (1) CREATE FUNCTION (1) DROP FUNCTION (1) Smalldatetime (1
Hello, We have an "in-house" application that we have developed in WPF reading a SQL Server 2008 Database. We are able to deploy this to the various clients via ClickOnce The question is, some of this data on the SQL Server 2008 Database can change. We want a good reliable way to have this data downloaded to the client "Sql Server Compact 3.5" Databases. Is WCF the way to go? Can anyone give me some advice on how to proceed? How tutorials or articles out there? Thanks Jason You'll find this useful that talks about WPF, Silverlight, and WCF architecture. http: / / www.eggheadcafe.com / tutorials / aspnet / b685f06c-537b-4def-bacc-e5d56e550430
vb.net con vs 2005 team system, y el framework 2.0 (y 3.0 wcf, wpf) Mi proyecto tiene dos partes: una aplicación winforms en Windows XP y servicios WCF en Windows Server 2003, con acceso a datos sql server 2005. ahora, me planteo el cambio en desarrollo y producción de pasar del framework Discussions Windows XP (1) Windows Server 2003 (1) Visual Studio 2008 (1) Entity Framework (1) WCF (1) Alhambra (1) Está (1) Eidos (1) Yo he migrado bastantes proyectos de 2005 a los migrados desde vs2003, que sí que son una lata). Pero ninguno de ellos usaba WCF. No he realizado ningún benchmark, pero he probado varios programas y no se aprecia a El SP1 aporta algunas funcionalidades nuevas, como por ejemplo el Entity Framework. Gracias. Los servicios WCF que ya tengo configurados, seguirán funcionando igual ?? Gracias. En teoría, sí, pero nunca vb.net con vs 2005 team system, y el framework 2.0 (y 3.0 wcf, wpf) Mi proyecto tiene dos partes: una aplicación win
NewTreeviewApp.zip Error Is Like An exception occurred during m Getting this error when i m trying to bind treeview by using linq to sql and wcf in sql. and also i m getting error in designing time when i my project build succeeded security > < transport > < extendedProtectionPolicy policyEnforcement = "Never" / > < / transport > < / security > Hope this helps keywords: AsyncCompletedEventArgs, Silverlight, Error, exception, WPF, Result, Check, Treeview, wcf, sql description: Getting Error On binding Treeview by linq to sql nd wcf in Silverlight
Can anyone share your knowladge of LINQ, WPF, WCF, Sharepoint MOSS2007 with me.In case anyone got any books, souce to learn this , please search for any topic and get articles. . . Here are some excellent sites to learn LINQ, WPF, WCF, Sharepoint MOSS2007 LINQ Overview and Samples http: / / msdn.microsoft.com / en-us / netframework / aa904594.aspx http: / / msdn.microsoft.com / en-us / vcsharp / aa336746.aspx WPF Overview and Samples http: / / msdn.microsoft.com / en-us / library / ms754130.aspx http: / / msdn.microsoft com / en-us / library / ms754130.aspx WCF Overview and Samples http: / / msdn.microsoft.com / en-us / library / ms735119.aspx http: / / msdn.microsoft
i dont know any thing about wcf and wpf i wantto know exactly about them please help me Try googling for "WCF" or "WPF" and you will find lots and lots of links related to this topic. There are even tutorials on each topic. Here are some sample links: WCF - Windows Communication Foundation http: / / msdn.microsoft.com / en-us / library / ms731082.aspx - What is WCF? http: / / msdn.microsoft.com / en-us / library / ms734712.aspx - Getting Started Tutorial WPF - Windows Presentation Foundation http: / / msdn.microsoft.com / en-us / library / ms742119.aspx - Getting Started (WPF