Wrox Programmer Forums
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 26th, 2010, 05: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
 
Old June 6th, 2011, 04: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();





Similar Threads
Thread Thread Starter Forum Replies Last Post
Synchronizing local database with Remote Database arnabghosh Classic ASP Databases 2 July 8th, 2009 03:01 AM
Problem to restore database in C#2005 database acmuralee MySQL 0 March 25th, 2008 04: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 05:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.