C# ADO.NET Question

D

DaBrain

I can use some help here.

I have been moving from Delphi/C++ Win32 Database Development (ADO) to
C#.Net /ADO.NET the language is not the problem ADO.NET is however
kicking my butt.

I finally get the true power and purpose of a Dataset, wow, that was a
light bulb Not easy when you are still thinking ADO RecordSets almost
got the basics down, , NOT! From what i have read so far, this is a
hurdle for lots of coders.

I have developed a tiny little DB program against SQL Server to test my
skills and all works fine, then I try the most mundane and simple of
tasks and realize I have no clue what I'm doing.

The Simple Problem (Made Small):

I have a DataGrid and a Button I click the button and I want it to
update the current record (Currently Selected in the Grid) Lets say,
update the Field/Colum Named "Price" to 19.95

Old School Delphi

MyTable.Edit;
MyTable.FieldByName("PRICE").Value := 19.95
MyTable.Post;

// ^This would have edited the CURRENT Record.

The Question
How do I Access the "Current" Active Record in a DataGrid
Programmatically and Update one of the fields Programmatically


My Guess is something Like
MyDataSet.Tables[0].SOMETHING // IM LOST HERE

By Asking this question I hope to:
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

I am a good coder, i just need the light bulb to go on- I need that
moment to happen.

My Head is Stuck In something like this:

MyDataSet.Tables[0].Edit;
MyDataSet.Tables[0].Field("PRICE") = 19.95
MyDataSet.Tables[0].Post;
 
D

DaBrain

Light bulb just went off.. or ON as it were! Thank you all anyway! Any
pointers for my .Net ventures are always welcome by email!
 
J

Jani Järvinen [MVP]

Hi DaBrain,

and welcome to the C# world!

I guess you already got your troubles solved, but since I had already
written this post, here goes anyway in case somebody else is looking for the
same info.
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.

You didn't state whether you are using .NET 1.1 or .NET 2.0, since .NET 2.0
has made things somewhat easier, but here's generic view of things.

Firstly, unlike in Delphi, ADO.NET datasets don't have the concept of
"cursors". A C# DataSet is collection of data represented as tables
(DataTable objects). If you know the Delphi TClientDataSet object, then you
can roughly compare a TClientDataSet to a C# DataTable. A DataSet is a
collection of those, i.e. a "set of data".

You can think a DataTable a large table of columns and rows. Unlike in
Delphi, you can access each row with a simple index, from zero to count-1.
To change, say, the fourth row, you could say something like:

myDataSet.Tables["Customers"].Rows[3]["ContactName"] = "Peach";

The thinking goes always first by row, then by column. Remember also, that
just like a Delphi TClientDataSet, a C# DataSet is an in-memory database. So
when you edit the data it contains, the changes are not yet written to a
database (the so-called "disconnected architecture"). Instead, you often use
the Update method of the TableAdapter that you used to fill the dataset from
the database.

Note also that just like in Delphi, you are better of using so-called typed
datasets instead of untyped ones. Using typed datasets gives you
compile-time type checking and cleaner code.

Since there really is a ton of stuff you have to learn (unfortunately or
not), and more than I can write in one post, I suggest that you take a look
at the Microsoft QuickStart Tutorials available here (written for .NET 1.1):

http://samples.gotdotnet.com/quickstart/howto/

At the top, click to "Common Tasks" and then on the left, to "Data and
ADO.NET". Also, try the link "WinForms" at the top, and then follow the
topics under the "Data Access Overview" topic under "Building Applications"
on the left.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

In Delphi, the regular TADOTable and TClientDataSet components know their
"current record", but in .NET, this thing is a bit more complex. What you
have to understand is the concepts of binding managers and currency
managers. Don't be fooled with the term "currency", though. It has nothing
to do with money!

In C#, a currency manager holds the index of the "current record" you are
looking for. Here's a nice article about these topics:

http://www.akadia.com/services/dotnet_databinding.html

A currency manager object has a Position property, which is an integer value
that you can use to access the correct row inside the DataTable inside your
DataSet. It's all neatly described in the above document.

Good luck!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
G

Guest

DataSets and DataTables are not appropriate for many purposes.
It sounds to me like SqlDataReader might be more appropriate (and probably
more efficient).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter
 
C

Cor Ligthert [MVP]

Jani,

Nice written including the currency. To add for the OP because you did that
so well, don't be fooled with the term AcceptChanges. It does not accept the
changes. They are always accepted at a rowchange, AcceptChanges set the
rowstate to nonchanged.

Cor

Jani Järvinen said:
Hi DaBrain,

and welcome to the C# world!

I guess you already got your troubles solved, but since I had already
written this post, here goes anyway in case somebody else is looking for
the same info.
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.

You didn't state whether you are using .NET 1.1 or .NET 2.0, since .NET
2.0 has made things somewhat easier, but here's generic view of things.

Firstly, unlike in Delphi, ADO.NET datasets don't have the concept of
"cursors". A C# DataSet is collection of data represented as tables
(DataTable objects). If you know the Delphi TClientDataSet object, then
you can roughly compare a TClientDataSet to a C# DataTable. A DataSet is a
collection of those, i.e. a "set of data".

You can think a DataTable a large table of columns and rows. Unlike in
Delphi, you can access each row with a simple index, from zero to count-1.
To change, say, the fourth row, you could say something like:

myDataSet.Tables["Customers"].Rows[3]["ContactName"] = "Peach";

The thinking goes always first by row, then by column. Remember also, that
just like a Delphi TClientDataSet, a C# DataSet is an in-memory database.
So when you edit the data it contains, the changes are not yet written to
a database (the so-called "disconnected architecture"). Instead, you often
use the Update method of the TableAdapter that you used to fill the
dataset from the database.

Note also that just like in Delphi, you are better of using so-called
typed datasets instead of untyped ones. Using typed datasets gives you
compile-time type checking and cleaner code.

Since there really is a ton of stuff you have to learn (unfortunately or
not), and more than I can write in one post, I suggest that you take a
look at the Microsoft QuickStart Tutorials available here (written for
.NET 1.1):

http://samples.gotdotnet.com/quickstart/howto/

At the top, click to "Common Tasks" and then on the left, to "Data and
ADO.NET". Also, try the link "WinForms" at the top, and then follow the
topics under the "Data Access Overview" topic under "Building
Applications" on the left.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

In Delphi, the regular TADOTable and TClientDataSet components know their
"current record", but in .NET, this thing is a bit more complex. What you
have to understand is the concepts of binding managers and currency
managers. Don't be fooled with the term "currency", though. It has nothing
to do with money!

In C#, a currency manager holds the index of the "current record" you are
looking for. Here's a nice article about these topics:

http://www.akadia.com/services/dotnet_databinding.html

A currency manager object has a Position property, which is an integer
value that you can use to access the correct row inside the DataTable
inside your DataSet. It's all neatly described in the above document.

Good luck!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
C

Cor Ligthert [MVP]

David,

Why? They both have their own purpose. Just read the message from Jani to
learn something about it.
In most situations is the dataset/datatable concept the best to use, their
are in my opinion situations like classic batch processing were the
SQLDataReader is better in its place.

Cor

David Anton said:
DataSets and DataTables are not appropriate for many purposes.
It sounds to me like SqlDataReader might be more appropriate (and probably
more efficient).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


DaBrain said:
I can use some help here.

I have been moving from Delphi/C++ Win32 Database Development (ADO) to
C#.Net /ADO.NET the language is not the problem ADO.NET is however
kicking my butt.

I finally get the true power and purpose of a Dataset, wow, that was a
light bulb Not easy when you are still thinking ADO RecordSets almost
got the basics down, , NOT! From what i have read so far, this is a
hurdle for lots of coders.

I have developed a tiny little DB program against SQL Server to test my
skills and all works fine, then I try the most mundane and simple of
tasks and realize I have no clue what I'm doing.

The Simple Problem (Made Small):

I have a DataGrid and a Button I click the button and I want it to
update the current record (Currently Selected in the Grid) Lets say,
update the Field/Colum Named "Price" to 19.95

Old School Delphi

MyTable.Edit;
MyTable.FieldByName("PRICE").Value := 19.95
MyTable.Post;

// ^This would have edited the CURRENT Record.

The Question
How do I Access the "Current" Active Record in a DataGrid
Programmatically and Update one of the fields Programmatically


My Guess is something Like
MyDataSet.Tables[0].SOMETHING // IM LOST HERE

By Asking this question I hope to:
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

I am a good coder, i just need the light bulb to go on- I need that
moment to happen.

My Head is Stuck In something like this:

MyDataSet.Tables[0].Edit;
MyDataSet.Tables[0].Field("PRICE") = 19.95
MyDataSet.Tables[0].Post;
 
M

Martin Z

Well, DataReader is better if you're using the DB primarily for
serialization are handling all the relational-stuff DB-side... if
you're mapping the resulting rows directly into your objects, then the
DataReader is all you need. This is common in CSLA-style projects
(which I consider a worst-of-both-worlds approach that would be better
handled with a system like NHibernate).
David,

Why? They both have their own purpose. Just read the message from Jani to
learn something about it.
In most situations is the dataset/datatable concept the best to use, their
are in my opinion situations like classic batch processing were the
SQLDataReader is better in its place.

Cor

David Anton said:
DataSets and DataTables are not appropriate for many purposes.
It sounds to me like SqlDataReader might be more appropriate (and probably
more efficient).
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
Instant Python: VB to Python converter


DaBrain said:
I can use some help here.

I have been moving from Delphi/C++ Win32 Database Development (ADO) to
C#.Net /ADO.NET the language is not the problem ADO.NET is however
kicking my butt.

I finally get the true power and purpose of a Dataset, wow, that was a
light bulb Not easy when you are still thinking ADO RecordSets almost
got the basics down, , NOT! From what i have read so far, this is a
hurdle for lots of coders.

I have developed a tiny little DB program against SQL Server to test my
skills and all works fine, then I try the most mundane and simple of
tasks and realize I have no clue what I'm doing.

The Simple Problem (Made Small):

I have a DataGrid and a Button I click the button and I want it to
update the current record (Currently Selected in the Grid) Lets say,
update the Field/Colum Named "Price" to 19.95

Old School Delphi

MyTable.Edit;
MyTable.FieldByName("PRICE").Value := 19.95
MyTable.Post;

// ^This would have edited the CURRENT Record.

The Question
How do I Access the "Current" Active Record in a DataGrid
Programmatically and Update one of the fields Programmatically


My Guess is something Like
MyDataSet.Tables[0].SOMETHING // IM LOST HERE

By Asking this question I hope to:
1) learn How To Access DataSet's Table Members BY Column Name and
update the value Programmatically.
2) Find out whether or not ADO.NET even knows what the current active
Record is in my DataSet

I am a good coder, i just need the light bulb to go on- I need that
moment to happen.

My Head is Stuck In something like this:

MyDataSet.Tables[0].Edit;
MyDataSet.Tables[0].Field("PRICE") = 19.95
MyDataSet.Tables[0].Post;
 
J

Jani Järvinen [MVP]

Cor,

thanks.

Learning ADO.NET isn't something that I'd call easy, but eventually one will
learn. Because of the complexity, I find it difficult to explain the core
concepts in a compact way; there's just too much to talk about initially.

The MSDN documentation does a good job in my opinion, but the problem is
that the texts are often very reference-like, whereas beginners are often
looking for simpler overviews without too much detail.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
(e-mail address removed)
http://www.saunalahti.fi/janij/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top