may i know how to add sqlexpress database to my visual studio project while iam deploying...
(i tried by simply adding database into my project through solution explore before deploy , but while instaling deployed project into client machine ,on that time i cant access the data's from database.)
can you tell me how to do this process in detail.
Loading
Bechir BejaouiPosted Apr 24, 2008, 9:30 PM
Add an Installer class to your application
Add a data connection object as follow
-
-
-
-
-
-
-
-
Add a new item TextFile into your project name it dbscript.txt for instanceIn Server Explorer, select Data Connections. Right-click and choose Add Connection.
In the Choose Data Source dialog box, select Microsoft SQL Server.
In the Add Connection dialog box, do the following:
In the Server name drop-down list, type or select a server name.
Select Use Windows Authentication.
In the database box, type master.
Click OK to close the dialog box.
From the Data menu, click Add New Data Source, then use the wizard to add the connection that you established in the previous steps. To verify that the data source is in the project, click Show Data Sources on the Data menu.
and put all the necessary script for creating you sql data base
using System.IO;In Solution Explorer, select sql.txt. In the Properties window, set the BuildAction property to Embedded Resource.
Add this code to the Installer class
using System.Collection;
private string GetSql(string Name)
{
try {
// Gets the current assembly.
Assembly Asm = Assembly.GetExecutingAssembly();
// Resources are named using a fully qualified name.
Stream strm = Asm.GetManifestResourceStream(Asm.GetName().Name + "." + Name);
// Reads the contents of the embedded file.
StreamReader reader = new StreamReader (strm);
return reader.ReadToEnd();
}
catch (Exception ex) {
Interaction.MsgBox("In GetSQL: " + ex.Message);
throw ex
}
}
private void ExecuteSql(string DatabaseName, string Sql){
SqlCommand Command = new SqlCommand(Sql, sqlConnection1);
Command.Connection.Open();
Command.Connection.ChangeDatabase(DatabaseName);
try {
Command.ExecuteNonQuery();
}
finally {
// Finally, blocks are a great way to ensure that the connection
// is always closed.
Command.Connection.Close();
}
}
protected void AddDBTable(string strDBName)
{
try {
// Creates the database.
ExecuteSql("master", "CREATE DATABASE " + strDBName);
// Creates the tables.
ExecuteSql(strDBName, GetSql("sql.txt"));
}
catch (Exception ex) {
// Reports any errors and abort.
Interaction.MsgBox("In exception handler: " + ex.Message);
throw ex;
}
}
public override void Install( System.Collection.IDectionary stateSaver)
{
base.Install(stateSaver);
AddDBTable(this.Context.Parameters.Item("dbname"));
}
On the File menu, click Add, and then click New Project.
In the Add New Project dialog box, open the Other Project Types node and select Setup and Deployment Projects in the Project Type pane. Then select Setup Project in the Templates pane. In the Name box, type DBCustomAction Installer.
In the Properties window, select the ProductName property and type DB Installer.
In the File System Editor, select the Application Folder. On the Action menu, click Add, and then click Project Output.
In the Add Project Output Group dialog box, select Primary output for the DBCustomAction project.
Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and choose User Interface.
In the User Interface Editor, select the Start node under Install. On the Action menu, choose Add Dialog.
In the Add Dialog dialog box, select the Textboxes (A) dialog, then click OK.
On the Action menu, choose Move Up. Repeat until the Textboxes (A) dialog is above the Installation Folder node.
In the Properties window, select the BannerText property and type Specify Database Name.
Select the BodyText property and type This dialog allows you to specify the name of the database to be created on the database server.
Select the Edit1Label property and type Name of database:.
Select the Edit1Property property and type CUSTOMTEXTA1.
Select the Edit2Visible, Edit3Visible, and Edit4Visible properties and set them to False.
Select the DBCustomAction Installer project in Solution Explorer. On the View menu, point to Editor, and then click Custom Actions.
In the Custom Actions Editor, select the Install node. On the Action menu, choose Add Custom Action.
In the Select item in project dialog box, double-click the Application Folder.
Select Primary output from DBCustomAction (Active), then click OK to close the dialog box.
Make sure that Primary output from DBCustomAction (Active) item is selected in the Custom Actions Editor. In the Properties window, select the CustomActionData property and type /dbname=[CUSTOMTEXTA1].
On the Build menu, choose Build DBCustomActionInstaller.
-
-
That's allIn Windows Explorer, navigate to your project directory and find the built installer. The default path will be \documents and settings\yourloginname\DBCustomAction Installer\project configuration\DBCustomAction Installer.msi. The default project configuration is Debug.
Copy the DBCustomAction Installer.msi file and all other files and subdirectories in the directory to another computer.