I am doing a school project in C# apps and I decided to create a ticketing system.
I want to impress my teacher (^^) so I decided to add a database for my app.
I have a month to do this so i think I can learn it since I don't have any prior experience with databases.
Could you tell me how to do it? Below is my app, I want to send the info in the TextBox to a database
I already followed the instructions in MSDN which basically tells you how to add a data source in your app. I added northwind dataset to my app, but I don't know what to do with it and how will it be useful with my app...
For a SQL backend, you can use SQLite quite easily. SQLite is simply a file that resides on the local system, so it is totally portable/deployable with your application. It comes with the caveat that the database is not shared between users. It is a single user database. Two people running an application based on SQLite will not share data. For a uni assignment, this is probably not going to be a big deal.
You could also use SQL Server CE (compact edition), which is a stripped down SQL Server implementation which is similar to SQLite (local, embedded, single user). This will allow you to use Visual Studio database tools to design your database.
Once you have a database embedded within your application, you need to design a schema to hold on to this information. If your screenshot is the only data you need to save, a table like the following should do the trick:
TABLE PERSON
COLUMN name varchar(100)
COLUMN address varchar(200)
COLUMN email varchar(100)
COLUMN mobile varchar(15)
You will need to investigate how to create tables in SQL. That should guide you in what you need though. Visual Studio (some versions), also have a database browser/designer.
Then you need to decide how you want to communicate with the database. You have several options.
Linq 2 SQL
Entity
DataTables
Scott Gu has an excellent series on how to use Linq 2 SQL which I would highly recommend reading. It will go the majority of the way to helping you get to where you need.
So now that you have a SQL database and a provider, you can start trying to wire up the database to the form. This is where databinding comes in. You can drag a Data Source onto a form (which is your Person table), and wire up the table to your text fields. There are many examples on the net how to do this.
If you want to take it a step further, look into the ErrorProvider control. It will allow you to bind validation to your data source and text fields. Once again, a few google searches should point you in the right direction.
I haven't provided code samples because this is homework. If you want to impress your teacher, you will do so by truly understanding the technology you're trying to use. These are just pointers in the right direction so you know what it is you can investigate. Best of luck.
That is a pretty broad question, is there something specifically that you need help with? Like connect to the database, use a datareader, etc...?
If you want to impress your teacher, don't refer to MSDN. Use something like couchdb. Don't get caught up in the "prescribed " .net ecosystem.
Related
Let's say I need to create an application like a books library management system for example that has a front-end like Windows form/WPF and it stores information to database. How to approach making such a solution.
Do we need to create a database first with all tables in C# or is the other way.. SQL to C#?
How do people generally do this? Can someone point me to a sample free project or a book that does this to reinforce my understanding.
Both ways are possible. People usually only focus on one side and use a tool for the other.
Database first approach: Codesmith is an exemple of tool that'll generate C# files after you created the database.
Code first approach: Entity Framework is (an exemple) for the other way around, you write your model in C# and it will generate the database accordingly.
Now pointing you at one or the other would be a bit subjective and also not really match Stack Overflow spirit.
I have an c#.net windows based application that uses a database in Microsoft SQL Server 2008. During deployment for very first time to our client(s), we create a copy of our database and deploy it on client(s) remote server along with the UI application. The client database can be on version SQL Server 2005 and higher.
During times the UI application and associated database has gone lots of changes. Since this is a thick client application the client(s) database is not sync with our latest database and unfortunately no one ever made notes of all the changes done. So my challenges are as follows:
How to find any missing columns on database table in Client's Database as compared to my Database? if any?
How to find any missing Primary/Unique Constraints on database table in Client's Database as compared to my Database? if any?
How to find any missing Indexes on database table that exist in Client's Database as compared to my Database? if any?
Please keep in mind the client(s) database size may ranges from 10-100GB, so i cannot plan to just drop all client tables and recreate it.
You can use Data-tier applications. It's built-in feature of SQL Server, so you don't need to use any extra tools.
You can extract data-tier application from your database (in SSMS right-click -> Tasks -> Extract data-tier application) to a DACPAC file, copy the file to the client's server and use it to upgrade the DB there (or generate update script).
It also integrates nicely with SQL Server Data Tools.
For this task, you need a software that compare SQL database. Just like there is a lot of software to compare text, there is a lot to compare database.
Personally, I use AdoptSQLDiff, but there is a bunch. RedGate has developed one also and I know others exists. Just type SQL Database compare in google to find them. You probably can have the job done with the trial period.
These softwares show you which tables was added, deleted or changed. It does the same for views, indexes, triggers, Stored Procedures, User Defined Functions, Constraints. More importantly, those tools generate script to push modifications into the target database. Very handy, but have a look at the script generated, it sometime messes it up by deleting data, but it can be fixed very easily.
There is also the option to compare data in a specific table if you need to.
Here is a screen shot of the interface of another so you know what it's look like.
With SQLServer Management Studio, you can try selecting a database and then Task->Generate Script, selecting appropriate options.
Do the same thing for the 2 db you want to compare. You will get two text files you can compare with a text file software comparer.
Comparison will highlight difference in the db structure.
Not the best way to do it, of course. But it can be a start. If the two dbs are not too different, you should be able to handle the differences
Better option, use some db comparer software. They are meant to compare db structure, constraint indexes and so on. Never used any of them, so cannot give any advice on that
If it is one time thing use any diff tool for DB, VS2010+ has a build in one, allows you to get difference for schema and data in two different files.
If you want to solve problem of your development process, you have wide range of options to implement versioning for data base.
If you are using EF - use Migrations, can't beat that.
If you are only on SQL Server and never looking at other RDBMS, check DAC ( Data-Tier applications, mentioned by Jakub)
Otherwise take a look at more generic solutions, among them I would reccomend you to take a look at DB.UP and if python code is good for you , check Alembic, it allow you to write your migrations using really nice python API.
if nothing works for you, create snapshot of current db schema and start doing differential scripts that you can use with self written tool or DB.UP
I am not sure if this can help, but who knows.
So is there any way to restore the server database on your local environment? If the answer is yes, you can try to join system views for each database and compare them?
I propose something like this(was a quick solution, so please sorry for formatting and other common stuff).
USE [master]
GO
SELECT
LocalDataBaseTable.name AS TableName,
LocalDataBaseTableColumns.name AS [Column],
LocalDataBaseTypes.name AS DataType,
LocalDataBaseTableColumns.max_length,
LocalDataBaseTableColumns.[precision]
INTO #tmpLocalInfo
FROM LocalTable.sys.columns as LocalDataBaseTableColumns
INNER JOIN LocalTable.sys.tables AS LocalDataBaseTable
ON LocalDataBaseTableColumns.object_id = LocalDataBaseTable.object_id
INNER JOIN LocalTable.sys.types AS LocalDataBaseTypes
ON LocalDataBaseTypes.user_type_id = LocalDataBaseTableColumns.user_type_id
SELECT
ServerDataBaseTable.name AS TableName,
ServerDataBaseTableColumns.name AS [Column],
ServerDataBaseTypes.name AS DataType,
ServerDataBaseTableColumns.max_length,
ServerDataBaseTableColumns.[precision]
INTO #tmpServerInfo
FROM ServerTable.sys.columns as ServerDataBaseTableColumns
INNER JOIN ServerTable.sys.tables AS ServerDataBaseTable
ON ServerDataBaseTableColumns.object_id = ServerDataBaseTable.object_id
INNER JOIN ServerTable.sys.types AS ServerDataBaseTypes
ON ServerDataBaseTypes.user_type_id = ServerDataBaseTableColumns.user_type_id
SELECT
#tmpServerInfo.*
FROM #tmpLocalInfo
RIGHT OUTER JOIN #tmpServerInfo
ON #tmpLocalInfo.TableName = #tmpServerInfo.TableName COLLATE DATABASE_DEFAULT
AND #tmpLocalInfo.[Column] = #tmpServerInfo.[Column] COLLATE DATABASE_DEFAULT
WHERE #tmpLocalInfo.[Column] IS NULL
DROP TABLE #tmpLocalInfo
DROP TABLE #tmpServerInfo
This will return all information about missed columns in your local database. The idea is to investigate 'sys' views and to find out if there any suitable solution for you.
You can use this simple script, which show you differences between tables, views, indexes etc.
Compalex is a free lightweight script to compare two database schemas. It
supports MySQL, MS SQL Server and PostgreSQL.
or look at this question Compare two MySQL databases. This question about comparing two MySQL schemas, but some of listed tools supports MSSQL or have MSSQL version (for example http://www.liquibase.org/).
Another answer What is best tool to compare two SQL Server databases (schema and data)?
I am developing an application that requires "some" customization by the enduser afecting the database design (beyond the parametrization).
Now a days this application supports to work with new columns for the existing tables or even new tables in runtime. But the enduser is not capable to alter the tables, and all the design work must be done using Microsoft SQL Server Magament Studio.
My question is: there is any kind of control (or tool) which implements this functionality? I would like to have it embeded in my application, but if it is a external tool wouldn't be so bad.
What I want to have is some tool that let the user define a repository of columns (name, type, size) for example:
CustomerCode, BIGINT, n/a
CustomerName, Varchar, 50
And then create or alter a table or view by adding these predefined columns. And underneath should execute the needed SQL script or maybe using SMO (SQL Management Objects).
Updated
Currently it's the application works with WinForms, but any WPF / ASP solution would be appreciated.
And referring to and enduser I mean a app administrator but not with programing skills.
Purpouse
The purpose is to be able to extend and customize the functionality from the own apllication without having to use SQL Server Managment Studio.
Imagine that you have an ERP, as a user you want to inform in your customers table something that the application was not orginally intended and even has no free fields for it, for example: GPS location, logo, CEO's photo...
Of course I can suggest to some advanced users to install SSMS Express, but that will give them too much freedom. I also think it is a IT tool. What I wanted is to develop the capability to modify the application from the own framework of the application.
If it can be done from the own application, some controls can be performed:
Check that a table field called X will be always defined with the same type,
Disable the capability to modify the non-customizable fields (application fields),
...
Alex, I think you should try to give the user the flexibility to add new information to the database without really change the design of the database.
You can take a look at EAV Model (entity-attribute-value). This give you the flexibilty to add any new attribute to customer without change the design of customer table (e.g.).
One good example is Magento, they did a great job with EAV (entity-attribute-value) but you must know that this design model will hit your performance a little (or a lot depends how you implement it).
It seems that this kind of control does not exists, so I will scratch it from 0.
#BrunoCosta's idea of using an EAV model to extend the standard application is a good practice. But I pretend to have a tool to modify the standard, and have a tool used for the final user and also for the developer. A pseudo SQL Server Studio + Visual Studio embedded in the a application.
Once I develop it, I will to post it in CodeProject.
I think that the control you are looking for may be a little too unique. However, I don't think it would be too difficult create this yourself using other controls/libraries.
I am just finishing up a similar WPF project.
I found that the DataGrid class worked well for representing a table. You can add/remove columns programmatically. Although you would be generating the SQL script yourself.
I have an xl spreadsheet that I'd like to scrap. I'll replace it with a simple windows form application built with c#.
The spreadsheet has a table with just two columns:
Date
Staff Member
So a sample of the table might look like:
01-Jan Jason
02-Jan
03-Jan Mike
As you can see it's very simple. I use Sqlserver everyday but it would seem like overkill to put this table into a db on Sqlserver. What datasouce should I use for the windows form - a text file?
note: not sure if it influences my choice of datasouce but I'm thinking of using a calender tool and maybe a datagridview in the form.
Seems so simple that you can use a simple text file(key-value), but if you want to expand the capability of the program in the future maybe it is better to put the table in a SQLite database, that is a lightweight embedded database and should fit well with your requirements.
Its not overhead to use sql server at all, if you wish to ditch the file/spreadsheet.
Also using sql-server will be a consistent method of data storage if that is what you currently use for other apps.
It really should take very little time to do.
Also assuming your sql setup does automatic backups etc, then it will ensure you don't lose the information. Hands down make use of sql server if you have access to it.
If you are used to using SQL Server everyday, the closest thing to that is SQL Server Compact, as it is very similar to SQL Server but designed instead to be used as a lightweight, standalone option. It interfaces with Visual Studio and you should feel right at home with it.
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"));