p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/)
-   BOOK: Professional ADO.NET 3.5 with LINQ and the Entity Framework ISBN: 978-0-470-22988-0 (http://p2p.wrox.com/book-professional-ado-net-3-5-linq-entity-framework-isbn-978-0-470-22988-0-482/)
-   -   Get a certain Field's valuer from DataBase (http://p2p.wrox.com/book-professional-ado-net-3-5-linq-entity-framework-isbn-978-0-470-22988-0/81064-get-certain-fields-valuer-database.html)

Cade_Feng September 26th, 2010 05:37 AM

Get a certain Field's valuer from DataBase
 
Hi,

I want to get a certain field's value from Database;

But I don't know how to get it;

Code:

string _StuName=string.Empty;
SqlConnectionStringBuilder _sqlCon = new SqlConnectionStringBuilder();
_sqlCon.DataSource=".";
_sqlCon.InitialCatalog="Test";
_sqlCon.IntegratedSecurity=true;
SqlConnection con = new SqlConnection(_sqlCon.ConnectionString);
SqlCommand command = new SqlCommand(@"select top 1 StudentName from StudentInfo");

Then How can I got "StudentName" this Field's value?
And keep it.

MohamedHassanAli June 6th, 2011 04:59 AM

Hi Feng!
I know its a very long time you would have found the answer all by yourself but the point here is if someone comes looking for a similar scenario this post might be of use to them. Find the code below i have commented the statements too for you to understand. Lemme know if u have any problem.

Code:

string _StuName=string.Empty;
SqlConnectionStringBuilder _sqlCon = new SqlConnectionStringBuilder();
_sqlCon.DataSource=".";
_sqlCon.InitialCatalog="Test";
_sqlCon.IntegratedSecurity=true;

SqlConnection con = new SqlConnection(_sqlCon.ConnectionString);

//You need to open the connection to the database
con.Open();

SqlCommand command = new SqlCommand(@"select top 1 StudentName from StudentInfo",con); //You need to pass the Query and the connection or you can also use the SqlCommand.Connection to specify the Connection String

//then u can use the SqlCommand.ExecuteReader() method to execute you query
SqlDataReader sDataReader = command.ExecuteReader();

while(sDataReader.Read())
{
        Console.WriteLine(sDataReader[0].ToString()); //sDataReader[0] takes the First Column
}
sDataReader.Close();
con.Close();



All times are GMT -4. The time now is 04:15 AM.

Powered by vBulletin®
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
2013 John Wiley & Sons, Inc.