Saturday, 19 October 2013

.Net with MQ Series sample

What is MQ Series?
System for sending messages between multiple platforms, It supports multiple platforms, protocol. This help us to transfer data from one system to another.

Installation of MQ Series.
The MQ Series consists of server and client, I downloaded the trial version of same from IBM site. You need to install the server & client on different machine, I tried to install on same but it did not work for me.

Server:
WS_MQ_V7.1.0.3_TRIAL_FOR_WINDOWS_ML

Client:
mqc7_7.0.1.6_win

Install the server, For below provide domain username.  Also "IBM Websphere MQ" will run using this username.





Tick the checkbox "Continue with this user account".


 Once installed and before configuring "IBM Websphere MQ Explorer." just make sure service   "IBM Websphere MQ"  is running.

Creating Queue Manager:
In the explorer, create an new Queue manager, call this QUEUE_MANAGER.


 
Creating Queue:
Click New->Local Queue, name it QUEUE_LOCAL






Creating Channel:
Select Channels--> New "Server-Connection Channel"
Name the channel as  SVR_RCV_CHANNEL

Once your server is ready, Install client in local machine.

Next is to code the connection with MQ Series.

Create a New Windows Form Application.

Add new class to it as "MQClass".

Following parameters are required to initiate connection to the MQ Series:
 1) Host Name: Machine name where server is installed.
2) Channel Name: Channel through which you are planning to connect to the server.
3) Port on which the Server is listening, Default port is 1414.

AddReference: amqmdnet.dll which is located in your bin folder

Include namespace: IBM.WMQ;

Connecting to the MQ Series server
public string ConnectMQ()
        {    
           String strReturn = "";
            try
            {
                //MQEnvironment.Port = 1414;
                //MQEnvironment.Channel = channelName;
                //MQEnvironment.Hostname = "localhost";

                Hashtable properties = new Hashtable();
                properties.Add(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES_MANAGED);
                properties.Add(MQC.HOST_NAME_PROPERTY, "hostmachinename");
                properties.Add(MQC.PORT_PROPERTY, 1414);
                properties.Add(MQC.CHANNEL_PROPERTY, "SVR_RCV_CHANNEL");
               // properties.Add(MQC.USER_ID_PROPERTY, "UserName");
               // properties.Add(MQC.PASSWORD_PROPERTY, "hhh");

                queueManager = new MQQueueManager("QUEUE_MANAGER ", properties);
                strReturn = "Connected Successfully";
            }
            catch (MQException exp)
            {
                strReturn = "Exception: " + exp.Message;
            }
                  return strReturn;
        }

 User the same user through which you are running the MQ Series service.

If you get the Error "mqrc_not_authorized" then make sure the Admin user is not blocked, which is by default blocked.

Go to Channels--> ChannelAuthrizationRecords and remove the blocked user.





Delete the channel authorization record if any.



Once you have connected to Queue manager using channel, Now it's time to pass some message and read the message.
Let's see how to pass the message to the queue,

public string WriteMsg(string strInputMsg)
        { 
            string strReturn = "";
            try
            {
                //first get the queue object, by connecting to queue created
                mqQueue = queueManager.AccessQueue(QueueName,

                MQC.MQOO_OUTPUT + MQC.MQOO_FAIL_IF_QUIESCING);

                message = strInputMsg;
1
                //create the message which has to be passed to queue.
                mqMessage = new MQMessage();

                //add some content to the message               
                mqMessage.WriteString(message);

                mqMessage.Format = MQC.MQFMT_STRING;
                queuePutMessageOptions = new MQPutMessageOptions();
                //go with the default message options
                mqQueue.Put(mqMessage, queuePutMessageOptions);

                //return success message
                strReturn = "Message sent to the queue successfully";

            }

            catch (MQException MQexp)
            {
                strReturn = "Exception: " + MQexp.Message;
            }

            catch (Exception exp)
            {
                strReturn = "Exception: " + exp.Message;
            }
            finally
            {
                if (mqQueue.OpenStatus)
                    mqQueue.Close();

            }
            return strReturn;
        }





Wednesday, 25 September 2013

Steps for LINQ TO SQL

1) Create a new project
    File-> New Project -> Console Application.

2) Create a new Connection
      1)  Go   to Server Explorer
      2) Select  "Data Connections" then right click and select  "Add Connection"
      3) This should add a new Connection to the Server Explorer.  You should see your tables, Functions,                Stored Procedures ... under this Connection object.
      4) Now select your project Right click and select "Add New Item" and select "LinqToSql".
      5) This should add .dbml file to your project.
      6)  This will create DataContext class in your project.
      7) Now from the connection object selected in previous statement, add tables to your dbml.
      8) which will consists of object model classes.

Create a object of that DataContext class and start using it.

           DataClasses1DataContext dc = new DataClasses1DataContext();
                     
            ////reading data from database

            var empList = from c in dc.employees
                          select c;

            foreach (var emp in empList)
            {
              Console.WriteLine("Emp Id: {0} " + "Emp Name:{1}", emp.emp_id, emp.emp_name);
            }

          //To insert data through LinqToSql

           employee emp1 = new employee();
            emp1.emp_id = "emp6";
            emp1.emp_name = "emp6 Name";
            dc.employees.InsertOnSubmit(emp1);
            dc.SubmitChanges();