Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to access a SQL Server database using C# .Net

I am new to .NET and have heard about several different ways of querying a SQL Server databse such as ADO.NET and the entity framework.

Can anyone give me some advise about the best way to go for new applications?

Thanks for any help or suggestions.

like image 403
user483267 Avatar asked Oct 21 '10 16:10

user483267


People also ask

Can you connect to database using C?

Database Connectivity using C/C++ This IDE is specially designed for C and C++ and easy to use. SQLAPI++ is a C++ library (basically a set of header files) for accessing multiple SQL databases (Oracle, SQL Server, DB2, Sybase, Informix, InterBase, SQLBase, MySQL, PostgreSQL, SQLite, SQL Anywhere and ODBC).

Can you use SQL with C?

You can code SQL statements in a C or C++ program wherever you can use executable statements. Each SQL statement in a C or C++ program must begin with EXEC SQL and end with a semicolon (;). The EXEC and SQL keywords must appear on one line, but the remainder of the statement can appear on subsequent lines.

How can I access my SQL Server database from another computer?

To connect to the Database Engine from another computer On a second computer that contains the SQL Server client tools, log in with an account authorized to connect to SQL Server, and open Management Studio. In the Connect to Server dialog box, confirm Database Engine in the Server type box.

How do I access an existing SQL Server database?

To connect to a database instanceRight-click the SQL Server node in SQL Server Object Explorer and select Add SQL Server. In the Connect to Server dialog box, enter the Server name of the server instance you want to connect to, your credentials, and click Connect.


2 Answers

Here is an example using EF with code generation from the database (for a real app you probably want to generate your DB from the code, instead):

  1. Right click on your project >> Add >> New Item >> ADO.NET Entity Data Model.
  2. Pick a name for your entities i.e. MyEntities.edmx, click Next
  3. Select "Generate from database"
  4. Configure a 'New Connection' if there is not one already there. Next.
  5. Select the Tables, Views, and SPROCs you want included in the entities. Finish.

You will see a file MyEntities.edmx added to your project. You can open it in design view to see a diagram of your entities and relationships. Note that each entity should have a primary key - the easiest way to do this is to add an ID - auto increment field to each table, or a GUID column. Anyway now you can query your db like this:

// assuming a "Product" table, which has an entity pluralized to "Products"

MyEntities db = new MyEntities();

var cheapProducts = db.Products.Where(p => p.Price > 30); // var is IEnumerable<Product>
like image 50
Alex Avatar answered Oct 07 '22 17:10

Alex


Entity Framework is the easiest, and its built in.

All you have to do is important your model, and then just update simple classes, that are automatically mapped to your db.

Its just like updating normal objects, but at the end you call SaveChanges.

like image 43
Nix Avatar answered Oct 07 '22 17:10

Nix