Interacting With a Database
-
I trying to interact with a Microsoft Access Database in an application. Interactions include creating, deleting, and editing entries. In Visual C# Studio, I have created a connection to the database, but I can't figure out what to do from there. Is there a way to cycle through the rows and columns in a database table using a
for
orforeach
loop? I'm thinking it has something to do with the DataSet and other associated classes.
If I had a sig, it would probably go here.
-
I trying to interact with a Microsoft Access Database in an application. Interactions include creating, deleting, and editing entries. In Visual C# Studio, I have created a connection to the database, but I can't figure out what to do from there. Is there a way to cycle through the rows and columns in a database table using a
for
orforeach
loop? I'm thinking it has something to do with the DataSet and other associated classes.
If I had a sig, it would probably go here.
Well, you can extract rows into a dataset and then use a foreach on the DataRows. For instance:
foreach (DataRow dr in dsDataset.Tables[0]) { // Do something with each row. }
Alternatively, you can pull the data out with a DataReader and loop through them using:
while (dr.MoveNext()) { // Do something here. }
Note that this is sometimes called a firehose cursor (something you may want to google).
Deja View - the feeling that you've seen this post before.
-
Well, you can extract rows into a dataset and then use a foreach on the DataRows. For instance:
foreach (DataRow dr in dsDataset.Tables[0]) { // Do something with each row. }
Alternatively, you can pull the data out with a DataReader and loop through them using:
while (dr.MoveNext()) { // Do something here. }
Note that this is sometimes called a firehose cursor (something you may want to google).
Deja View - the feeling that you've seen this post before.
-
That's what I was thinking, but how do I extract rows into a dataset?
If I had a sig, it would probably go here.
Well, one way to do this is:
DataSet ds ; using (OleDbConnection conn = new OleDbConnection("myConnectionDetails")) { using (OleDbDataAdapter cmd = new OleDbDataAdapter ("myCommand", conn)) { conn.Open(); try { cmd.Fill(ds); cmd.Close(); } finally { if (conn.State == ConnectionState.Open) conn.Close(); } } }
Deja View - the feeling that you've seen this post before.
-
Well, one way to do this is:
DataSet ds ; using (OleDbConnection conn = new OleDbConnection("myConnectionDetails")) { using (OleDbDataAdapter cmd = new OleDbDataAdapter ("myCommand", conn)) { conn.Open(); try { cmd.Fill(ds); cmd.Close(); } finally { if (conn.State == ConnectionState.Open) conn.Close(); } } }
Deja View - the feeling that you've seen this post before.
-
I trying to interact with a Microsoft Access Database in an application. Interactions include creating, deleting, and editing entries. In Visual C# Studio, I have created a connection to the database, but I can't figure out what to do from there. Is there a way to cycle through the rows and columns in a database table using a
for
orforeach
loop? I'm thinking it has something to do with the DataSet and other associated classes.
If I had a sig, it would probably go here.
-
And is there a way to put the data from the DataSet back into the database?
Hippophobia- Fear of horses. What... What?
I recommend buying a book on ADO.NET, and perhaps one on SQL ( from the sounds of it )
Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )