I have looked through this forum and other Google searched sites for an answer to this question with no luck.
I have a product database named 'prodDb.mdf' constructed in Visual C# 2010 Express and would like to use this data with Webmatrix 2, but I 'm having trouble including the database. I put the .mdf file into the App_Data folder (also tried adding the .ldf)
and modified the AppStart.cshtml file . I don't know what to do next. I thought I tested this previously with success. If so, I should have taken some notes because I'm stuck now.
I thought Visual C# would attach the database to the server?. I looked in C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA where prodDb.mdf and .log files are listed, but I noticed these files are approximately 2 meg and in Visual C#
they are closer to 37 meg? (the data includes several hundred jpg, png and bmp files) so I copied in the larger files with no change to the result.
Also in Microsoft SQL Server 2008 the database was listed, but the table and data don't appear? This is the same as in WebMatrix. Any suggestions on what I should I try next? Meanwhile, I will keep searching.
Put your mdf and ldf files in the App_Data folder in your site for this to work. Then in WebMatrix, you still need to use the Add New Connection in the databsaes workspace to be able to see the database. and its tables.
Obviously, I'm not understanding something basic here.
For testing in WebMatrix I am using the Bakery template.
In Webmatrix,
I right-click on the App_Data folder
Select “Add Existing File...” and add the .mdf and .ldf files from Visual C# bin\Debug folder. - no problems.
Then, going to the database files, I right-click on the top level and select “New SQL Server Connection”.
Add the database name (with no extension), Server .\SQLEXPRESS and select Windows Authentication.
The web.config file now has a new connection string listed and the database file name is listed in the Webmatrix folder-file tree list on the left side.
I still have no data table and still don't know what to do next.
Having to move on I changed my Visual C# file to write row data to a comma delimited text file and plan to use the .Split() method in Webmatrix to get the data to store in a SQLCE (sdf) database. The images I just copied to a folder and moved the whole folder
into Webmatrix. I now have a different problem that I am starting as a new thread.
To close this thread, I am assuming Mike's solution is correct and I am still not getting it.
Sorry if I make you some questions, but I haven't clear your problem:
have you installed on your system SQL Server Management Studio?
If yes
do you see your database (prodDb?) in SSMS Object Explorer?
if you right click on one of its tables and choose "Select top 1000 rows", do you see the rows that you expect are in the table?
If no
install it.
Once secure that your database is rightly attached to your sql server instance, add a web.config file like this in the root directory of your WebMatrix site (where I state that prodDb is the name of your database):
Thanks GnGregori, I made some progress, but still no data in Webmatrix (WM).
The correct database and data do show up in
SQL Server Management Studio after I loaded the prodDb.mdf file from Visual C# into
C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA. I then put prodDb.mdf and log.ldf into the WB App_Data folder and loaded the .mdf file into the WM Databases section at the top using "New
SQL Server Connection". The database name is now correctly shown and also a heading for
Tables, but there are no Table names or any data. I also modified the web.config file as you suggested.
If I right-click on the prodDb.mdf file name in WB and click "copy connection string" I get "server=.\SQLEXPRESS;database=prodDb.mdf;trusted_connection=True".
You mustn't copy your .mdf and .ldf files into App_Data: this procedure is only valid with Sql Server Compact .sdf files.
If your database is rightly attached to the SQL Express Server, the connection string is what you need for using your database with the Webmatrix site.
Try to cancel your .mdf and .ldf files from the App_Data folder.
Marked as answer by katiej on Jul 30, 2012 11:58 PM
katiej
Member
3 Points
12 Posts
Using .mdf Database with Webmatrix
Jul 20, 2012 03:44 PM|LINK
I have looked through this forum and other Google searched sites for an answer to this question with no luck.
I have a product database named 'prodDb.mdf' constructed in Visual C# 2010 Express and would like to use this data with Webmatrix 2, but I 'm having trouble including the database. I put the .mdf file into the App_Data folder (also tried adding the .ldf) and modified the AppStart.cshtml file . I don't know what to do next. I thought I tested this previously with success. If so, I should have taken some notes because I'm stuck now.
Thanks Jim
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Using .mdf Database with Webmatrix
Jul 20, 2012 06:06 PM|LINK
You need to attach the database to your SQL Server: http://msdn.microsoft.com/en-us/library/ms190209(v=sql.100).aspx
Then add a connection string to the web.config file: http://www.connectionstrings.com/sql-server-2008
Then provide the name of the connection string in the Database.Open() method.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
katiej
Member
3 Points
12 Posts
Re: Using .mdf Database with Webmatrix
Jul 21, 2012 08:21 AM|LINK
Mike,
Thanks for your response.
I thought Visual C# would attach the database to the server?. I looked in C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA where prodDb.mdf and .log files are listed, but I noticed these files are approximately 2 meg and in Visual C# they are closer to 37 meg? (the data includes several hundred jpg, png and bmp files) so I copied in the larger files with no change to the result.
Also in Microsoft SQL Server 2008 the database was listed, but the table and data don't appear? This is the same as in WebMatrix. Any suggestions on what I should I try next? Meanwhile, I will keep searching.
Jim
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Using .mdf Database with Webmatrix
Jul 21, 2012 08:30 AM|LINK
You can get the framework to auto-attach your database if you are using SQL Express. You just need to pick the right connection string:
Attach a database file, located in the data directory, or connect to a local SQL Server Express instance
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf; Database=dbname;Trusted_Connection=Yes;
[From http://www.connectionstrings.com/sql-server-2008]
Put your mdf and ldf files in the App_Data folder in your site for this to work. Then in WebMatrix, you still need to use the Add New Connection in the databsaes workspace to be able to see the database. and its tables.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
katiej
Member
3 Points
12 Posts
Re: Using .mdf Database with Webmatrix
Jul 23, 2012 12:02 PM|LINK
Obviously, I'm not understanding something basic here.
For testing in WebMatrix I am using the Bakery template.
In Webmatrix,
I right-click on the App_Data folder
Select “Add Existing File...” and add the .mdf and .ldf files from Visual C# bin\Debug folder. - no problems.
Then, going to the database files, I right-click on the top level and select “New SQL Server Connection”.
Add the database name (with no extension), Server .\SQLEXPRESS and select Windows Authentication.
The web.config file now has a new connection string listed and the database file name is listed in the Webmatrix folder-file tree list on the left side.
I still have no data table and still don't know what to do next.
Jim
katiej
Member
3 Points
12 Posts
Re: Using .mdf Database with Webmatrix
Jul 28, 2012 07:07 PM|LINK
Having to move on I changed my Visual C# file to write row data to a comma delimited text file and plan to use the .Split() method in Webmatrix to get the data to store in a SQLCE (sdf) database. The images I just copied to a folder and moved the whole folder into Webmatrix. I now have a different problem that I am starting as a new thread.
To close this thread, I am assuming Mike's solution is correct and I am still not getting it.
Jim
GmGregori
Contributor
5470 Points
737 Posts
Re: Using .mdf Database with Webmatrix
Jul 28, 2012 11:13 PM|LINK
Sorry if I make you some questions, but I haven't clear your problem:
have you installed on your system SQL Server Management Studio?
If yes
If no
Once secure that your database is rightly attached to your sql server instance, add a web.config file like this in the root directory of your WebMatrix site (where I state that prodDb is the name of your database):
<?xml version="1.0" encoding="UTF-8"?> <configuration> <connectionStrings> <add connectionString="Trusted_Connection=True;Server=.\SQLEXPRESS;Database=prodDb" name="prodDb" providerName="System.Data.SqlClient" /> </connectionStrings> </configuration>Now you should see your database in the Webmatrix database workspace.
katiej
Member
3 Points
12 Posts
Re: Using .mdf Database with Webmatrix
Jul 30, 2012 06:00 PM|LINK
Thanks GnGregori, I made some progress, but still no data in Webmatrix (WM).
The correct database and data do show up in SQL Server Management Studio after I loaded the prodDb.mdf file from Visual C# into C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA. I then put prodDb.mdf and log.ldf into the WB App_Data folder and loaded the .mdf file into the WM Databases section at the top using "New SQL Server Connection". The database name is now correctly shown and also a heading for Tables, but there are no Table names or any data. I also modified the web.config file as you suggested.
If I right-click on the prodDb.mdf file name in WB and click "copy connection string" I get "server=.\SQLEXPRESS;database=prodDb.mdf;trusted_connection=True".
Jim
GmGregori
Contributor
5470 Points
737 Posts
Re: Using .mdf Database with Webmatrix
Jul 30, 2012 06:17 PM|LINK
You mustn't copy your .mdf and .ldf files into App_Data: this procedure is only valid with Sql Server Compact .sdf files.
If your database is rightly attached to the SQL Express Server, the connection string is what you need for using your database with the Webmatrix site.
Try to cancel your .mdf and .ldf files from the App_Data folder.
Mikesdotnett...
All-Star
154927 Points
19867 Posts
Moderator
MVP
Re: Using .mdf Database with Webmatrix
Jul 30, 2012 06:58 PM|LINK
It is also valid if you want to autoattach a SQL Server database to SQL Express.
http://msdn.microsoft.com/en-us/library/ms247257.aspx
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter