I've been at this for many, many hours and I've run into nothing but roadblocks. All I want to do is to start a project in Visual C# 2010 that connects to a SQL database. I've always worked with Access databases and I want to learn how to with SQL Server. Who would have thought it would be this much trouble.
In my programs files (on my home computer) I have Microsoft SQL Server and Microsoft Server Compact Edition. Under Sql server there is a folder 80, 90, 100, 110, MSSQL10.SQLEXPRESS. In the Server compact Edition folder there is v3.5 & v4.0. I have MS SQL Server Managment Studio (installed & reinstalled 2 times to finally work) and when I open it it says SQL Server 2008 R2.
I Downloaded the Adventuresworks database and finally got it to connect in management studio, but when i try to connect in Visual studio (add new data source) I get errors, wrong version, etc. So I now try connecting using connection string, but I don't know which database to connect to.. nor how to write it. There are multiple databases in C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA from my adventure works install.
I also read that with Windows Vista there is a permissions issue (only Administrator can install and access databases).
How do I resolve this error?
It's true that getting started with SQL Server can be quite maddening. That said, your connection strings are not really based on folder locations on your machine. Instead, when connecting to SQL Server on your own computer, the server name is usually "localhost" then possibly followed by an instance name--for example localhost\sqlexpress. You can also use your computer name in place of "localhost." A full connection string would look like:
Data Source=localhost;Database=AdventureWorks;Integrated Security=SSPI
In code, you test this by doing something like
using (SqlConnection cn = new SqlConnection("Data Source=localhost;Database=AdventureWorks;Integrated Security=SSPI"))
{
cn.Open();
cn.Close();
}
This will essentially test a connection.
The easiest way to find servers you can connect to is to use the Browse for Servers dialog when connecting in SSMS (but this depends on the SQL Browse service running).
here you can see I have two instances that are named after my computer. I can use either the name "adam-laptop" or "localhost" to form
Data Source=adam-laptop;Database=AdventureWorks;Integrated Security=SSPI
or
Data Source=localhost;Database=AdventureWorks;Integrated Security=SSPI
or
Data Source=localhost\sql2012;Database=AdventureWorks;Integrated Security=SSPI
or
Data Source=adam-laptop\sql2012;Database=AdventureWorks;Integrated Security=SSPI
Related
I'm writing a program with using C#. And I'm using SQL Server Management Studio to create local databases to keep my data. I would like to run this program on other computers. I do not want to install SQL Server Management Studio on every computer. Can I move my local database to other computers without installing SQL Server Management Studio?
In example I don't mind installing localDB. Actually I think that I can do it only using localDB but I could not find proper info. My program can reach the database with connections.
In other words, how can I move my database to another computer which ONLY has localDB?
Thanks...
From Microsoft:
LocalDB is a feature of SQL Server Express targeted to developers. It is available on SQL Server 2016 Express with Advanced Services.
LocalDB installation copies a minimal set of files necessary to start the SQL Server Database Engine. Once LocalDB is installed, you can initiate a connection using a special connection string. When connecting, the necessary SQL Server infrastructure is automatically created and started, enabling the application to use the database without complex configuration tasks. Developer Tools can provide developers with a SQL Server Database Engine that lets them write and test Transact-SQL code without having to manage a full server instance of SQL Server.
You can work with LocalDb without having to install Management studio.
I have a question that I have developed a C# WPF application which requires a local SQL Server database. I am having trouble publishing the application to a different computer where there is no SQL Server installed.
Is it necessary for me to have a SQL Server installed on all the PCs where I am going to install my application? Or am I missing something?
I am making the setup file form Advance Installer and copying the .mdf database to the location mentioned in the connection string
string connectionString = #"Data Source =(LocalDB)\MSSQLLocalDB; AttachDbFilename = " + Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + #"\abc\Databases\abc.mdf; Integrated Security = True; Connect Timeout = 30";
The error I am getting:
If having a SQL Server a necessity, which one should I install on a low end pc?
any help will be appreciated. Thank you
If you want to use a .mdf file, then you must have SQL Server installed
either locally on each PC
or centrally on a server which all the PC's can connect to
If you install a .mdf file locally on a PC and you want to use the AttachDbFileName= parameter in your connection string, then you must install SQL Server Express on each PC that has this application. Other editions of SQL Server do NOT support the AttachDbFileName= approach.
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
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...
Is there any way to do this?
Update: Sorry, I should have included more information. I am trying to create and connect to a SQL CE database using System.Data.SqlServerCe in C# running on a PC (i.e. not running on a Windows Mobile device).
This code:
string connstr = "Data Source=\"" +
filename + "\";Persist Security Info=False;";
System.Data.SqlServerCe.SqlCeEngine engine = new SqlCeEngine(connstr);
engine.CreateDatabase();
... works fine on any PC that has SQL Server 2005 installed, but fails on any PC that doesn't have it installed. I'm trying to find out if there's any way to get this to work without installed SQL Server 2005 on the machine.
You can do it with Visual Studio - when you add a connection, change the data source from Microsoft SQL Server to Microsoft SQL Server Compact 3.5.
Also, if you mean the actual server - as opposed to the management tools - then SQL Server 2008 Management Studio [Express] can open SQL CE databases directly.
Edit: To create the database in Visual Studio, choose "Local Database" when you go to add a new item. That's a SQLCE database. And in SSMS[E], when you choose the SQL Server Compact option, you can choose "New Database" as an option in the Database File drop-down.
Edit2: In order to have code written against SQL CE run successfully on a vanilla target machine, you will need to install something on it, although not SQL Server 2005. SQL CE is a separate product (download page). It should also appear as a redistributable module in Visual Studio if/when you create an MSI installer for your product.
I assume that what you mean is can you create one with a tool, rather than with code. Studio can create them just by going to the Server Explorer and adding a new connection (you'll get the option to create one).
If you're looking for something a little nicer or something that doesn't require Studio, then Primeworks' Data Port Console is a really nice tool.
EDIT
If you need to create it through code then yes, you can still do this without Server installed. Make sure that you have the SQL CE Redistributable binaries (for the proper 32/64bit) deployed to the target and in a place the app can find them.
See locally at
C:\Program Files (x86)\Microsoft SQL
Server Compact Edition\v3.5
or online.
Just to add to what Aaronaught was saying, to connect to a SQL CE database programmaticaly, you don't either need SQL Server installed. CE runs in proc, and as long as the SqlCE dll's are installed (their part of the framework nowadays) then you should be able to connect to it without any issues.