Connection string for the Northwind.sdf - c#

I am making a small Windows application using Visual Studio 2010 and SQL server compact edition 3.5. I want to access Northwind.sdf database in my application but I am unable to make the connection string.
I have copied the database in my application as well as in the debug folder as I am trying to run application in debug mode.

Well you have got to give us a bit more than that. First of all are you trying to connect to the database using SqlConnetion or SqlCeConnection? Secondly, are you sure that path or you database is correct? You may also referre to the links below for more info.
Sql Compact 2008 Connection string Problem
Sql Compact Connection String
If you want any further help try to post the code you have done so far.

Related

C# | Can't insert Data into LocalDB but it doesn't show any error [duplicate]

Apparently, using AttachDbFilename and user instance in your connection string is a bad way to connect to a DB. I'm using SQL server express on my local machine and it all seems to work fine. But what's the proper way to connect to SQL server then?
Thanks for your explanation.
Using User Instance means that SQL Server is creating a special copy of that database file for use by your program. If you have two different programs using that same connection string, they get two entirely different copies of the database. This leads to a lot of confusion, as people will test updating data with their program, then connect to a different copy of their database in Management Studio, and complain that their update isn't working. This sends them through a flawed series of wild goose chase steps trying to troubleshoot the wrong problem.
This article goes into more depth about how to use this feature, but heed the very first note: the User Instance feature has been deprecated. In SQL Server 2012, the preferred alternatives are (in this order, IMHO):
Create or attach your database to a real instance of SQL Server. Your connection string will then just need to specify the instance name, the database name, and credentials. There will be no mixup as Management Studio, Visual Studio and your program(s) will all be connecting to a single copy of the database.
Use a container for local development. Here's a great starter video by Anna Hoffman and Anthony Nocentino, and I have some other resources here, here, and here. If you're on an M1 Mac, you won't be able to use a full-blown SQL Server instance, but you can use Azure SQL Edge if you can get by with most SQL Server functionality (the omissions are enumerated here).
Use SqlLocalDb for local development. I believe I pointed you to this article yesterday: "Getting Started with SQL Server 2012 Express LocalDB."
Use SQL Server Compact. I like this option the least because the functionality and syntax is not the same - so it's not necessarily going to provide you with all the functionality you're ultimately going to want to deploy. Compact Edition is also deprecated, so there's that.
Of course if you are using a version < SQL Server 2012, SqlLocalDb is not an option - so you should be creating a real database and using that consistently. I only mention the Compact option for completeness - I think that can be almost as bad an idea as using AttachDbFileName.
EDIT: I've blogged about this here:
Bad Habits : Using AttachDBFileName
In case someone had the problem.
When attaching the database with a connection string containing AttachDBFile
with SQLEXPRESS, I noticed this connection was exclusive to the ASP.NET application that was using the database. The connection did block the access to all other processes on the file level when made with System.Data.SqlClient as provider.
In order to assure the connection to be shareable with other processes
instead use DataBase to specify the database name in your connection string
Example or connection string :
Data Source=.\SQLEXPRESS;DataBase=PlaCliGen;User ID=XXX;password=ZZZ; Connect Timeout=30
,where PlaCliGen is the name (or logical name) by which SQLEXPRESS server knows the database.
By connecting to the data base with AttachDBFile giving the path to the .mdf file
(namely : replacing DataBase = PlacliGen by AttachDBFile = c:\vs\placligen\app_data\placligen.mdf) the File was connected exclusively and no other process could connect to the database.

I developed an application in C#, is it possible to run this application on another PC which has no SQL Server and Visual Studio

I developed a desktop application in C# and save data into SQL Server. Is it possible to run this application on another PC which has no SQL Server and Visual Studio if it is possible then how to do that
Yes, it is possible. You will need to set the connection string in your application with the connection information needed to connect to a remote database. You can always check out http://www.connectionstrings.com/sql-server/
to see some examples of SQL connection strings.

Deploying WPF Application with database

I have made a Wpf application and i am using linq to SQL classes also i have made my database in SQL server 2012 so is there any way that i can deploy my application in such a way that it will run on other PC's?
Check your app.config file. Make sure your connection string contains:
valid DataBase address
Valid credential for the db
As your update:
whenever i make setup file and run on another pc it give me exception
because sql server isnt installed on that pc
Either you need to deploy a database in internet, accessable by your app. Or you have to use an embedded sql db (e.g. add a db.mdf to your solution). I think this suit you better.

Easiest way to set up SQL Database for local testing of C# project in Visual Studio Express 2013

Okay, so this is going to sound stupid or trivial, but please, humor me. I am writing a personal project in C# using Visual Studio 2013 Express and some of the code is going to hit a SQL database. Before I hit other databases, I want to test it locally and make sure that it performs as expected. However, I can't seem to find any tutorial that either is complete or works in order to get a SQL database up from nothing.
First, I tried installing SqlLocalDb 2014. Even after the tools I can't figure out where there is a UI that I can type in scripts to create a database.
I tried the directions here: http://www.wikihow.com/Create-a-SQL-Server-Database
But at step 3, the option to create a database is greyed out, and searching that issue got me nowhere.
I've tried adding a new SQL Server Database Project to my solution. This looked like it would work exactly, but when I designed the table I couldn't figure out how to actually deploy/publish it so I could connect to it. I get a SQL file, and I can't figure out what to do with it from there. I found instructions on how to publish it, but it seemed the only directions were to publish to a remote server (and I want to use a local one).
I tried the instructions here (yes, I know they're for 2012): http://msdn.microsoft.com/library/ms233763(VS.110).aspx but I can't find the Server Explorer / Database Explorer. I found the Server Explorer (where my table doesn't appear) and the SQl Server Object Explorer, where my table does appear but I can't figure out how to populate it with dummy data or how to connect to it.
This should not be this hard and I feel like I'm missing something obvious. Thank you in advance for any help.
Hitting F5 deploys your SQL Server Database to a localdb instance that you can connect your application to. You can see the connection string for this database by right-clicking on the project -> Properties -> Debug -> Target Connection String.
You can use this connection string in your application code to connect to the local database and test it - place it in a configuration file that can be replaced with the real database connection string in your production code. Alternatively use a database first Entity Framework Model to connect your application to your database, and you can just change the App.Config file to specify whether to target your test or production database. That's a really nice way to handle this since it will produce all the code you need to work with the database.
You could use xampp. From there you can create local databases and then you could talk to the created database using "localhost" instead of the productive server.
For the record there is some information on Accessing Data in Visual Studio at:
https://msdn.microsoft.com/en-us/library/wzabh8c4.aspx
And specifically to answer your question there is a "Walkthrough: Creating a Local Database File in Visual Studio" at:
https://msdn.microsoft.com/en-us/library/ms233763.aspx

How to run desktop application with its Sql Server database on a machine that have not SQL server installed

I have already finished a desktop application using Visual Studio 2010 and SQL Server 2008.
it's working perfectly on my machine (i have a SQL Server 2008 and 2005 ).
the database is local and unchangeable,
i want this application to be run successfully on the machine that doesn't have SQL Server installed.
my Connection string is:
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\KBank.mdf;Integrated Security=True;User Instance=True;Connection Timeout=300
I want to know the best and simplest way to make it. thanks
The simplest way would be to create a setup for your application that states SQL Server 2008 as a prerequisite. That's it.
For further help you would have to supply more information on how you use the database in your application and if you have a way to host a public sql server instance to which your application will connect.
If you're using the AttachDbFilename=|DataDirectory|\KBank.mdf;User Instance=True approach, then you have no choice but to install SQL Server Express (and no other edition!) locally on that / every machine that is supposed to use your application.
This approach is severly flawed in my opinion, and it limits your flexibility.
What I'd suggest is to use the real server approach: put your database onto a server (both on your development environment, and in production) and then you have the flexibility of having either a SQL Server instance on every user's machine (if that makes sense), or you can have a centralized server which the clients only connect to (no local database server installed).
If the database is "local and unchangeable" how about the compact edition? That way your clients dont need to install a full sql server instance
I agree with Nick. if you dont want to install SQL Server on the machine then SQL server compact edition would be one of the option. Syntax is pretty similar to SQL Server but it comes with limitations. like you can not have a stored procedures in compact edition and few more.
You might want to visit SQL CE Tutorial as a development resource.

Categories

Resources