Thursday, October 28, 2010

Restore Database from .bak file after terminating all connections

To enable or disable an user
--ALTER LOGIN emsdba ENABLE;
--ALTER LOGIN emsdba DISABLE;


To create a login for a DB User
EXEC sp_change_users_login 'Update_One', 'emsdba', 'emsdba';

Reset the password for a particular login
ALTER LOGIN emsdba WITH PASSWORD = 'emsdba$1';

To find the current logins
sp_who

To restore it is preferred to have a SINGLE_USER Connection from MULTI_USER Connection. Now this can be tricky as the current login needs to be the admin itself to prevent being locked out of the DB.

To get the logical names from the backup file
Restore FILELISTONLY from DISK ='C:\proto_db_201010270217.BAK'

Results from this above command is:

EMSMAIN d:\MSSQL\DATA\proto\emsmain.mdf D PRIMARY 52428800 35184372080640 1 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 1 NULL 1285000001202200041 EF9BAB6E-7891-4971-818A-4FE27166952B 0 1
EMSDATA1 d:\MSSQL\DATA\proto\emsdata1.ndf D EMSDATA1 78643200 35184372080640 3 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 2 NULL 1285000001202200041 EF9BAB6E-7891-4971-818A-4FE27166952B 0 1
EMSDATA2 d:\MSSQL\DATA\proto\emsdata2.ndf D EMSDATA2 518062080 35184372080640 4 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 3 NULL 1285000001202200041 EF9BAB6E-7891-4971-818A-4FE27166952B 0 1
EMSDATA3 d:\MSSQL\DATA\proto\emsdata3.ndf D EMSDATA3 623312896 35184372080640 5 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 4 NULL 1285000001202200041 EF9BAB6E-7891-4971-818A-4FE27166952B 0 1
EMSDATA4 d:\MSSQL\DATA\proto\emsdata4.ndf D EMSDATA4 263454720 35184372080640 6 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 5 NULL 1285000001202200041 EF9BAB6E-7891-4971-818A-4FE27166952B 0 1
EMSINDX1 d:\MSSQL\DATA\proto\emsindx1.ndf D EMSINDX1 52428800 35184372080640 7 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 6 NULL 1285000001202200041 EF9BAB6E-7891-4971-818A-4FE27166952B 0 1
EMSINDX2 d:\MSSQL\DATA\proto\emsindx2.ndf D EMSINDX2 151519232 35184372080640 8 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 7 NULL 1285000001202200041 EF9BAB6E-7891-4971-818A-4FE27166952B 0 1
EMSINDX3 d:\MSSQL\DATA\proto\emsindx3.ndf D EMSINDX3 100073472 35184372080640 9 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 8 NULL 1285000001202200041 EF9BAB6E-7891-4971-818A-4FE27166952B 0 1
EMSINDX4 d:\MSSQL\DATA\proto\emsindx4.ndf D EMSINDX4 59768832 35184372080640 10 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 9 NULL 1285000001202200041 EF9BAB6E-7891-4971-818A-4FE27166952B 0 1
EMSLOG d:\MSSQL\DATA\proto\emslog1.ldf L NULL 1073741824 35184372080640 2 0 0 00000000-0000-0000-0000-000000000000 0 0 0 512 0 NULL 0 00000000-0000-0000-0000-000000000000 0 0



-- Restore database
RESTORE database FFocus from disk ='C:\proto_db_201010270217.BAK'
with replace,move 'emsmain' to 'C:\OLDFFocus\emsmain.mdf',
move 'emsdata1' to 'C:\OLDFFocus\emsdata1.ndf',
move 'emsdata2' to 'C:\OLDFFocus\emsdata2.ndf',
move 'emsdata3' to 'C:\OLDFFocus\emsdata3.ndf',
move 'emsdata4' to 'C:\OLDFFocus\emsdata4.ndf',
move 'emsindx1' to 'C:\OLDFFocus\emsindx1.ndf',
move 'emsindx2' to 'C:\OLDFFocus\emsindx2.ndf',
move 'emsindx3' to 'C:\OLDFFocus\emsindx3.ndf',
move 'emsindx4' to 'C:\OLDFFocus\emsindx4.ndf',
move 'emslog' to 'C:\OLDFFocus\emslog1.ldf'

Wednesday, October 6, 2010

Loop through values from a LINQ ResultSet and post the file stored in a database

To iterate through a data retrieved from LINQ

SPDDataDataContext dataObj = new SPDDataDataContext();
    ISingleResult<SelectBidTab4ViewResult> selectObj = null;
protected void Page_Load(object sender, EventArgs e)
    {
        string clerkId = Session["Clerk_ID"].ToString();

        selectObj = dataObj.SelectBidTab4View(clerkId);
        BidTabView.DataSource = dataObj.SelectBidTab4View(clerkId); ;
        BidTabView.DataBind();

    }
    public void BidtabViewCommand(Object sender, GridViewCommandEventArgs e)
    {
        SPDDataDataContext dataObj = new SPDDataDataContext();
        byte[] buffer = null;
        string urlStr = "";
       
        if (e.CommandName == "Download")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string fileNUm = BidTabView.DataKeys[index].Value.ToString();
            GridViewRow selectRow = BidTabView.Rows[index];
            Label lblDesc = selectRow.FindControl("LabelDesc") as Label;
            string fileDesc = lblDesc.Text.ToString();
            string fileType = "";

            foreach (var dr in selectObj)
            {
                if(dr.File_Identity.ToString() == fileNUm)
                {
                    string fileContent = dr.FileName.ToString();
                    if (dr.FileName.ToString() != "")
                    {
                        buffer = dr.SPD_File.ToArray();
                        fileType = dr.File_Type.ToString();
                    }
                    else
                    {
                        urlStr = fileDesc;
                    }
                }
            }

            if (fileType != "")
            {
                if (fileType == "doc")
                {
                    Response.ContentType = "Application/msword";
                }
                else if (fileType == "pdf")
                {
                    Response.ContentType = "Application/pdf";
                }
                else if (fileType == "xls")
                {
                    Response.ContentType = "Application/x-msexcel";
                }
                else if (fileType == "html")
                {
                    Response.ContentType = "text/HTML";
                }
                else if (fileType == "gif")
                {
                    Response.ContentType = "image/GIF";
                }
                else if (fileType == "jpeg")
                {
                    Response.ContentType = "image/JPEG";
                }

                Response.AddHeader("content-length", buffer.Length.ToString());
                Response.BinaryWrite(buffer);
            }
            else
            {
                Response.Redirect(urlStr);
            }
        }//e.CommandName == "Download"
}

Tuesday, October 5, 2010

Install MSMQ 3.0 in Windows 2003 Server

To install the MSMQ 3.0 in Windows Server 2003 follow these quick ad easy steps:

1. Open Control Panel and Add and Remove Programs.
2. Choose the Add/Remove Windows Components.
3. Choose the Application Server and Click Details button.
4. Check the Messaging Queue in the list and click OK -> Next -> Finish.
5. Now Open the Administrative Tools -> Computer Management.
6. In the Services and Applications Node - > Pick the Services and this opens all the Services.
7.Pick the Message Queuing Services -> Right Click and choose Properties.
8.In Recovery Tab for First failure, Second Failure and Subsequent Failure choose Restart the Service
and Choose OK

Note: Don't choose the Message Queuing Folder for this setup.

Tada!! and you have installed the MSMQ on Windows 2003.