Tuesday, 12 August 2014

run asp.net web application from pendrive using iis server , run website from pendrive, run website from pendrive using iis

run an asp.net website or web application from pen drive using iis server .

please follow these steps

1) configure iis in computer

2) copy web application and database in pendrive

3) connect application from iis as well as attach database mdf file in sql server

4) set connection string in web config using sql server name

5) only one issue will come , that will be like , when u will remove pen drive and reconnect it again then it will not work and will give error.

reason of error : actually what happend when u remove the pendrive and reconnect it then sql server  does not recognize the sql server database files from that path,

so we need to detach the database from sql server then reattach it for make it work.

but its very time consuming task to do it mannualy so what we will do is

we will put a code in our application start up that will do this by code.

means we will detach the database file from c# code.

 try
            {
                SqlConnection sqlConnection1 = new SqlConnection("Server=COMPUTER-001;Database=master;Integrated Security=true");
                SqlCommand cmd = new SqlCommand();


                cmd.CommandText = "ALTER DATABASE EasyInventory  SET SINGLE_USER WITH ROLLBACK IMMEDIATE   EXEC sp_detach_db 'EasyInventory'";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = sqlConnection1;

                sqlConnection1.Open();

                cmd.ExecuteNonQuery();
                // Data is accessible through the DataReader object here.

                sqlConnection1.Close();


                string fullPath = "D:\\NewEasyInventory\\Database\\EasyInventory.mdf";
                DirectoryInfo dInfo = new DirectoryInfo(fullPath);
                DirectorySecurity dSecurity = dInfo.GetAccessControl();
                dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
                dInfo.SetAccessControl(dSecurity);

                string fullPath2 = "D:\\NewEasyInventory\\Database\\EasyInventory_log.ldf"; ;
                DirectoryInfo dInfo2 = new DirectoryInfo(fullPath2);
                DirectorySecurity dSecurity2 = dInfo2.GetAccessControl();
                dSecurity2.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
                dInfo2.SetAccessControl(dSecurity2);
            }

            catch (Exception ex)
            {
            }


here we have two codes,

one is to detach the sql server database file

second is to give everyone permission on database mdf and ldf files in pendrive.

so u need to set the correct path of mdf and ldf files on ur pen drive

for further help reply.

thanks

1 comment:

  1. sharepoint is a web based collaborative platform.it is basically an intranet and content management system.you can get more information at sharepoint online training.Read More At sharepoint online training hyderabad

    ReplyDelete