I am writing an application in VB.Net, the application stores a number of images within the database and therefore the SQL database size gets quite large. At the moment I use a single database, however what I want to do is create a new database for each project within my application. I need some advise on the best way to proceed with this and also the best way to be able to create a new database for each project. Below is the approach that I was planning to adopt:
1. When the application is installed it automatically has a common database that comes with the application and this database is attached to the SQL Server, lets call this application "AppA" and common database "AppADB"
Within the AppADB there would be a table called projectsTbl and in this projectsTbl it has a 2 columns called "projectDesc" and "projectDB". projectDesc would hold the description of a project and then the projectDB would hold the SQL database name for that project.
2. The user would login to my application and in the admin side of the application have a create project form. In this form he would enter the project description and also a 3 character code for the project. When the user OK's the form it would take the project descrption from the form and put this in the "projectDesc' field in the AppADB and then take the 3 character code from the form add this as a prefix to "AppADB" and put this within the "projectDB" field within the AppADB e.g. if the 3 character code was "SIM" then the projectDB name would be "SIMAppADB"
3. What I obviously now need to do is create a new database called "SIMAppADB" now what I was thinking was I could create a SQL database backup of a database that has the standard database structure that is required and then I can restore this .bak file (which would be in my application installed directory) to this new name, using the following code:
4. Then when the user logins to my application again, on the login screen he will see a list of projects from the "projectsTbl" table in the "AppADB" database. The user can then select the project they wish to login to and from this the application and extract the database name from the "projectDB" table in the "AppADB" database. I then can store this database name in my application and when the application needs to connect to the database it modifies the connection string to connect to the respective database.
Noew this is where I need the advise, would that be a good way to proceed? Also the user who is creating the new project within my application may not have access to the SQL server, and therefore I need for a normal user to be able to create a new database and be able to attach it to the SQL server. Would they need to know the sa username and password, or could I create a SQL user when my application is installed by the DBA to allow this to be done?
Any help and guidance would be really apreshiated, I hope I have explained this well enough.
Thanks in advance
Simon
1. When the application is installed it automatically has a common database that comes with the application and this database is attached to the SQL Server, lets call this application "AppA" and common database "AppADB"
Within the AppADB there would be a table called projectsTbl and in this projectsTbl it has a 2 columns called "projectDesc" and "projectDB". projectDesc would hold the description of a project and then the projectDB would hold the SQL database name for that project.
2. The user would login to my application and in the admin side of the application have a create project form. In this form he would enter the project description and also a 3 character code for the project. When the user OK's the form it would take the project descrption from the form and put this in the "projectDesc' field in the AppADB and then take the 3 character code from the form add this as a prefix to "AppADB" and put this within the "projectDB" field within the AppADB e.g. if the 3 character code was "SIM" then the projectDB name would be "SIMAppADB"
3. What I obviously now need to do is create a new database called "SIMAppADB" now what I was thinking was I could create a SQL database backup of a database that has the standard database structure that is required and then I can restore this .bak file (which would be in my application installed directory) to this new name, using the following code:
Code:
RESTORE DATABASE SIMAppDB
FROM DISK = 'C:\Program Files\AppA Installed Directory\DB.Bak'
WITH REPLACE, MOVE SIMAppDB_Data' TO 'C:\MSSQL\DATA\SIMAppDB_Data.MDF',
MOVE 'SIMAppDB_Log' TO 'C:\MSSQL\DATA\SIMAppDB_Log.LDF'
4. Then when the user logins to my application again, on the login screen he will see a list of projects from the "projectsTbl" table in the "AppADB" database. The user can then select the project they wish to login to and from this the application and extract the database name from the "projectDB" table in the "AppADB" database. I then can store this database name in my application and when the application needs to connect to the database it modifies the connection string to connect to the respective database.
Noew this is where I need the advise, would that be a good way to proceed? Also the user who is creating the new project within my application may not have access to the SQL server, and therefore I need for a normal user to be able to create a new database and be able to attach it to the SQL server. Would they need to know the sa username and password, or could I create a SQL user when my application is installed by the DBA to allow this to be done?
Any help and guidance would be really apreshiated, I hope I have explained this well enough.
Thanks in advance
Simon