Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Professional ADO.NET 3.5 with LINQ and the Entity Framework ISBN: 978-0-470-22988-0
This is the forum to discuss the Wrox book Professional ADO.NET 3.5 with LINQ and the Entity Framework by Roger Jennings; ISBN: 9780470182611
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Professional ADO.NET 3.5 with LINQ and the Entity Framework ISBN: 978-0-470-22988-0 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 26th, 2010, 06:37 AM
Authorized User
 
Join Date: Aug 2010
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Cade_Feng
Post 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.
__________________
Wana be a .net developer.
Sincere yours
Cade_Feng
  #2 (permalink)  
Old June 6th, 2011, 05:59 AM
Authorized User
 
Join Date: Mar 2011
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
Default

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();
 


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronizing local database with Remote Database arnabghosh Classic ASP Databases 2 July 8th, 2009 04:01 AM
Problem to restore database in C#2005 database acmuralee MySQL 0 March 25th, 2008 05:42 AM
Microsoft JET Database Database Engine (0x80040E09 cannielynn0312 Classic ASP Professional 2 December 17th, 2007 02:50 AM
Inserting "TextArea" field's data problem phungleon Classic ASP Databases 11 August 2nd, 2004 06:19 PM



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


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