Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


arrgh... How to get simple one value to string variable???

Posted on 2003-03-23
Medium Priority
Last Modified: 2010-04-15
I just started to study .NET so my question may be too sily.
How do I get some string value from some table?
In ADO ( old non .NET ) I could open Recordset for example
rs = conn.Execute(someSqlQuery);
and get my data with
sValue = rs.Fields("someField").Value;

How do I do this simple operation in .NET?
Question by:LazyStudent
  • 2
  • 2
  • 2
LVL 23

Expert Comment

ID: 8190205
Here are some samples from documentation.....

SqlCommand myCMD = new SqlCommand("SELECT CategoryID, CategoryName FROM Categories;" +
                                  "SELECT EmployeeID, LastName FROM Employees", nwindConn);

SqlDataReader myReader = myCMD.ExecuteReader();

  Console.WriteLine("\t{0}\t{1}", myReader.GetName(0), myReader.GetName(1));

  while (myReader.Read())
    Console.WriteLine("\t{0}\t{1}", myReader.GetInt32(0), myReader.GetString(1));

} while (myReader.NextResult());



OleDbConnection nwindConn = new OleDbConnection("Provider=SQLOLEDB;Data Source=localhost;" +
                                                "Integrated Security=SSPI;Initial Catalog=northwind");

OleDbCommand selectCMD = new OleDbCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
selectCMD.CommandTimeout = 30;

OleDbDataAdapter custDA = new OleDbDataAdapter();
custDA.SelectCommand = selectCMD;

DataSet custDS = new DataSet();
custDA.Fill(custDS, "Customers");

Author Comment

ID: 8190218
i fear it doesn't answer my question... can you write for me using someSqlQuery, someField values example that performs the same thing I suplied in question?
LVL 23

Accepted Solution

naveenkohli earned 200 total points
ID: 8190261
If you are using Datareader then...

sValue = myReader["someField"].ToString();

and if you are suing DataSet..

sValue = ds.Tables[0].Rows[0]["someField"].ToString();
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 44

Expert Comment

ID: 8194637
LazyStudent, just because you are using C# does NOT mean that you cannot also use standard ADO, just like you have used ADO in the past.  ADO.NET is a NEW tool, NOT a replacement for standard ADO.  YOu can add a reference to normal, standard ADO, by clicking on the Solution Expolrer, then Add Reference, then click on the COM tab, ans scroll down to Microsoft ActiveX Data Objects 2.7 (or whatever is the latest version on you PC).  That will add the necessary ADO com-interop wrapper, and ADO will be accessible from your .NET code.


Author Comment

ID: 8194942
Arthur_Wood: Thank you, I know that.
But my desire ( I hope I am right ) to stay consistent with .NET technologies.
LVL 44

Expert Comment

ID: 8195600
but there are times to CORRECTLY use ADO , in .NET, and other times to CORRECTLY use ADO.NET.  ADO.NET is NOT intended to be used IN ALL cases, within a >NET based program.

ADO.NET is fundamentally geared to use DISCONNECTED recordsets, and does not function particularly weel if you REALLY need to use a CONNECTED recordset, which is the application area targetted by ADO.  It depends on the nature of the application that you are working on.  DO NOT simply run off to ADO.NET because it is the latest "And greatest".  ADO has been greatly IMPROVED with the release of .NET, but it is still the technology of choice for MANY applications within the .NET arena.

That was my point.



Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question