C#怎样利用MSMQ发送和接收DataSet?高分请教各位高手!万分感谢!

xuxinqi 2005-05-05 05:22:20
System.Messaging.MessageQueueTransaction objtran=new MessageQueueTransaction();
System.Messaging.Message objMsg=new System.Messaging.Message();

objtran.Begin();
objMsg.Label="0200011111";
objMsg.Body=ds;

System.Messaging.MessageQueue objque=new System.Messaging.MessageQueue("FormatName:DIRECT=TCP:172..0.0.1\\private$\\222send");
objque.Send(objMsg,objtran);
objtran.Commit();

这样虽然说是把数据集发出去了,但是对方怎么接收呢??
...全文
133 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
dxuesong 2005-05-12
  • 打赏
  • 举报
回复
#region Class Cls_MessageMG
/// <summary>
/// This Class is used read and write message from MessageQueue
/// </summary>
public class Cls_MessageMG
{
public static void clear_message(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
}
MessageQueue mq=new MessageQueue(Queue_Path);
mq.Purge();
}

public static void send_message(string Queue_Path, string Msg_Content, Msg_Type Msg_Type_Instance)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
}
MessageQueue mq=new MessageQueue(Queue_Path);
mq.Label=System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff");
switch (Msg_Type_Instance)
{
case Msg_Type.XML:
mq.Formatter=new XmlMessageFormatter();
break;
case Msg_Type.Binary:
mq.Formatter=new BinaryMessageFormatter();
break;
case Msg_Type.ActiveX:
mq.Formatter=new ActiveXMessageFormatter();
break;
}
mq.Send(Msg_Content);
}

public static string receive_message(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return "";
}

MessageQueue mq= new MessageQueue(Queue_Path);
mq.Label=System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff");
((XmlMessageFormatter)mq.Formatter).TargetTypeNames=new string[]{"System.String,mscorlib"};

try
{
System.Messaging.Message m = mq.Receive();
return (string)m.Body;
}
catch (Exception ex)
{
return (ex.ToString());
}
}

public static string peek_message(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return "";
}

MessageQueue mq= new MessageQueue(Queue_Path);
mq.Label=System.DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:ffff");
((XmlMessageFormatter)mq.Formatter).TargetTypeNames=new string[]{"System.String,mscorlib"};

try
{
System.Messaging.Message m = mq.Peek();
return (string)m.Body;
}
catch (Exception ex)
{
return (ex.ToString());
}
}

public static bool IsQueueEmpty(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;
bool isQueueEmpty = false;

if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return true;
}

// Connect to a queue.
MessageQueue myQueue = new MessageQueue(Queue_Path);

try
{
// Set Peek to return immediately.
myQueue.Peek(new TimeSpan(0));

// If an IOTimeout was not thrown, there is a message
// in the queue.
isQueueEmpty = false;
}

catch(MessageQueueException ex)
{
if (ex.MessageQueueErrorCode ==
MessageQueueErrorCode.IOTimeout)
{
// No message was in the queue.
isQueueEmpty = true;
}

// Handle other sources of MessageQueueException.
}

// Handle other exceptions as necessary.

// Return true if there are no messages in the queue.
return isQueueEmpty;
}


public static int GetQueueCount(string Queue_Path)
{
Queue_Path=@".\Private$\"+Queue_Path;

if (!MessageQueue.Exists(Queue_Path))
{
MessageQueue.Create(Queue_Path);
return 0;
}

MessageQueue myQueue = new MessageQueue(Queue_Path);
MessageEnumerator myEnumerator = myQueue.GetMessageEnumerator();

int message_count=0;

while (myEnumerator.MoveNext())
{
message_count++;
}
return message_count;
}

}
#endregion
SZY_JZM 2005-05-05
  • 打赏
  • 举报
回复
没有用过
singlepine 2005-05-05
  • 打赏
  • 举报
回复
http://www.c-sharpcorner.com/2/MessageQueueTut1.asp
http://search.csdn.net/Expert/topic/547/547529.xml?temp=4.513186E-02
http://search.csdn.net/Expert/topic/1118/1118013.xml?temp=.5077783
供参考
coolksj 2005-05-05
  • 打赏
  • 举报
回复
没用过,帮你顶

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧