通过WCF服务同步SQL表

通过WCF服务同步SQL表,wcf,web-services,sql-server-2008,wcf-data-services,Wcf,Web Services,Sql Server 2008,Wcf Data Services,我的MS SQL数据库上有一个表。(2008 R2) 表名称:消息 字段:消息id、消息、是否同步 在我的移动智能手机上,我需要开发一个应用程序来同步我的表格记录 并在应用程序同步每条记录时更新表。 我需要支持对WCF的最小呼叫数,因此我不能为每条记录生成呼叫 若表中有n条记录,我不想调用WCF n次,我想调用 一次,要获取所有记录,请使用WCF同步并返回所有同步结果。 这样做对吗? 您能建议更好的实施方式吗?当情况发生变化时,您可以通过双工通信将数据从服务发送到所有智能手机 然而,要回答您

我的MS SQL数据库上有一个表。(2008 R2) 表名称:消息 字段:消息id、消息、是否同步

在我的移动智能手机上,我需要开发一个应用程序来同步我的表格记录 并在应用程序同步每条记录时更新表。 我需要支持对WCF的最小呼叫数,因此我不能为每条记录生成呼叫

若表中有n条记录,我不想调用WCF n次,我想调用 一次,要获取所有记录,请使用WCF同步并返回所有同步结果。 这样做对吗?
您能建议更好的实施方式吗?

当情况发生变化时,您可以通过双工通信将数据从服务发送到所有智能手机

然而,要回答您当前的问题,考虑到您当前的实现,您可以对服务进行投票 启动时或计时器上所有消息的列表

在您的服务器上,您可以在一个简单的集合中拥有如下内容:

[ServiceContract(Namespace = "Contracts.IDatabaseResponder")]
public interface IDatabaseResponder
{
    //You could use an object rather than a string but then you have to mark the object
    [OperationContract]
    List<String> GetMessages();
    [OperationContract]
    void SyncMessagesBack(List<String> messages);

}



[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class DatabaseResponder : IDatabaseResponder
{
    List<String> _DatabaseMessageList;

    public List<String> GetMessages()
    {
        //Code here to go to SQL and grab all of the needed Messages
        //..


        return _DatabaseMessageList;
    }

    public void SyncMessagesBack(List<String> messages)
    {
        //Code here to go to SQL and update messages you want to update
        //..


    }

}
[ServiceContract(Namespace=“Contracts.idatabasereponder”)]
公共接口IDatabaseResponder
{
//您可以使用对象而不是字符串,但必须标记对象
[经营合同]
List GetMessages();
[经营合同]
void SyncMessagesBack(列出消息);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,ConcurrencyMode=ConcurrencyMode.Multiple)]
公共类数据库响应程序:IDatabaseResponder
{
列表(DatabaseMessageList);
公共列表GetMessages()
{
//在此处编写代码以转到SQL并获取所有需要的消息
//..
返回DatabaseMessageList;
}
公共无效SyncMessagesBack(列出消息)
{
//在此处编写代码以转到SQL并更新要更新的消息
//..
}
}
那么,在客户端,类似这样的操作将起作用:

    //Can use plain old list or ObservableCollection
    private IList<String> _DatabaseMessagesItems = new ObservableCollection<String>();
    private DatabaseResponderClient _Proxy;
    DispatcherTimer dispatcherTimer;
    List<String> LocalListOfMessages;

    public Constructor()
    {

          _Proxy = new DatabaseResponderClient();
          _Proxy.InnerChannel.Faulted += new EventHandler(InnerChannel_Faulted);


       try
       {
                _DatabaseMessagesItems = _Proxy.GetMessages();
       }
       catch (Exception ex)
       {
                MessageBox.Show(ex.Message);

                throw;
       }

       dispatcherTimer = new DispatcherTimer();
       dispatcherTimer.Tick += new EventHandler(dispatcherTimerTick);
       dispatcherTimer.Interval = new TimeSpan(0, 0, 60);
       dispatcherTimer.Start();

       dispatcherTimerTick();
    }


    private void dispatcherTimerTick(object sender, EventArgs e)
    {

        try
        {
             //Push back to the service any new or changed list of messages you need to push
             _Proxy.SyncMessagesBack(LocalListOfMessages);
        }
        catch (Exception ex)
        {
            //Handel error
        }
    }

   //Code to keep track of new messages add them etc
   //...
//可以使用普通旧列表或ObservableCollection
私有IList_DatabaseMessagesItems=新的ObservableCollection();
专用数据库ResponderClient\u代理;
调度员调度员;
列出LocalListOfMessages;
公共构造函数()
{
_Proxy=newdatabaseresponderclient();
_Proxy.InnerChannel.Faulted+=新的EventHandler(InnerChannel_Faulted);
尝试
{
_DatabaseMessagesItems=_Proxy.GetMessages();
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message);
投掷;
}
Dispatchermer=新Dispatchermer();
dispatcherTimer.Tick+=新事件处理程序(dispatcherTimerTick);
Dispatchermer.Interval=新的时间跨度(0,0,60);
dispatchermer.Start();
DispatcherMertick();
}
私有void dispatchermertick(对象发送方,事件参数e)
{
尝试
{
//将需要推送的任何新消息或更改的消息列表推送回服务
_SyncMessagesBack(LocalListOfMessages);
}
捕获(例外情况除外)
{
//汉德尔误差
}
}
//用于跟踪新消息的代码添加消息等
//...

SQL只是结构化查询语言—许多数据库系统使用的查询语言,而不是数据库产品本身。我们真的需要知道您使用的是什么具体的数据库系统(以及哪个版本)(请相应地更新标记)…现在还不清楚您在问什么。“这是正确的方法吗”-嗯,不清楚您正在尝试做什么或为什么要这样做。sql2008 db上的表是动态的,这意味着它每隔几秒钟就会收到新记录。在asp.net站点aspx页面上,我需要“精确”打印尚未同步的结果。在使用手机时,我需要同步尽可能多的结果,并始终向sql2008表报告哪些记录已经同步。所以,在这种情况下,时间就是一切——我不能允许已经在手机上同步的记录在网站上打印为未同步。我从未使用过双面打印-在我的情况下使用双面打印准确吗?是的,双面打印应该可以工作。双http绑定是您需要的。请看一看web上此类WCF服务实现的示例。我的客户端智能手机应用程序将用于android。我看到这里没有对双工wcf的支持,是的,但是我提供的代码可以在没有双工的情况下用于同步。而且你从一开始就不清楚所有的平台。使您的服务双工,然后使用上面的代码为android和回调双工通信为您的ASP网站。