Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone please explain to me how to connect to a SQL database (with the .mdf file extension) in a C# windows application? Any answers are appreciated!

Note: if I am not too clear on what I am asking, or you want more information, just let me know.

Thx!
Posted

Connect to DB using LINQ

Steps

1. Create the schema for your database.

Download the SQLMETAL tool and make the schema file for your database

SQL METAL Command to generate shcema:
<location>\SqlMetal.exe /server:serverName /database:databaseName /functions /sprocs 
   /language:cs /code:<location to="" generate="" schema="">\schemaFile.cs /namespace:nameSpaceName /pluralize</location></location>


For more options of the SQL metal please goto http://msdn.microsoft.com/en-us/library/bb386987(v=VS.90).aspx[^]


2. Once the Schema is generated, attach it to your project.

3. While your application on load, connect to database using connection string.

MIDL
DatabaseName _db = new DatabaseName(@"Persist Security Info=False" +
                                     ";Integrated Security=" + _integratedSecurity + 
                                     ";User ID=" + _userName +
                                     ";Initial Catalog=" + _dbIdentification +
                                     ";Data Source=" + _serverName +
                                     ";Password=" + _password);


4. Then you can use the '_db' object to access the database.

MIDL
_db.Products.Single(a => a.Name == productName)
 
Share this answer
 
Go to the Data menu in Visual Studio->Add new Datasource->Database->DataSet->New connection.
Then you can locate your SQLServer and set password and so on.

When you are done you will have a DataSet in your Solution Explorer with the .xsd extension.

Double click on it and you have a visual representation of your Chosen DataBase.

This should get you started I think?
 
Share this answer
 
v2
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900