Saturday, 22 June 2024

How to search for a string in a database in Microsoft SQL

Use the following query to find a string or value in the entire database, regardless of whether you know the table name.


DECLARE
@search_string  VARCHAR(100),
@table_name     SYSNAME,
@table_id       INT,
@column_name    SYSNAME,
@sql_string     VARCHAR(2000)

SET @search_string = '8907595018255'


DECLARE tables_cur CURSOR FOR SELECT name, object_id FROM sys.objects WHERE  type = 'U'

OPEN tables_cur

FETCH NEXT FROM tables_cur INTO @table_name, @table_id

WHILE (@@FETCH_STATUS = 0)
BEGIN
    DECLARE columns_cur CURSOR FOR SELECT name FROM sys.columns WHERE object_id = @table_id
        AND system_type_id IN (167, 175, 231, 239)

    OPEN columns_cur

    FETCH NEXT FROM columns_cur INTO @column_name
        WHILE (@@FETCH_STATUS = 0)
        BEGIN
            SET @sql_string = 'IF EXISTS (SELECT * FROM ' + @table_name + ' WHERE [' + @column_name + ']
            LIKE ''%' + @search_string + '%'') PRINT ''' + @table_name + ', ' + @column_name + ''''

            EXECUTE(@sql_string)

        FETCH NEXT FROM columns_cur INTO @column_name
        END

    CLOSE columns_cur

DEALLOCATE columns_cur

FETCH NEXT FROM tables_cur INTO @table_name, @table_id
END

CLOSE tables_cur
DEALLOCATE tables_cur

Wednesday, 22 April 2015

What are the difference between DDL, DML and DCL commands?

DDL


Data Definition Language (DDL) statements are used to define the database structure or schema. Some examples:
  • CREATE - to create objects in the database
  • ALTER - alters the structure of the database
  • DROP - delete objects from the database
  • TRUNCATE - remove all records from a table, including all spaces allocated for the records are removed
  • COMMENT - add comments to the data dictionary
  • RENAME - rename an object

DML


Data Manipulation Language (DML) statements are used for managing data within schema objects. Some examples:
  • SELECT - retrieve data from the a database
  • INSERT - insert data into a table
  • UPDATE - updates existing data within a table
  • DELETE - deletes all records from a table, the space for the records remain
  • MERGE - UPSERT operation (insert or update)
  • CALL - call a PL/SQL or Java subprogram
  • EXPLAIN PLAN - explain access path to data
  • LOCK TABLE - control concurrency

DCL


Data Control Language (DCL) statements. Some examples:
  • GRANT - gives user's access privileges to database
  • REVOKE - withdraw access privileges given with the GRANT command

TCL


Transaction Control (TCL) statements are used to manage the changes made by DML statements. It allows statements to be grouped together into logical transactions.
  • COMMIT - save work done
  • SAVEPOINT - identify a point in a transaction to which you can later roll back
  • ROLLBACK - restore database to original since the last COMMIT
  • SET TRANSACTION - Change transaction options like isolation level and what rollback segment to use

Character escape sequences in C#

C# defines the following character escape sequences:
  • \' - single quote, needed for character literals
  • \" - double quote, needed for string literals
  • \\ - backslash
  • \0 – Null
  • \a - Alert
  • \b - Backspace
  • \f - Form feed
  • \n - New line
  • \r - Carriage return
  • \t - Horizontal tab
  • \v - Vertical quote
  • \u - Unicode escape sequence for character
·     \U Unicode escape sequence for surrogate pairs.
  • \x - Unicode escape sequence similar to "\u" except with variable length.

Thursday, 12 March 2015

Codenet: Thread-Safe Calls to Windows Forms Controls [.NET Framework 4.5]

Codenet: Thread-Safe Calls to Windows Forms Controls [.NET Framework 4.5]

Thread-Safe Calls to Windows Forms Controls [.NET Framework 4.5]

Thread-Safe Calls to Windows Forms Controls

.NET Framework 4.5


As access to the controls in Windows Form applications is not inherently thread safe. If any one have multiple threads manipulating the state of a control, possibly it force the control into an inconsistent state. Other thread related bugs such as rece conditions or deadloacks are also possible.



But you can perform your required task safely using some tricks.



Use the following extensions and just pass the action like:




dataGridView1.PerformSafely(() => dataGridView1.SelectAll());

OR


dataGridView1.PerformSafely(() => dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableAlwaysIncludeHeaderText);


OR


panel1.PerformSafely(() => panel1.Visible = false);

Extension class:


public static class CrossThreadExtensions
{
    public static void PerformSafely(this Control target, Action action)
    {
        if (target.InvokeRequired)
        {
            target.Invoke(action);
        }
        else
        {
            action();
        }
    }

    public static void PerformSafely<T1>(this Control target, Action<T1> action,T1 parameter)
    {
        if (target.InvokeRequired)
        {
            target.Invoke(action, parameter);
        }
        else
        {
            action(parameter);
        }
    }

    public static void PerformSafely<T1,T2>(this Control target, Action<T1,T2> action, T1 p1,T2 p2)
    {
        if (target.InvokeRequired)
        {
            target.Invoke(action, p1,p2);
        }
        else
        {
            action(p1,p2);
        }
    }
}

Wednesday, 18 February 2015

Draggable class for .net

Draggable

In some cases, it is handy to move controls on a form around by using your mouse. In this project, there is a helper class which does all the stuff needed to do this. Not only can a control be moved, but also its container.

Only one line of code is used to make a control movable:

Control.DoDraggable(true);

OR

Control.DoDraggable(false);



This code do the all hard work to drag and drop control from a location to other. One advantage of this is that the Draggable class has only static methods.


Most interesting thing in this class is, when you reposition a control on your form, your application will remember your setting next you start your application.


You can download this class from here.


 here

Using the code


To make draggable and change the position of single control:

Control.DoDraggable(true);



To make draggable and change the position of all controls on form control:



                foreach (Control x in this.Controls)
                {
                    x.DoDraggable(true);
                    if (x.Controls.Count > 0)
                    {
                        foreach (Control c in x.Controls)
                        {
                            c.DoDraggable(true);
                        }
                    }
                }



To save the current control position:


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.SaveState();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.GetState();
        }





Wednesday, 14 January 2015

Server Side MessageBox Control for ASP.NET

Server Side MessageBox Control for ASP.NET 


Sample Output

This control supports four type of messages :

  1. MessageBox1.ShowSuccess = "Your success message"; : Use to display success message
  2. MessageBox1.ShowError = "Your error message"; : Use to display error message
  3. MessageBox1.ShowInfo = "Your info message"; : Use to display info message
  4. MessageBox1.ShowWarning = "Your warning message"; : Use to display warning message

To use this Control you need to download from HERE

Steps to Use:
  • Open Visual Studio
  • Right click on ToolBox
  • Select Choose Items...
  • Browse to the location where you have downloaded Messagebox dll and select.
  • Drag and drop the MessageBox control on the Web Form.
  • Set Message show property as required : MessageBox1.ShowSuccess,
    MessageBox1.ShowError, MessageBox1.ShowInfo
    and
    MessageBox1.ShowWarning