i wrote a sample with c# but when i use relative connectionstring it doesnt save data to database and doesnt make any exception or error even:
private void AddButton_Click(object sender, EventArgs e)
{
using (DataBaseModelDataContext DB = new DataBaseModelDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DataBase.mdf;Integrated Security=True;User Instance=True"))
{
Person person = new Person { Name=NameField.Text,LastName=LastNameField.Text};
DB.Persons.InsertOnSubmit(person);
DB.SubmitChanges();
MessageBox.Show("Add successfully");
}
}
but when i change connection string it works:
using (DataBaseDataContext DB = new DataBaseDataContext(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Herald开发者_Python百科\Documents\Visual Studio 2010\Projects\LinqToSql\LinqToSql\DataBase.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
but i need to use relative connection string what should i do:
You'll need to get the current directory and map it into the string.
path = New Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;
path = IO.Path.GetDirectoryName(path);
精彩评论