Wednesday, September 19, 2012

MS SQL Select full content of a varchar column


DECLARE @S varchar(max)
SELECT @S = ''
SELECT @S = @S + '
' + OBJECT_DEFINITION(OBJECT_ID) FROM SYS.PROCEDURES
SELECT @S AS [processing-instruction(x)] FOR XML PATH('')

Monday, August 13, 2012

Access GUI from background thread using C# Forms

using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Threads
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        delegate void SetTextCallback(string text, int value);

        private void Form1_Load(object sender, EventArgs e)
        {
            ThreadTest t = new ThreadTest(this);
            Thread th = new Thread(t.DoWork);
            th.Start();
            //th.Join();
        
        }

        public void Populate(string text, int value)
        {

            if (this.lblMesaj.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(Populate);
                this.Invoke(d, new object[] { text,value });
                this.Invoke(d, new object[] { text,value });

            }
            else
            {
                this.lblMesaj.Text = text;
                this.progressBarFinalize.Value = value;
            }

            lblMesaj.Text = text;
        }


        private void btnSave_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }

    class ThreadTest
    {
        public delegate void populateTextBoxDelegate(string text);
        Form1 formMain;

        public ThreadTest(Form1 form)
        {
            formMain = form;
        }


        public void DoWork()
        {
            for (int i = 0; i < 1000; i++)
            {
                formMain.Populate("In progress ....", i/10);
                for (int j = 0; j < 1000; j++)
                {
                    for (int k = 0; k < 100; k++)
                    {
                        double b = Math.Sqrt(923409290423) / Math.Sqrt(423423);
                    }
                }
            }

            formMain.Populate("Thread has finished!",100);

        }
    }

}

Monday, August 6, 2012

Generate Random String c#

private string GeneratePassword()
    {

        Random rng = new Random();
        string _chars =  
                         "ABCDEFGHIJKLMNOPQRSTUVWXYZqwertyuiopplkjhgfdsazxcvbnm1234567890";
        int size = rng.Next(8,12);

        char[] buffer = new char[size];

        for (int i = 0; i < size; i++)
        {
            buffer[i] = _chars[rng.Next(_chars.Length)];
        }
        return new string(buffer);
    }

Friday, June 15, 2012

BackTracking c#

 class Program
    {

        static int[] a;
        static int k;
        static int SIZE = 4;
        static void Main(string[] args)
        {
            a = new int[20];
            bkp();
            System.Console.ReadLine();


        }

        static void bkp()
        {
            k = 1;
            a[k] = 0;
            bool getSuccesor;
            while (k > 0)
            {
                do
                {
                    getSuccesor = HaveSuccesor();
                }
                while (getSuccesor && !IsValid());

                if (getSuccesor)
                {
                   
                    if (IsValidSolution())
                    {
                        ShowSolution();
                    }
                    else
                    {
                        k++;
                        a[k] = 0;
                    }
                }
                else
                {
                    a[k] = 0;
                    k--;
                }

            }
        }

        static bool HaveSuccesor()
        {
            if (a[k] < SIZE)
            {
                a[k]++;
                return true;
            }
            else
                return false;
        }

        public static bool IsValid()
        {
           if (k > 1)
                if (a[k] < a[k - 1]) return false;
            for (int i = 1; i < k; i++)
                if (a[i] == a[k]) return false;
           
            return true;
        }

        public static bool IsValidSolution()
        {
            if(k==SIZE) return true;
            return false;
        }

        static void ShowSolution()
        {
            for (int i = 1; i < = SIZE; i++)
                System.Console.Write(a[i] + " ");
            System.Console.Write(Environment.NewLine);
        }

        static void ShowTowers()
        {
            for (int i = 1; i < = SIZE; i++)
            {
                for (int j = 1; j < = SIZE; j++)
                {
                    if(a[i]==j)
                        System.Console.Write("T");
                    else
                        System.Console.Write("*");
                }
                System.Console.Write(Environment.NewLine);
             }
            System.Console.Write(Environment.NewLine);
          
        }


    }
}

Monday, May 28, 2012

Copy an image from internet:

private void CopyFile(string file)
        {
            WebRequest req = WebRequest.Create(@"http:....." + file);
            req.Proxy = new WebProxy("1.1.1.1", 8080);
            req.Proxy.Credentials = new NetworkCredential(@"user", "pass");
            WebResponse responsePic = req.GetResponse();
            Image webImage = Image.FromStream(responsePic.GetResponseStream()); // Error
            webImage.Save(@"D:\\pictures\" + file);
        }

Wednesday, May 23, 2012

AES Encription (symetric key) with SQL Server

go

--keys for encryption
Create Master Key Encryption by Password='xxxx#'
Create Certificate App_Certificate With Subject='Test Certificate'
Create Symmetric Key App_SymmetricKey With Algorithm=AES_256 encryption by Certificate App_Certificate


go

create procedure EncryptStr
       @str varchar(256),
       @strOUT varbinary(256) output    
as

OPEN SYMMETRIC KEY App_SymmetricKey DECRYPTION BY CERTIFICATE App_Certificate

SET @strOUT = EncryptByKey(Key_GUID('SymmetricKey'), @str)

CLOSE SYMMETRIC KEY App_SymmetricKey

go

create procedure DecryptStr
       @str varbinary(256),
       @strOUT varchar(256) output
as

OPEN SYMMETRIC KEY App_SymmetricKey DECRYPTION BY CERTIFICATE App_Certificate

SET @strOUT = DecryptByKey(@str)

CLOSE SYMMETRIC KEY App_SymmetricKey

go


-- example for use

declare @x varbinary(256)
declare @s varchar(256)
declare @z varchar(256)


set @s = 'aaabbbccc'
--encrypt the string
exec EncryptStr @s, @x out
select @x

--decrypt the string
exec DecryptStr @x,@z out

select @z