Using a local SQL database in Visual C# - c#

I am working on a Visual C# program and have added a local database through the Add->New Item... dialog.
The guides I have read about using this type of database then instructed to make a data source using the database and then in the data sources toolbar just drag and drop a table to create the tools for entering data into the database and reading data from it.
This isn't how I want to access the database, though. I want to have completely different controls for the user and then just directly interface with the database in my code. I haven't been able to find anything that explains how to do this, though. Can anybody offer any advice? Do I need to use LINQ for this or would it be just as good for me to use SQL?

You may want to check out at http://weblogs.asp.net/scottgu/archive/2007/06/29/linq-to-sql-part-3-querying-our-database.aspx

I suggest that you must have to learn the Data Provider classes (ADO.NET). For direct access to Microsoft SQL Server use data provider classes for MsSql server (System.Data.SqlClient). In order to use LINQ, you must have know-how of Data Provider classes, Generics, Delegates and Anonymous methods.

Related

Where to store the data from database temporarily?

I am using SQLite local database in my C# application (WPF).
Should not you open the database connection once and load all the data in the beginning (instead of opening the connection everytime you need a bit of data from the database)?
My question is where should that data be stored in the application.
Should I create a static class which contains all the information in multi-dimensional arrays or multi-dimensional lists. Or is there a better way to store the information?
those are the most common tools for this task:
(by my order of recommendation)
http://en.wikipedia.org/wiki/NHibernate
http://en.wikipedia.org/wiki/ADO.NET_Entity_Framework
http://en.wikipedia.org/wiki/ADO.NET
Your question cannot be properly answered without knowing your business logic.
However, I can tell you that no most applications do NOT load the entire database into objects but rather queries the data when the data is needed.
For the other part of your question, why dont you create a ADO.Net data model? There are templates available if you are using vs 2010, just type model in the search box after opening new project.
Take a look at datasets and the dataset designer tool of visual studio.
If You use this approach, You can benefit from nice features, like serializing tghe data to xml and so on.,
Yes your initial solution is ok,In order to prevent traffic between your App and DB you should save your data in memory,such as propery that you have mentioned before but you could benefit of Caching advantages.
if your are using .Net framework 4.0 ,you can utilize System.Runtime.Caching ,otherwise, you should be familiar with Enterprise application block 5.0 or lower.
Related Post
System.Runtime.Caching-MSDN

Insert/Select data in foxpro database using c# .net

I want to use FoxPro database in backend and c# .net in front end but i don't know how to connect with foxpro database in .net
For connectivity what code i use, please suggest...
You may want to look at .Net Interop.
Also take a look at West-Wind web connect. They have a framework that allows you to use the Visual studio IDE to create webforms but also use your VFP business logic and data source. This works well.
West wind also has a wwDotNetBridge that allows you to access .Net components from VFP.
Check out their website below.
http://west-wind.com/WestwindClientTools.aspx
http://www.west-wind.com/presentations/VFPDOTNETiNTEROP/VFPDOTNETINTEROP.HTM
I don't think you necessarily need to go the way of "interop", but get a basic understanding of connecting and querying data.
First, get Microsoft's OleDB provider located here
Here is a sample doing a connection and running a simple query to get data but this one sends the results to another VFP table instead of bringing back to C# for process/usage.
This example shows Inserting records and uses parameters to help prevent any attempts at SQL-injection attacks
And another using SQL-Update
Once you get the basics down, its not that difficult. I've actually made a simple "wrapper" class to centralize ensuring a valid connection, execute a given query and closing connection when done. Then, I've just added methods to it for each thing I wanted to do (or could be subclassed too). Anyhow, when I need to do a certain action, I would just call that function and pass in whatever parameter(s) was(were) needed.

How to use a local database in c#?

I've made a local database for a C# project:
I know basic SQL commands, but haven't worked with databases in C#.
What I'd like to know specifically is:
How to read from the database (query)
How to add and update rows
The database only consists of 3 tables, so I don't think anything fancy is needed.
First, you should learn a bit about various technologies and APIs for connecting with a database.
The more traditional method is ADO.NET, which allows you to define connections and execute SQL queries or stored procedures very easily. I recommend digging up a basic tutorial on ADO.NET using Google, which may differ depending on what type of project you're creating (web app, console, WinForms, etc).
Now days, ORMs are becoming increasingly popular. They allow you to define your object model in code (such as every database table would be a class, and columns would be properties on that class) and bind to an existing database. To add a new row to a table, you'd just create an instance of a class and call a "Save" method when you're done.
The .NET framework has LINQ to SQL and the Entity Framework for this sort of pattern, both of which have plenty of tutorials online. An open source project I really like is Castle Active Record, which is built on top of NHibernate. It makes defining ORMs quite easy.
If you have specific questions about any of the above, don't hesitate to post a new question with more specific inquiries. Good luck!
Update:
I thought I'd also put in one last reference as it seems you might be interested in working with local database stores rather than building a client/server app. SQLite allows you to interact with local stores on the file system through SQL code. There's also a .NET binding maintained by the SQLite guys (which would in theory allow you to work with the other platforms I mentioned): http://system.data.sqlite.org/index.html/doc/trunk/www/index.wiki
You can use SQLCE.
This blog will give you a good start.
http://weblogs.asp.net/scottgu/archive/2011/01/11/vs-2010-sp1-and-sql-ce.aspx
Here is a small tutorial that should be helpful to you.
You can make use of the SqlDataReader to read data
and the SqlCommand to Insert Update Delete rows from your tables.
http://www.dotnetperls.com/sqlclient
You could use it by adding following to your Startup.cs
services.AddDbContext<DemoDbContext>(options => options.UseSqlite("Filename=data.db"));

How to dynamically map a datatable to a SQL table

Hopefully I can explain what I am trying to do.
I am writing a system to take data stored in Sharepoint lists and push them into SQL tables. This is being done so the data from the lists can be joined with other data and reported on.
I need the system to be quite flexible so I want to store the mapping between the lists and SQL and then create any of the SQL that is missing.
So I would first have to check if the SQL table I want exists and if not create it. Then check all the columns I expect and create an missing ones then populate the table with the list data.
Getting the list data is no problem to me and it isn't a problem for me to store by configuration information.
My issue is I'm not sure what .NET features to use when talking to the database. I was looking into the entity framework and LINQ but these seem to need fixed tables which I don't have.
I am also looking at using the enterprise libraries (4.1) as I use these for event logging.
Ideally what I want to be able to do is build a datatable and then "compare" it to a SQL table and have the system update it as required.
Does any thing like this exist and what approach would you use.
These may help get you started :-
Codeplex - SPListSync
Synchronize information with other
lists or SQL Server table based on a
linked column. This can be helpfull
when having list with companies and
another list with contacts. The
company-information (e.g. Business
phone and address) can be copied to
the linked contacts.
Exporting Data from SharePoint 2007 Lists to SQL Server via SSIS
SO - Easiest way to extract SharePoint list data to a separate SQL Server table?
Commercial
Simego - Data Synchronisation Studio
AxioWorks SQList
You need to bit Study SQL Server Management Objects, through which you can directly interact with SQL Server very easily. Through this you can create New Table, Stored Procedure etc and also check pre-existance of any object.
Talking to Database like this was never so easy...

Easiest way to use SQL in a C# application?

I'm making an application in C# (with Express Edition) for which I would like to add some SQL functionality so that the user can query a database. I don't care how or where I store the database. I can store it in a DataTable or a bi-dimensional array or any kind of file. But I want the user to be able to SQL-query it. Apparently this should be quite simple since the .net seems to be full of database libraries and stuff. I was thinking about downloading MySQL and see if I can connect it to my application. I guess if I want to distribute my application then the user would need to download MySQL as well, which is not a big deal but would be great if I can avoid it. Anyway, for now I would like to start working on my program ASAP, so whatever is the easiest way to do what I want, even if it's not distributable, (but if it is then that's even better), will be good. Thanks in advance.
There are embeddable databases. SQL Server Compact Edition and SQLite are common ones. You can execute queries against these just as you can MySQL or SQL Server.
SQLite (.NET)
SQL Server Compact
You can use most popular databases with .NET. SQL Server, Oracle, MySQL, etc. But you're gonna need drivers of each. So, I'd suggest using SQL Server Express Edition to you to get started.
Then you can easily use SqlConnection and SqlCommand classes to connect and execute queries.
You could use a dbml file in your project and link it to your sql database and then run a sql statement using Linq2SQL documented here
I would look at using and embedded database that you can distribute with your application. SQLite is a very good option. You can then use a free ADO.Net library like System.Data.SQLite for data access. It also provides design time support for Visual Studio.
You can use LINQ to Objects or LINQ to Datasets to run LINQ queries with no database whatsoever. You can't use a bi-dimensional array, but you can use a List<> of objects with properties as a LINQ context.
From your question it sounds like your application, like most applications, may need to store the data for later use: that's where a database will come in handy. .NET Datasets have built in support for persistence to an XML file if your data storage requirements are simple enough to use that. .NET also supports persistence for objects, but you may find that using a database is the simplest solution, especially if you require multi-user access and editing.

Categories

Resources