WCF (Web Services) + Silverlight 4 + 連接 Database Insert Record 方法 (SOAP Server Side / SOAP Client Side)

 
WCF (Web Services) + Silverlight 4 + 連接 Database Insert Record 方法 (SOAP Server Side / SOAP Client Side)
 
(SOAP Server)

   IService1.cs
 
   using System;
   using System.Collections.Generic;
   using System.Runtime.Serialization;
   using System.ServiceModel;
 
   namespace WcfService1
   {
 
      // 注意: 您可以使用 [重構] 功能表上的 [重新命名] 命令同時變更程式碼和組態檔中的介面名稱 “IService1″。
      [ServiceContract]
      public interface IService1
      {
         [OperationContract]
         void InsertItem(int ID, String Text);
 
         // TODO: 在此新增您的服務作業
      }
 
   }
 

 

   Service1.svc.cs
 
   using System;
   using System.Collections.Generic;
   using System.Text;
   using System.Data.SqlClient;
 
   namespace WcfService1
   {
      // 注意: 您可以使用 [重構] 功能表上的 [重新命名] 命令同時變更程式碼、svc 和組態檔中的類別名稱 "Service1"。
      public class Service1 : IService1
      {
         public void InsertItem(int ID, String Text)
         {
 
            String connectionString1 = "Data Source=.\\SqlExpress; Initial Catalog=Soccer; Integrated Security=SSPI";
            SqlConnection con1 = new SqlConnection(connectionString1);
            con1.Open();
 
            StringBuilder sb = new StringBuilder();
            sb.Append("INSERT INTO dbo.Test_A (ID, Text) VALUES (" + ID + ",'" + Text + "');");
 
            SqlCommand cmd1 = new SqlCommand(sb.ToString(), con1);
            cmd1.ExecuteNonQuery();
 
            cmd1.Dispose();
            con1.Dispose();
 
         }
      }
   }
 

 
(SOAP Client – 需自行增加 Service References)

   MainPage.xaml.cs
 
   using System;
   using System.Collections.Generic;
   using System.Linq;
   using System.Net;
   using System.Windows;
   using System.Windows.Controls;
   using System.Windows.Documents;
   using System.Windows.Input;
   using System.Windows.Media;
   using System.Windows.Media.Animation;
   using System.Windows.Shapes;
   using SilverlightApplication7.ServiceReference1;
 
   namespace SilverlightApplication7
   {
      public partial class MainPage : UserControl
      {
         public MainPage()
         {
 
            InitializeComponent();
            Service1Client _Proxy = new Service1Client();
 
            _Proxy.InsertItemCompleted +=
            new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(_Proxy_InsertItemCompleted);
            _Proxy.InsertItemAsync(1000, "Test 1000");
 
         }
 
         void _Proxy_InsertItemCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
         {
            throw new NotImplementedException();
         }
      }
   }
 

 

   clientaccesspolicy.xml (令 Silverlight 得到 Permission 連接 SOAP Server – 放係 SOAP Server 既 Root Folder 上)
 
   <?xml version="1.0" encoding="utf-8" ?>
   <access-policy>
      <cross-domain-access>
         <policy>
            <allow-from http-request-headers="*">
               <domain uri="*" />
            </allow-from>
            <grant-to>
               <resource path="/" include-subpaths="true" />
            </grant-to>
         </policy>
      </cross-domain-access>
   </access-policy>
 

 

   crossdomain.xml (令 Silverlight 得到 Permission 連接 SOAP Server – 放係 SOAP Server 既 Root Folder 上)
 
   <?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>