quinta-feira, 30 de julho de 2015

IDataAccess C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OracleClient;

namespace Lapuinka.COM.DAO
{
    public interface IDataAccess : IDisposable
    {
        List<String> getPendingMessages();
        List<String> getUnsentMessages();
    }

    public class OracleDataAccess : IDataAccess
    {
        private OracleConnection oracleConnection;

        public OracleDataAccess()
        {
            oracleConnection = new OracleConnection(ServiceSettings.activeInstance().getOracleConnectionString());
            oracleConnection.Open();
        }

        public void Dispose()
        {
            oracleConnection.Close();
        }

        public List<String> getPendingMessages()
        {
            List<String> idList = new List<String>();

            using (OracleCommand command = oracleConnection.CreateCommand())
            {
                command.CommandText = "SELECT NUMBERPHONE FROM MESSAGE WHERE (MSG_PROCESSED = 'N') GROUP BY NUMBERPHONE";

                try
                {
                    using (OracleDataReader dr = command.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            if (dr.GetString(0) != null)
                            {
                                idList.Add(dr.GetString(0));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw formatException("getPendingMessages()", ex);
                }
            }

            return idList;
        }

        public List<String> getUnsentMessages()
        {
            List<String> idList = new List<String>();

            using (OracleCommand command = oracleConnection.CreateCommand())
            {
                command.CommandText = "SELECT NUMBERPHONE FROM MESSAGE WHERE (MSG_PROCESSED = 'Y' AND MSG_SENT = 'N') GROUP BY NUMBERPHONE";

                try
                {
                    using (OracleDataReader dr = command.ExecuteReader())
                    {
                        if (dr.Read())
                        {
                            if (dr.GetString(0) != null)
                            {
                                idList.Add(dr.GetString(0));
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    throw formatException("getUnsentMessages()", ex);
                }
            }

            return idList;
        }

        private Exception formatException(String method, Exception ex)
        {
            return new Exception("PushNotification : OracleDataAccess | Method: " + method + "", ex);
        }
    }
}


        private void sendMessage(TextBox textBox, TcpClient clientSocket)
        {
            NetworkStream serverStream = clientSocket.GetStream();
            byte[] outStream = System.Text.Encoding.ASCII.GetBytes("$" + textBox);
            serverStream.Write(outStream, 0, outStream.Length);
            serverStream.Flush();

            byte[] inStream = new byte[10025];
            serverStream.Read(inStream, 0, (int)clientSocket.ReceiveBufferSize);
            string returndata = System.Text.Encoding.ASCII.GetString(inStream);
            msg("Data from Server : " + returndata, textBox);
        }


Postar um comentário