Connecting to a Service-Based Datasource in C# - c#

This is a very noobish question:
I don't understand SQL. I'm trying to make a generic connection to an empty SQL database in C#. I don't want to make tables in advance, I just want to be able to open the connection.
Every "connectionstring" I try results in "InvalidOperationException" so I need some help understanding the proper syntax to connect to a SQL data file in Visual Studio.
What I have done is gone to "Add -> New Item -> Service-based Database"
This gives me a "Database1.mdf" in the solution explorer.
Now I want to open a connection through C# to this file at which point I can use SQL strings to manipulate the file.
I believe the next step is to use: SQLConnection SQLConn = new SQLConnection(" ... ");
But I don't know what to put for the "server" or the "database" in the connectionstring.
Can someone tell me the most basic way to connect to a "Service-based Datasource" in C#?
Will this "service-based database" be included in the final compile? E.g. if I compile a finished executable, will each installation come with its own local SQL database?

The service based database will not be embedded in the dll. See the last section of the below (in bold) to see how to obtain the connection string.
From an MSDN article (http://msdn.microsoft.com/en-us/library/vstudio/ms233763%28v=vs.110%29.aspx):
To create a project and a database file:
Create a Windows Forms project that's named SampleDatabaseWalkthrough.
See Creating Solutions and Projects.
On the menu bar, choose Project, Add New Item.
The Add New Item dialog box appears so that you can add items that are appropriate in a windows Form project.
In the list of templates, scroll down until Service-based Database appears, and then choose it.
Name the database SampleDatabase, and then choose the Add button.
The Data Source Configuration Wizard opens.
On the Choose a Database Model page, choose the Dataset icon, and then choose the Next button.
On the Choose Your Database Objects page, no database objects are available because the database is new.
Choose the Finish button to create the database and dataset and add them to the project.
The properties window for the database shows the connection string and the location of the primary .mdf file for the database. To display this window, choose the Server Explorer tab, expand the Data Connections node, open the shortcut menu for the database, and then choose Properties.

Related

How do you create a local database in visual studio

I am new to visual studio.
I made an app using excel vba that converts words into code 128 and display them.
I tried to recreate it, with visual studio, but I have no idea how to create a file that store data.
For example, the barcode code 128 table should be accessible through code to convert a simple text into a barcode view.
I know so far how to create service based database on sql, add column, and rows, but I need to know how to have (or create) a file with stored data to work with (like in excel, you create a sheet filled with data, and then, you can adress it using vba)
You've a really good tutorial's on the internet who helps you building a local database on visual studio.
I'll give you a initial steps to build one:
1st: After you create your project, Go to Project > Add New Item > And Choose Service-based Database
2nd: Open your database located on Solution Explorer with the name: nameyouchoose.mdf
3rd: It will open a DataSource on the Server Explorer where you can add your tables, records, etc...
Good tutorials:
docs
devu.com

How to type some values for database table in Visual Studio IDE

I downloaded source code of an ASP.NET MVC book and it looks this, I can see the table but I need to enter some data in it so it can show them to me
Where should I go to enter data?
While you should actually do this in SQL Server Management Studio, Visual Studio does offer you some support to deal with data. To do so, Open Server Explorer (Ctrl+Alt+S) in VS. Create a data connection, if it doesnt exist already to the DB in question by right clicking on Data Connection and click on Add Connection. Since you downloaded the source code for exercises, there is a good chance that they already created one for you. Once you have a connection to your desired database, you will be able to see database object under that including tables. To add data to a table, right click on that table and click on Show Table Data. You can then go to individual cells and enter data. Hitting enter should submit the data change to SQL Server.
Hope this helps..

C# How do I copy tables and stored procedures to an empty mdf file

So I have created a database in C#, and the mdf is in a specified location also attached to the same server (it is a backup database).
Now I want to know how to copy all the tables together with its data, definitions, and stored procedures from one table to the newly created one. Basically, they would be the same database with a different name and will be stored in a different location. I don't intend to use .bak by the way if someone suggests it.
Will I be able to do this in a query? or better yet, in C#? Or maybe I'm doing this wrong. Any suggestions will be welcome with open arms.
Summary
With Sql Server Management Studio (SSMS) you can right click on your database, select tasks, then generate scripts. This will bring up a wizard. In the Choose Objects step, you can select a radio button that will allow you to select your database tables individually. The next step Set Scripting Options will give you the ability to create a script in a single file, a file per object (table, sproc, etc). At this point you can click next through the rest of the wizard or review the steps.
If you use a script file, this can be used to recreate your database. Just modify the USE [YourDbNameHere].
Reference
https://msdn.microsoft.com/en-us/library/hh245282.aspx

Multiple locations for connection string in C# winforms visual studio?

When I go to add a project data source for my datagridview there are multiple connection strings to choose from on the choose your connection page. I have already deleted the multiple strings out of the app.config and tried deleting my dataconnection all together.
They are still there. Where am i missing?
thanks!
The connection strings you see are the ones that are defined as "Data Connections" in the Visual Studio Server Explorer window. You can delete entries from there that you no longer need. (View -> Server Explorer, or CTRL-ALT-S in VS2010.)
These are stored globally, in this file:
%appdata%\Microsoft\VisualStudio\10.0\ServerExplorer\DefaultView.SEView
You can just delete that if you want to get rid of them all, or try to edit it; for some silly reason it's formatted as a SOAP message. (The nodes you want are <DataViewNode> elements.)
Make sure you remove them from your projects first, though; from what I have seen, if you open a project that uses a data connection that you don't already have defined, it will be added into your Server Explorer list automatically.
If you deleted them from the places you've said and you've deleted them from under your project Properties, DataSources folder the only other place I can think of is on the actual Form itself.
You'll either see the datasources listed in the component tray beneath your form in the designer or you could open up your form's .Designer.cs file and do a find for the datasource names you are seeing in the DataGridView wizard.

Databases in winforms c#

I can't understand how to manage database with Entity Framework.
I'll try to describe my problem step-by-step
create winforms application
add database (.mdf) to solution
when appears window "choosing database model", select dataset
then finish, as database is empty
go to server explorer, choose created database, create some tables
add to solution EDM ADO.NET
The problem is that new data doesn't appear in my database in server explorer. This means that while my Winforms app is working, I can modify my database (change existing values, add new, ..). but if I close my app, there is no update in my database in server explorer. I DON'T forget to use function SaveChanges()
P.S. while my app is working, values are updating. Think, that means that data updates in dataset, but not in database. if i am right, give a clue how to update database from dataset.
pps. visual studio 2010 ultimate. sorry for english
dbentity db=new dbentity();
db.items.addobject(new item() {value=something});
db.savechanges();
Don't add the database to your solution. You can use the Server Explorer to add a connection to your database via your existing SQL Server, or you can select your database in the wizard that appears when adding a new EDMX file to your solution.
Did you try either of these? Where did you get stuck?

Categories

Resources