I'm creating a setup project for my C# desktop application.
What the data source should be written in the connection string for the access database ?and where I should put my database file in the solution project ?
Assuming you're using the VS setup project, you need to add the access database file as content and place it in the application directory, for example. To specify the location in the configuration file, you need to write a custom action that modifies the connection string accordingly.
The following example is an installer class that sets the connection string after install phase (not tested):
[RunInstaller(true)]
public partial class Installer1 : System.Configuration.Install.Installer
{
public Installer1()
{
InitializeComponent();
this.AfterInstall += new InstallEventHandler(Installer1_AfterInstall);
}
void Installer1_AfterInstall(object sender, InstallEventArgs e)
{
string sTargetDir = Context.Parameters["TargetDir"];
string sAppConfig = Path.Combine(sTargetDir, "<your app>.exe.config");
string sDBPath = Path.Combine(sTargetDir, "<your db>.mdb");
XDocument doc = XDocument.Load(sAppConfig);
var elem = doc.Root.Element("/configuration/connectionStrings/add[#name='<your connection name>']");
string connectionString = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", sDBPath);
elem.SetAttributeValue("connectionString", connectionString);
doc.Save(sAppConfig);
}
}
Alternatively, you can use Wix which has the XmlFile utility in the util extension which does it for you without you writing a custom action.
Related
I am creating an ASP.Net MVC application and I have created a new console application just so that I can pass it a few parameters and use the DataContext in the MVC application so that I dont have to continually repeat myself.
This is the code that I am using
using mySite.WebSite.DataModel;
namespace mySite.AvailabilityManager
{
class Program
{
public static List<DateTime>Availability = new List<DateTime>();
static void Main(string[] args)
{
var startingDt = Convert.ToDateTime("04-09-2015 08:00:00");
var endingDt = Convert.ToDateTime("04-09-2015 17:00:00");
CreateAvailabilty(startingDt, endingDt);
AddAvailabilityToDatabase();
}
public static void CreateAvailabilty(DateTime startingDt, DateTime endingDt)
{
var hoursDiff = endingDt.Subtract(startingDt);
for (int i = 0; i < hoursDiff.Hours; i++)
{
Availability.Add(startingDt);
startingDt = startingDt.AddHours(1);
}
}
public static void AddAvailabilityToDatabase()
{
using (var db = new FitnessForAllContext())
{
foreach (var availableDate in Availability.Select(date => new AvailableDate {DateAvailable = date}))
{
db.AvailableDates.Add(availableDate);
db.SaveChanges();
}
}
}
}
When I get to db.AvailableDates.Add(..) I get this error
No connection string named 'MyDBContext' could be found in the application config file.
I was under the impression that because I am using the reference from my MVC application and the connection string is in the ASP.Net MVC config file that I would not have to repeat the connection string in my app.config file for the console application.
So, to summaries,
I have the MVC Project refernece in my console application
This fails because of the lack of a connection string at db.AvailableDates.Add(availableDate);
The mySite.Website assembly is being pulled through into my bin debug folder
If you could offer some insight as to what I need to do without having to continually repeat myself by adding the connection string everywhere I intend on using this, unless I REALLY have to repeat myself
Standard, the connection string needs to be in de config file of the startup project. In this case of the console application. The config of the referenced project is ignored.
You can have a constant or an embedded resource or anything IN your EntityFramework project that contains connection string. But I think, it's not a good practice, every executing project should have it's own configuration.
I have created a class library and added a EF Model but as soon as I declare a variable my project just skip the rest of my code without any error. I do not understand what is causing this to happen.
Library Code
public class Phisc
{
//Global DB Entity variable
live_restoreEntities db = new live_restoreEntities();
//Write data to file
public void PhiscFile(int value)
{
string strFileName, strFilePath;
StreamWriter stm;
//Create a file name that will be created
strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_PHISC";
//The path that were the file will be saved
strFilePath = "c:\\" + strFileName + ".txt";
//Validate if file exists
if (!System.IO.File.Exists(strFilePath))
System.IO.File.Create(strFilePath).Dispose();
stm = new StreamWriter(strFilePath, false);
stm.Write("This is a test message from C#");
stm.Close();
}
}
WinForm Code
private void Form1_Load(object sender, EventArgs e)
{
Phisc.Phisc pFile = new Phisc.Phisc();
pFile.PhiscFile(14);
}
When I create a instance of the library it does not hit my PhiscFile Method.
I have added a breakpoint to it and it stops at this constructor
public live_restoreEntities() : base("name=live_restoreEntities", "live_restoreEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
I am using a windows application to test my library
The parameterless constructor goes out and look for the conenctionstring in the App.config file. It look next to the .exe file.
I'm guessing that you need to include your App.config (from your entity library) to your WinForms library.
In the App.config, it should look like this:
<configuration>
<connectionStrings>
<add name="live_restoreEntities"
connectionString="<your connection string here>"
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
I need to create an address string in app.config as:
<client>
<endpoint address="http://ServerName/xxx/yyy.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IClientIInfoService"
contract="DocuHealthLinkSvcRef.IClientIInfoService" name="BasicHttpBinding_IClientIInfoService" />
</client>
The ServerName need to be entered by the user during installation.
For that i have created a new UI dialog in the Installer. I have also written an Installer.cs class and overrided the install () as:
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
string targetDirectory = Context.Parameters["targetdir"];
string ServerName = Context.Parameters["ServerName"];
System.Diagnostics.Debugger.Break();
string exePath = string.Format("{0}myapp.exe", targetDirectory);
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
config.AppSettings.Settings["ServerName"].Value = ServerName;
config.Save();
}
}
But how do i use this ServerName in my app.config to create the specified string.
I'm working on VS2010.
You could use WiX (Windows Installer XML toolset) to build your MSI, in which case you can use the XmlFile utility tag to update the server name:
<util:XmlFile Id="UpdateServerName" File="[INSTALLLOCATION]AppName.exe.config" Action="setValue" ElementPath="/client/endpoint" Name="address" Value="http://[SERVERNAME]/xxx/yyy.svc" />
You can capture the server name during installation using a WixUI extension form.
Advantages of WiX: WiX is msbuild compliant (unlike .vdproj files), and gives you much finer-grained control over your installer, among other things
Assuming you are using the full ServiceModel section group in the app.config
Essentially you follow these steps:
Load ServiceModel config section
Get Client Section
Get ChannelEndpoint Element
Change Address value by replacing string "ServerName" with entered value
Set Address attribute to new value
Save config
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
string targetDirectory = Context.Parameters["targetdir"];
string ServerName = Context.Parameters["ServerName"];
System.Diagnostics.Debugger.Break();
string exePath = string.Format("{0}myapp.exe", targetDirectory);
Configuration config = ConfigurationManager.OpenExeConfiguration(exePath);
config.AppSettings.Settings["ServerName"].Value = ServerName;
//Get ServiceModelSectionGroup from config
ServiceModelSectionGroup group = ServiceModelSectionGroup.GetSectionGroup (config);
//get the client section
ClientSection clientSection = group.Client;
//get the first endpoint
ChannelEndpointElement channelEndpointElement = clientSection.Endpoints[0];
//get the address attribute and replace servername in the string.
string address = channelEndpointElement.Address.ToString().Replace("ServerName", ServerName);
//set the Address attribute to the new value
channelEndpointElement.Address = new Uri(address);
config.Save();
}
At the end of the day, app.config is xml file. You can use Linq To XML or XPathNavigator to replace the address attribute of endpoint element.
Below code uses Linq to Xml
using System.Xml.Linq;
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
string targetDirectory = Context.Parameters["targetdir"];
string ServerName = Context.Parameters["ServerName"];
System.Diagnostics.Debugger.Break();
string configPath = string.Format("{0myapp.exe.config", targetDirectory);
XElement root = XElement.Load(configPath);
var endPointElements = root.Descendants("endpoint");
foreach(var element in endPointElements)
{
element.Attribute("address").Value = ServerName;
}
root.Save(configPath);
}
}
Since you have a windows-installer tag, I assume you either have an MSI package, or can create one...
Then:
You can create a public MSI property like ENDPOINTSERVER you require
during installation.
Add a custom action that modifies app.config to
run after "InstallFinalize" with the value of ENDPOINTSERVER
A silent installation will be possible using:
msiexec /i app.msi ENDPOINTSERVER=www.MyServer.com /qb-
try to use below two lines before saving the config file changes:
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("Section Name");
root.Save(configPath);
P.S: it doesn't update the solution item 'app.config', but the '.exe.config' one in the bin/ folder if you run it with F5.
i have problems in reading DLL application setting in c# Visual Studio 2010.
I post a sample code of the get workarounded using reflection because with the ConfigurationManager fails.
private string LDAPDomain
{
get
{
string strPath = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
string val = GetValues(strPath, "LDAPDomain");
return val;
}
}
//strPath is path of the file.
//strKey is key to access
private string GetValues(string strPath, string strKey)
{
System.Configuration.Configuration con = System.Configuration.ConfigurationManager.OpenExeConfiguration(strPath);
string strValue = con.AppSettings.Settings[strKey].Value;
return strValue;
}
If you are expecting the main project referencing the DLL to pick up the app settings it doesn't work like that. The ConfigurationManager will read the config for the executing assembly, you need to put all the necessary configuration into your app if you want to use this.
Alternatively you can manually read the contents of your DLL's app.config file - see this question for some example code.
I am currently in the middle of creating an app that uses a sql CE database, I have made this database deploy-able with the application, however the problem I'm having at the moment is I need to run TestMethods but this is erroring out when it doesn't find the database as its looking in the "testingProject" folder under debug or release as that is it's Data Directory
using (SqlCeConnection sqlCon = new SqlCeConnection(#"Data Source=|DataDirectory|\database.sdf;Persist Security Info=False;"))
The code above is my connection string, so I'm guessing that means that the test is running and searching for a database in its own data directory
Any help on what I could do without changing the database connection string, database location and still leaving my application deployable? or am I asking something impossible?
EDIT
[TestMethod]
public void TestForReadingFromDB()
{
List<string> list = class.readDB();
Assert.IsNotNull(list);
Assert.AreNotEqual(0, list.Count);
}
just added in the test method that's currently failing
In the test project you can override the DataDirectory location using
AppDomain.CurrentDomain.SetData("DataDirectory", <PATH_TO_DATA_DIRECTORY>);
For instance in my app.config file the testing projects I have
<appSettings>
<add key="DataDirectory" value="..\..\Database"/>
</appSettings>
In my test fixture base I have:
var dataDirectory = ConfigurationManager.AppSettings["DataDirectory"];
var absoluteDataDirectory = Path.GetFullPath(dataDirectory);
AppDomain.CurrentDomain.SetData("DataDirectory", absoluteDataDirectory);
This sets the DataDirectory to the folder /Database under the test project folder structure.
Once I drop or create a copy of the database in there I can easily run Integration Tests.
this is how I specify the data directory path for testing in my initialize data class
public class TestClasse
{
public TestClass()
{
GetAppDataDirectoryForTesting();
}
private static string GetAppDataDirectoryForTesting()
{ //NOTE: must be using visual studio test tools for this to work
string path = AppDomain.CurrentDomain.BaseDirectory;
var dirs = path.Split(Path.DirectorySeparatorChar);
var appDataPath = "";
for (int i = 0; i < dirs.Length - 3; i++)
{
appDataPath += dirs[i] + Path.DirectorySeparatorChar.ToString();
}
appDataPath = appDataPath + "[foldername(i.e. in my case project name)]" + Path.DirectorySeparatorChar.ToString() + "App_Data";
return appDataPath;
}
[TestMethod]
public void SomeTestMethod()
{
....test code
}
}