L
Lloyd Dupont
I never really use SQLExpress before...
Anyway I'm trying to something a bit 'advanced' and I'm failing miserably.
I have written a BlogEngin a while ago using SqlServer & ASP.NET 2.0 Beta 2 and try to rewrite it from scratch (and copy paste). It's hard as I forgot all about ASP.NET...
Anyway, right now I'm trying to open a connection to and SqlServerExpress file. To make things a bit more difficult this file might not exist (as a matter of fact it doesn't exist now, it might be the reason of my trouble).
I'm trying to create a connection like that:
===
public static SqlConnection GetConnection(string filename)
{
string connString = @"Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|{0}.mdf;Integrated Security=True;User Instance=True";
connString = string.Format(connString, filename);
SqlConnection sqlc = new SqlConnection(filename); // <= exception here
return sqlc;
}
===
It fails with "Format of the initialization string does not conform to specification starting at index 0."
Then, somehow, I would like to detect if the DB exist or not and create the database automatically (I have the SQL creation script ready).
How could I do that?
Is my connection string alright? (except for the fact the file doesn't exist...)
How could I check if the file exist? Is something like:
File.Exists(MapPath(ResolveUrl(string.Format("~/App_Data/{0}.mdf", filename"))))
a good idea?
How could I create the required connection to call the creation script if the database/file doesn'ty exist?
Anyway I'm trying to something a bit 'advanced' and I'm failing miserably.
I have written a BlogEngin a while ago using SqlServer & ASP.NET 2.0 Beta 2 and try to rewrite it from scratch (and copy paste). It's hard as I forgot all about ASP.NET...
Anyway, right now I'm trying to open a connection to and SqlServerExpress file. To make things a bit more difficult this file might not exist (as a matter of fact it doesn't exist now, it might be the reason of my trouble).
I'm trying to create a connection like that:
===
public static SqlConnection GetConnection(string filename)
{
string connString = @"Data Source=.\SQLEXPRESS;AttachDbFileName=|DataDirectory|{0}.mdf;Integrated Security=True;User Instance=True";
connString = string.Format(connString, filename);
SqlConnection sqlc = new SqlConnection(filename); // <= exception here
return sqlc;
}
===
It fails with "Format of the initialization string does not conform to specification starting at index 0."
Then, somehow, I would like to detect if the DB exist or not and create the database automatically (I have the SQL creation script ready).
How could I do that?
Is my connection string alright? (except for the fact the file doesn't exist...)
How could I check if the file exist? Is something like:
File.Exists(MapPath(ResolveUrl(string.Format("~/App_Data/{0}.mdf", filename"))))
a good idea?
How could I create the required connection to call the creation script if the database/file doesn'ty exist?