Monday, 28 July 2014

Add primary key constraint in MySql syntax

syntax for an primary key in My Sql

Create Table tablename
(
      [columnname] [datatype] [columnconstraint] [table constraint] [column name]
);


Example:

Create Table Table1
(
      ID Not Null auto_Increment,
      Name varchar(256) ,
      primary key (ID)
);

Happy Scripting :)

Friday, 20 June 2014

Add new column in existing table with default value in sql server

To perform this process just follow this,


Syntex:

Alter table <table_Name>
Add column <column_Name> null/not null default


Query:

let say user table has column username,password. now i am adding createdon(date), to achieve this

Alter table user
add column createdon null default(getdate())


Result:
along with existing data new column will added as well as value also.

happy coding :)

Thursday, 12 June 2014

Difference Between Sql Server VARCHAR and NVARCHAR Data Type

Refer this following link

http://sqlhints.com/2011/12/23/difference-between-varchar-and-nvarchar/

http://social.msdn.microsoft.com/Forums/en-US/b6615caa-629b-45ac-9710-aab7f6e172b3/varchar-and-nvarchar-use-what-is-exact-difference-between-these?forum=sqldataaccess

Monday, 12 May 2014

How to retrieve binary image from database using C# in ASP.NET

Please follow these steps:


Step1: In aspx page add a image control 


<asp:Image ID="img" runat="server"  />

Step2: In code behind add the following code 


protected void Page_Load(object sender, EventArgs e)
        {
            string strCon = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            SqlConnection con = new SqlConnection(strCon);
            SqlCommand cmd = new SqlCommand("getimage", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            con.Open();
            if (con.State== ConnectionState.Open)
            {
                cmd.ExecuteNonQuery();
                DataTable dt = new DataTable();
                da.Fill(dt);
                byte[] image = (byte[])dt.Rows[0][0];
                img.ImageUrl = "data:image/png;base64 ," + Convert.ToBase64String(image, 0, image.Length);
            }
           

        }

Step3: In Web.config


<add name="DB"
         connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=dbname;Data Source=servername;"
         providerName="System.Data.SqlClient" />

Monday, 28 April 2014

how to get nth highest and lowest salary in sql server

Ex Code: Lowest case :

select top 1 column as 'salary' from (select distinct top 4 column from  Table order by ID asc) a order by ID desc

Highest Case:

select top 1 column as 'salary' from (select distinct top 4 column from  Table order by ID desc) a order by ID asc

Tuesday, 1 April 2014

linq get second highest number or salary

Use this query:

var employees = Employees
    .GroupBy(e => e.Salary)
    .OrderByDescending(f => f.Key)
    .Skip(1).Take(1)
    .First();