开发者

ConnectionString in LinqToSQL

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜