2012-07-16 76 views
0

我正在嘗試將我的Silverlight應用程序連接到SQL Server 2005以用於登錄目的。我對Silverlight完全陌生,並希望在Silverlight中構建自己的網站。請建議有用的地點進行放肆。提前Thanx。如何將我的silverlight應用程序連接到SQL Server 2005

+0

建設性的批評:這個問題太泛泛,無法得到有用的答案。基本原則是Silverlight無法直接連接到數據庫,因此您需要通過其他方式代理請求。一個非常通用的起點:http://www.dotnetcurry.com/ShowArticle.aspx?ID = 624 – 2012-07-16 15:40:03

回答

1

您必須使用Web服務示例WCF。 1)將WCF添加到您的項目中。

//This is your interface Class. 
namespace SilverlightApplication1.Web 
{  
    [ServiceContract] 
    public interface IService1 
    { 
    [OperationContract] 
    bool UserLogin(string email, string password); 
    } 
} 

//This is your service code behind class 
namespace SilverlightApplication1.Web 
{ 
    public class Service1 : IService1 
    { 
    public bool UserLogin(string email,string password) 
    { 
       // Your logic here to verify user name and password 
    } 
    } 
} 

//After creating the service. Add a reference to your application.** 

2)服務引用添加到您的Silverlight應用程序。 右鍵單擊您的項目,選擇Web引用選項並將該服務添加到您的項目中。現在,如果您的表單上有一個按鈕控件,它會將數據提交給您的wcf服務。在其單擊事件中添加以下代碼。

Service1Client proxy ; 
private void button1_Click(object sender, RoutedEventArgs e) 
    { 
     proxy.UserLogin += new EventHandler<InsertDataCompletedEventArgs>(proxy_UserLogin); 
     proxy.UserLogin(txtEmail.Text, "Password"); 
    } 

    void proxy_UserLogin(object sender, InsertDataCompletedEventArgs e) 
    { 
     if (e.Result == true) 
     { 
      lblMesg.Content = "User Login successfully"; 
     } 
     else 
     { 
      lblMesg.Content = "User record not found"; 
     } 
    } 

在按鈕單擊事件調用該服務。

+0

woa,你真的應該在你的標記:) – 2012-07-16 06:15:35

+0

@AndreasNiedermair更新。 :) – 2012-07-16 06:18:52

+0

真棒!....... – 2012-07-16 06:21:18

0

如果你想要登錄,您可以創建一個Silverlight業務應用程序項目中的SQL Server連接。它內置了用戶登錄。這樣,您可以專注於Silverlight應用程序的其他功能。

相關問題