Trying to assign a variable using SQLData Reader

November 11, 2012 at 18:15:58
Specs: XP\7
I have found out how in C# to read from a sql database table. Write now my program is simply using console.writeline to show the value or values read from the table. I would like to...if possible.. take the results from the query and store it as a variable. Is this possible and if so how>? Here is my program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//create a SQL connection object
SqlConnection o_sqlConnection = new SqlConnection();
//provide connection details
//example uses a SQL instance named deployed at a remote physical location
o_sqlConnection.ConnectionString = @"Server=100-nuweb-k-005.adcs.net;Database=Report;User Id=reports;Password=Password$$";


//open connection (SQLconnections need to be opened before query or executing related tasks to database engine
//to handle exceptions, it is common approach to nest connection open method within try catch finally block
o_sqlConnection.Open();

//create a command object to store T-SQL Query
SqlCommand o_sqlCommand = new SqlCommand();
//provide command text
o_sqlCommand.CommandText = "select employee from location";
//provide opened - active SQL connection
o_sqlCommand.Connection = o_sqlConnection;

//create SQL data reader
SqlDataReader o_sqlDataReader;
//execute reader to store back retrieved data into reader object
o_sqlDataReader = o_sqlCommand.ExecuteReader();

//console data presentation sections
//Console.WriteLine("employee...");

//display queried records till last entry returns false as bool for record within reader
while (o_sqlDataReader.Read())
{
//read name column of SQL server table data
Console.WriteLine(o_sqlDataReader["employee"]);
}
//reader returns false after the encounter of last record execution

//close the SQL network level connection: call off the session with database engine
o_sqlConnection.Close();

//close reader
o_sqlDataReader.Close();

Console.Read();

}
}
}

This is currently a console app, but i might end up attempting to do this in a windows form if this can be done.

If there are better ways to do this please let me know.


See More: Trying to assign a variable using SQLData Reader

Report •


#1
November 12, 2012 at 02:13:17
string result;
result = o_sqlDataReader["employee"];

Report •

#2
Report •

#3
November 16, 2012 at 07:55:48
I've tried to do this in a windows form but I am not having any luck, it's like it's not doing anything. Here is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//create a SQL connection object
SqlConnection o_sqlConnection = new SqlConnection();
//provide connection details
//example uses a SQL instance named deployed at a remote physical location
o_sqlConnection.ConnectionString = @"Server=100-nuweb-k-005.adcs.net;Database=Report;User Id=reports;Password=Password$$";


//open connection (SQLconnections need to be opened before query or executing related tasks to database engine
//to handle exceptions, it is common approach to nest connection open method within try catch finally block
o_sqlConnection.Open();

//create a command object to store T-SQL Query
SqlCommand o_sqlCommand = new SqlCommand();
//provide command text
o_sqlCommand.CommandText = "select employee from location";
//provide opened - active SQL connection
o_sqlCommand.Connection = o_sqlConnection;

//create SQL data reader
SqlDataReader o_sqlDataReader;
//execute reader to store back retrieved data into reader object
o_sqlDataReader = o_sqlCommand.ExecuteReader();

//console data presentation sections
//Console.WriteLine("employee...");

//display queried records till last entry returns false as bool for record within reader
while (o_sqlDataReader.Read())
{
//read name column of SQL server table data
MessageBox.Show((o_sqlDataReader["employee"]));.

//string value;
//value = (o_sqlDataReader["employee"].ToString());

MessageBox.Show(value);


}
//reader returns false after the encounter of last record execution

//close the SQL network level connection: call off the session with database engine
o_sqlConnection.Close();

//close reader
o_sqlDataReader.Close();

//Console.Read();

}
}
}


Report •

Related Solutions

#4
November 16, 2012 at 08:04:21
And my suggestion to use a DataSet instead of a SqlDataReader?

How To Ask Questions The Smart Way


Report •

#5
November 19, 2012 at 06:53:17
Well, a dataset could be what I need to do. Here is my main goal: I want a user to be able to enter a computername in a text box and I have a table in my db that has computernames and ipaddresses in it. So i want to be ale to doe something like this when I query the db...
"select ipaddress from computername where computername = 'textbox1.text'". That way whatever pc name the user enters it will the query the db and return the IP Address. Maybe this helps a little explaining things.

Thanks for your help so far.


Report •


Ask Question