Database Programming in C#

Started by
10 comments, last by Nik02 17 years, 10 months ago
I need to do some database programming for C#. I've been looking around for some tutorials - but havn't found too much that is useful. Do any of you know of any good ones? Or books?
Advertisement
MSDN has some great video tutorials about using C#, especially with Sql Server. See here. I recommend lessons 8 and 9; while SQLS Express is used in these, other database engines are very similar to use in .net framework.

Niko Suni

Quote:Original post by Nik02
MSDN has some great video tutorials about using C#, especially with Sql Server. See here. I recommend lessons 8 and 9; while SQLS Express is used in these, other database engines are very similar to use in .net framework.


This looks very promising - thank you.
Core C# and .NET is a good book, all around. It has a pretty good chapter on the database stuff.
check out the Linq (c# 3.0) project as it simplifies database programming a lot!
Microsoft DirectX MVP. My Blog: abi.exdream.com
That first post was very useful - but I have a follow up question - I want to create a display for the database so that as I am typing it filters to what I have typed.

For example if I have 5 Names:
Alex
Bob
Matt
Michael
Mike

and I type in a textbox "M"
it would now display:
Matt
Michael
Mike

and if I typed "Mi"
it would display:
Michael
Mike

Any ideas?
You should write a query that returns records based on your parameter. In Transact-SQL (the language of SQL Server), you could do something like this:

SELECT * FROM Persons WHERE Name LIKE (@Name + '%');

In that expression, @Name is a parameter that you give to the query from the host program, and LIKE compares it to the records with using a wildcard (%) so as to return records that begin with your Name parameter.

On C# side, you can specify that the parameter takes it's value from a control property, such as your textbox's text.

Niko Suni

Quote:Original post by Nik02
You should write a query that returns records based on your parameter. In Transact-SQL (the language of SQL Server), you could do something like this:

SELECT * FROM Persons WHERE Name LIKE (@Name + '%');

In that expression, @Name is a parameter that you give to the query from the host program, and LIKE compares it to the records with using a wildcard (%) so as to return records that begin with your Name parameter.

On C# side, you can specify that the parameter takes it's value from a control property, such as your textbox's text.


Thanks - that worked great!
New question regarding this same issue - I got it to filter how I want it filtered, however - it will only display one field (i.e. last name) and not all of the fields into a single string or so.

Does anyone have any ideas?

(when I preview the data in the C# SQL builders and what not - it will display all of the data, but when I run the program, it doesn't.)
You can use composite values when selecting data from your database. For example:

SELECT FirstName + ' ' + LastName AS FullName FROM Persons;

And then binding the FullName composite field to your control.

You can't perform updates on a composite statement (as the server wouldn't know which physical field to update if you tried to update FullName), but for displaying purposes it works very well. It is always preferable to perform value compositions in as low a level as possible, and database is usually the lowest practical level.

Microsoft has SQL Server tutorials available also; you can download the full documentation, that includes samples and tutorials, here.

Niko Suni

This topic is closed to new replies.

Advertisement