I'm trying to learn SQL and how to use it in my C# projects, but I'm struggling to understand how to connect it. So far I've seen most people simply do
using (var connection = new SqlConnection(Properties.Settings.Default.TestConnectionString))
{
connection.Open();
/..
}
But in Visual Studio 2017, there is a tab for DataSource. Once I set my database connection there, I get a generated file DbNameTest, which I don't see to serve any purpose at all, there is no loaded data in it because I'm creating new instance of it every time I try to access it, I can save it, but I will still need to populate it the first time.
What's the point of using the DataSource tab and what's the purpose of the the DataSet?
This is a profound subject that has many answers and methods. I'll try to explain it simply.
The datasource tab consists of ready-made structures presented to developers. With the help of the components in this tab, you can do many operations with dragging and dropping the code.
However, many developers prefer to build a more extensible and manageable structure by writing code instead of drag and drop with ready components.
DataSet; It is a copy of the database in ADO.NET. A dataset can contain one or more DataTables. DataTables are database tables.
Related
Objective
Build a PDF file which represents single C# class object with multiple fields and properties, that is created in runtime. It constains plain text, variables, tables, images.
What I have to use?
As a requirement, I have to render PDF report with Microsoft.Reporting.WebForms.LocalReport and it will probably contain SubReports.
What I did?
When working with reports until now, following simple pattern was enough:
ReportParameter param = new ReportParameter();
param.Name = "Param name";
but in that case I'd have to make over 80 parameters and I think it's just not a nice way, so I'm curious if there is better way to do that. Based on my researchers on SO and google, I can use System.Data.DataSet to achieve what I want, but:
It is only sightly better that hardcoding all the parameters
It is almost the same, but I produce additional files, IMO it's easier to use and understand ReportParameter way for future code users than getting burried in tons of extra files, that(maybe) could have been avoided
DataSets are for tabular data; I have one big model
Problem
Actually, I think the problem lies in DataSource of the RDLC report itself. The ways that VS provides are created for DB connection or any DB related object. That's why using DataSources and DataSets is indicated. I can provide any data that is either of type IEnumerable or IReportData (or from db connection), but mine are neither. I got my model already build and I'd want to use it if possible.
Most examples I found was for creating reports straight from database or custom data sets. Got no more ideas how to make it work. That's why I'm here.
When using an external component is an option for you: I've been using the .NET reporting component "combit List & Label". It has PDF export and an object dataprovider included that takes any .NET object or even object structure and provides the object`s properties as variables/parameters in an interactive report designer for tables, charts, etc.
I'm relatively new to C# and very new to WPF. I've been trying to wrap my head around the concept of MVVM and I've thrown ADO Entity into the mix now.
The purpose of my sample application is to track CAD items. I'm pulling items out of a database and successfully populating my view; great. I've added the information in manually to test that the views are working as they should be.
I'm now, from my application trying to add a new item through a function which I'm launching from my ICommand. As I understand it, I'm creating a new DBContext object, adding an item to it and saving my changes. Executing "SaveChanges()" successfully tells me that 1 row was updated but when I check, the data isn't there? In addition to this, if, I call SaveChanges() again (within the same debug session) it throws an error to indicate that there's multiple entries. Again, when viewing the data via "Show Table Data" I'm seeing nothing.
public void AddNewItem(object parameter)
{
using (var dbq = new DBEntities()) {
var tempItem = dbq.OutstandingCAD.Create();
tempItem.Id = 2;
dbq.OutstandingCAD.Add(tempItem);
dbq.SaveChanges();
}
}
Could someone please look over that small block of code and suggest whether what I'm doing is correct and whether my issue lies somewhere else?
Much appreciated
Your issue is not with your code but the way you are trying to evaluate your work.
There is a Microsoft article here which discusses the situation you are experiencing. It references Visual Studio 2005, but it is still relevant.
Basically, your database is an .mdf file that is stored in your project. your SQL connection string is something like AttachDbFileName=|DataDirectory|\data.mdf".
One of the things to know when working with local database files is that they are treated as any other content files. For desktop projects, it means that by default, the database file will be copied to the output folder (aka bin) each time the project is built. After F5, here's what it would look like on disk
MyProject\Data.mdf
MyProject\MyApp.vb
MyProject\Bin\Debug\Data.mdf
MyProject\Bin\Debug\MyApp.exe
At design-time, MyProject\Data.mdf is used by the data tools. At run-time, the app will be using the database under the output folder. As a result of the copy, many people have the impression that the app did not save the data to the database file. In fact, this is simply because there are two copies of the data file involved. Same applies when looking at the schema/data through the database explorer. The tools are using the copy in the project, not the one in the bin folder.
Essentially, you have 2 copies of your database, one for design-time, one for run-time. You are looking at the design-time database and not seeing changes that were made to the run-time database. However, every time you run the project, the run-time database gets overwritten, so it can appear that your changes disappear. This is really a debugging feature, to keep you from having hundreds of lines in the database from multiple tests of the same feature. However, some people prefer this kind of persistence, and would use an external SQL instance instead of a project contained .mdf.
I have many Databases with same structure and I have designed a dataset that matches the database design. It is easy to connect to database using connectionStrings which asked at design time and defined in app.config. But the problem arises when trying to change the database at runtime. I can not find any non-reflection solution to handle it. Is there any other way to change connection string of a dataset dynamically at run time or at least create dataset with different connection string!!!
You are filling DataSet using TableAdapter and you can easily modify TableAdapter connection string like this:
myTableAdapter.Connection.ConnectionString = connectionString;
Hope this helps :)
gzaxx answer will not work, simply because different DBMS work with different ADO.NET providers, which may or may not be compatible with each other. There's a lot of theory behind it and I won't type all of that in this textbox, but you need to understand that is the TableAdapters that is the main issue, and not the DataTable. Your business and UI layers normally only talk to DataTables which will mostly have the same structure for almost any DBMS given that you have correctly used corresponding data types when creating table columns. So, in theory, if Typed DataSets could provide a way to attach multiple Adapters per DataTable, you could add one adapter for each DBMS you support, while keeping the DataTable structure the same.
I myself had to deal with this issue in a somewhat large project and the only workable solution for me was to separate my Data Access into a separate project (a class lib) and then create one such DLL for each DBMS I was supporting. Hope that helps you get started with this.
I am bit new to C#. I am working on a Database application and I need to execute a query say a SELECT and load it in to some sort of table view. Then I want to double click on a result and get it loaded to my original data entry form. Can someone tell me the way that I should follow? Dont waste your time to add coding etc. Simply the steps would be enough. A link to a good resource is also be OK :)
Have a look at SqlCommand, SqlDataAdapter and DataTable to retrieve data from the DB. Assuming SQL server, otherwise other DB providers are also available and implement the same API.
For Window Forms look at DataGridView, you can bind a DataTable to it and it will display the data in the DataTable
You can find an example over here
Loot at some samples:
Database Viewer (in sources) http://www.codeproject.com/KB/database/MyDbViewerSite.aspx
Also look at this http-support.microsoft.com/kb/308247
Future Readings:
Data Access Application Block #MSDN
Various Object Relational Mappers (LinqToSQL, Entity Framework, NHibernate etc)
http://www.java2s.com/Code/CSharp/Database-ADO.net/FillDatafromdatabasetabletoListView.htm
ADO.NET
SUMMARY: ADO.NET is a data-access
technology that enables applications
to connect to data stores and
manipulate data contained in them in
various ways. It is based on the .NET
Framework and it is highly integrated
with the rest of the Framework class
library. The ADO.NET API is designed
so it can be used from all programming
languages that target the .NET
Framework, such as Visual Basic, C#,
J# and Visual C++.
I am a newbie to Database programming. After inserting a couple of rows into a DataSet object, I am trying to write back the updated DataSet to the Database but cannot figure out how to do it.
Can you give an example for the following please?
what SQL insert command to use if DataSet is already updated with new rows
Databinding example - especially considering an initially empty table in the DB
I am using SQLExpress 2008 & its C# WinForms application.
thanks
ps: I have already looked at the related questions here. This one is different in that I am first adding my new data into a DataSet and want to update the DB then.
What you need to do is configure a DataAdapter or TableAdapter object that contains the proper Update command. Then when you are done updating the rows in your DataSet, you can call DataAdapter.Update(DataSet) and it will do all the hard work for you.
Since you're starting out, I'd suggest looking at the TableAdapter objects that are built using the XSD schema tool. They allow you to simply drop your tables into the XSD to create a schema, and then let the wizard generate the appropriate SQL commands (it'll even do Stored Procedures for you) to handle all the CRUD work. I've been using these for a while and love them.
EDIT: In response to Sesh's request, Scott Gu has a great tutorial series on using the Table Adapters here. I wanted to post this in the answer so others can find it easily.