How can I connect to a database in Visual Studio with Entity Framework?
I have a database:
But in visual studio I don't know what I must enter.
I have a window:
Can You say what I must write there?
I tried many times and I still have error.
A dot (aka period), like this: .
If you have SQL Server Express installed, it should be .\sqlexpress
Use the Server explorer. See http://msdn.microsoft.com/en-us/library/x603htbk.aspx
You can choose the instance name when you install SQLServer Enterprise or SQLServer Express. I think it is the same for other database.
In the other hand, you can add a new database just in Visual studio project. Then you will get a MDF file in the resources list, which can be added here.
I don't know if I understood you, but those fields are about the server where the database is
Server name is the name o the server that appears on SQL Managment Studio (probably the name of the machine the server is running on)
When you open SQL Server Management studio you can a popup as shown below:
The server name that you use there should be the one you can use in your case. If the SQL Server database is installed on your machine it would be (local) or your machines name which can be found by right clicking on "My Computer" and then Properties and you can see "Computer Name" in the window that opens.
Related
I am on a Windows 10 machine, which has Visual Studio 2015. I am now trying to do projects with a database on the same machine. I seem to have SQL server ( I am really new to Databases ) Here is why I think I have a Database, which I can use:
C:\Users\zohal>wmic product get name,version | findstr "SQL"
Microsoft SQL Server Compact 4.0 SP1 x64 ENU 4.0.8876.1
When I try to connect to a database from Visual Studio I do the following:
click on Project > Add New Data Source new Window Comes up
select Database new Window Comes up
select Dataset the only option available, new Window Comes up
select NewConnection new Window Comes up There are six options
select Micorsoft SQL Server new Window Comes up but the pulldown is blank. I looked in the services section of Computer Management application, and do not see a SQL service running. I want to know how do I connect to this SQL server and does it work for what I want to do, which is to run .Net C# code with SQL statements?
Update...........
Guys, thank you for the content and advice. I have tried both SQL compact and SQL Express, and ran into the following problems. I wonder if you can help, or perhaps I'll create a new post. I have tried everything I could think of. 1) I tried to use the SQL compact. Got as far as "Creating a SQL server Compact Database". When I right click on the "App_Data", I just don't see an option of "SQL Server Compact 4.0 Local Database" as it stated in the procedures. Here is an of what I see. I did a search online for this type of issue, and the solution was to install a tools package. I did that. In fact I ran the Visual Studio install file, and chose the modify option and added all the tools, and support packages I could think of. I rebooted the machine also; no change.
I also ended up installing SQLExpress, and followed this link to set it up, but when I click on a new connection option I do not see the SQL Express option in the server name pull down as it states in the procedure. I basically get as far as creating the project and three steps into the procedure. I can put the projects on Git if that helps. My immediate goal is to create a table and query it. Here the code in my project:
var query = from c in db.Customers
where c.City == "Nantes"
select new { c.City, c.CompanyName };
Right now, I get an error under "c" and "db" as I have not created a connection. While I don't know how to do that yet, I do know that is not a huge task. But if I can not connect to a Database, this does not help me much. I did find the right assemblies and added them to the beginning of my project. I am talking about
using System.Linq.Expressions;
using System.Data.SqlServerCe;
Just tell me what to do. Should post again or can help me?
Update: Thank you. The MVC .Net Core MVC procedures worked. Thank you so much.
SQL Server Compact is not a full fledged database service. It is a way to store data in a file and run sql like queries against it.
Here is a short walk through of how to connect to a sql compact database: https://msdn.microsoft.com/en-us/library/gg606540(v=vs.100).aspx
If you want to use an actual sql server you can download and install SQL Server Express for free. Here is a walk through for that: https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
Both of the linked examples use Entity Framework to connect which is an ORM. There are many ways to connect to a database in C#. But since you mentioned running sql statements directly you may want to look into Dapper.Net which is a mirco ORM that let's you run SQL statements and map the results back to objects.
If you are not developing mobile (win mobile) apps, you need to download SQL server express edition which is suitable for any database application built for .NET and targeting the desktop/web apps.
To install the latest SQL express 2016, please visit this link or do some search on the web:
https://www.microsoft.com/en-us/sql-server/sql-server-editions-express
I would first verify in "Control Panel > Programs > Programs and Features" to check whether SQL Server is installed on the machine. You should see program with name "Microsoft SQL Server 20xx Setup". If you don't see any such names, then I suggest you to download and install a fresh copy of SQL Server.
Microsoft® SQL Server® 2014 Express
If you do see it in control panel, then open RUN (Windows Key + R) and type SSMS and press enter. This should open SQL Server Management Studio. (Sometimes SQL Server might be installed but Management Studio might not be. You can download Management Studio as well from the link provided above)
After opening management studio, enter the server name and click on "Connect". If you don't know server name enter . (dot) and click "Connect". Now you should be connected to SQL Server.
You should be able to connect C# code with SQL Server using a connection string.
Different formats of connection strings
How to store connection strings in web.config or app.config
Execute a command in c# by connecting to SQL Server
I have SQL Server 2014 installed and want to use localdb\v12.0 in Visual Studio. I found this post and understand how to connect to localdb using SQL Server Object Explorer successfully. Now I have created a LightSwitch project in Visual Studio 2013 and when building it throws exception:
An error occured while establishing a connection to SQL Server
instance '(LocalDB)\v11.0'.
Seems it is looking for SQL Server 2012 to create the development database in Bin\Data and I don't have SQL Server 2012 so there is no localdb\v11.0 on the system. So
Why does VS 2013 try to use localdb\v11.0?
How can I force Visual Studio to use localdb\v12.0 when building the project?
I tried changing the _IntrinsicData connection string in web.config but when building again it throws the same exception trying to use localdb\v11.0 which belongs to SQL Server 2012!
Ok after spending lots of time I found what was the problem! Here is the cause and solution:
LightSwitch uses SQL Express LocalDB to store information such as User and Roles when building the project (this is called development database). There is no problem with SQL Server 2012 but if SQL Server 2014 is installed something force the LightSwitch to connect to (localdb)\v11.0 . LightSwitch makes three projects in the solution. If the Client and the Server projects build successfully so the problem is with the third project. Go through these steps:
Right-Click on the third project in the solution
Select Unload Project
Again Right-Click on it and select Edit <yourProjectName>.lsxtproj
Search the SqlExpressInstancename
You will find its value is (localDB)\v11.0 !
Change it to the right SQL Server LocalDB instance name; for more information check the notes below
Now the solution successfully build
--
Note 1: To see which version of SQL Server LocalDb is installed, open cmd and type sqllocaldb.exe v
Note 2: To which instance of LocalDb has been created on the system, open cmd and type sqllocaldb.exe info
Note 3: The instance which is going to be used by LightSwitch must be started: open cmd and type sqllocaldb.exe start
UPDATE: Looking for a better solution I found the correct way to fix this problem. It was a pain for me to follow those 6 steps every time I create a new LightSwitch project. So if you want to fix it once you can do it this way:
In Visual Studio go to Tools->Options
In the Database Tools select the Data Connections
Set your desired SQL Server Instance Name
So no need to set it every time you create a LS project
I'm using Visual Studio 2010 and SQL Server 2012. When I try to connect to SQL Server through Visual Studio, I don't get my server name on Add connection drop down menu.
Go to SQL Server Management Studio to look for the correct Server Name and copy the value. Make all the configurations by yourself at the end just click Test connection button. If the connection is test successfully proceed with your work.
If the database is in your local machine, Just put a . (Dot) in Server Name and select the database.
run services.msc
find " SQL server browser " , it might be disabled , start that service and set it to automatic.
Insert machine_name/SqlServerName
For people using sql server 2014+ and using the local db on your pc. simply set the server name to be (localdb)\ name of your database. It worked for me.
In the past I used to install Ms SQL 2005 in my system separately but in recent years I never care to use them anymore as I totally depend on XML tables rather than those of Ms SQL. So I never had the need for installing them.
Today, I thought of trying out using Ms SQL 2005 again in my ASP.NET project as many people suggest that its far more better than XML tables.
When installing Ms Visual Studio 2008 Professional Edition, it seems some controls of Ms SQL 2005 is included. What I could see from the "All Programs" (from start menu) is the Microsoft SQL Server 2005 Folder. Inside, I have Configuration Folder only.
Now on my Visual Studio Project, My "Server Explorer" looks like the image below.
I cannot create a database by right-clicking the Data Connection. I always get an error same as the one I get when trying to add "New connection" by clicking on "Add Connection".
Let me show you how I did using images:
Step 1: Choosing the Data Source
Q 1: Are the data source shown here are installed on my system or just the names shown by Default even if its not installed?
Step 2: Connecting to Microsoft SQL Server
The Error says: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provide: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
Q 2: What does this mean?
My suspicion is that, this error occurred because I don't have SQL Server and my problem will be solved once I install SQL Server.
My curiosity is that, if SQL Server Database can be used even without installing SQL Server separately then why not just use it from here (Visual Studio) as some features seems to be already installed while installing Visual Studio.
Thank you for your time for caring to help me
Q1: That list does not show existing datasources, it shows available ADO.Net providers (you can think of them as database drivers).
Q2: Why did you enter "Me" as server name; is this the name of your computer? Try ".", "localhost" or "localhost\SQLEXPRESS" instead.
If you want to connect to SQL Server on your local machine, it needs to be installed on your local machine. The features installed as part of visual studio are front end management tools (depending on the version of Visual Studio installed).
Also, check out the version of SQL Server you're using for supportability purposes.
http://www.microsoft.com/en-us/sqlserver/support/support-updates.aspx
Those data sources shown in the image in Step 1 are not specific sources, they are simply types of sources. The Add Connection dialog you're looking at allows you to create a connection to, in the case that you selected Microsoft SQL Server, any MS SQL server you have access to connect to. Thus, your exact question: yes, what VS installs are simply drivers to connect to various databases, not the actual databases themselves.
Since you aren't running a MSSQL Server on your local machine, it tries to connect to your local machine to find a MSSQL server, can't, and thus gives that message. If you want to connect to a MSSQL server without installing one on your local machine, you could always find one someone else had installed on their machine and see if you could use it...
Can't access Northwind database from Visual Studio
I am working on my own computer, and created tne Northwind database from a script file. i should be the admin on the computer
and can access the table from SQL Server
From windows explorer, I right click the table and I can see two accounts that have full control (full control, modify, read etc are all checked). those accounts are
SQLServerMSSQLUser$Gary-PC$Gary (Gary-PC\SQLServer..
Administrators (Gary-PC\Administrators).
However, when i select the table to try to connect to it in VS, i get the message, you do not have permissions to view this file.
is there something else I have to do in SQL server to set permissions? or might it have something to do with the user context not being one of these two users in visual studio?
I guess you mean the Server Explorer/Database Explorer in Visual Studio, When you add a connection in Server Explorer there is a field for user name and password, however the default is using Windows Authentication, if you don't use that you probably have to set your user id correctly there.
Server Explorer/Database Explorer can be access from the View menu, for the existing connection you need choose the modify menu.
Please note if you connect from your code, the connection string is set in your code .NET, C# or configuration not from the server explorer, you need to set them separatly.
I don't have the exact the same version of C# Express installed, the settings might vary a little on different versions.
The connection string in Database Explorer/Server Explorer is depending on the the data source you use. You might want to look at this to connect to SQL Server using Microsoft SQL Server(SqlClient)
Then you can see the SQL Server Authentication method from Visual Studio Express.
You may also need to change the SQL Server Authentication, That is in SQL Server Management Studio Object Explorer, right-click the server, and then click Properties. On the Security page, You can see Server authentication, MSDN has that well documented.