playing with ASP.NET & SqlServer express

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?
 
K

Karl Seguin

shouldn't you be passing connString into the code?

Personally, instead of checking for the database file, I'd simply connect to MASTER, and see if the database exists

select * from sysdatabases
where name = 'whatever'


you can returna value, if it exists, and run the creation script if not.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
http://openmymind.net/redirector.aspx?documentId=51 - Learn about AJAX!



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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top