Click here to Skip to main content
15,916,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to run this query from C# and the results will be anywhere from 1 to maybe 20 integers less than 2000, and read those results into an integer array. How do I do that?


SQL
Select DATEDIFF (DAY, DeployedDate, GETDATE())
From Deployment
Where EmployeeID =1 
And
DeploymentID = 1";
Posted
Updated 22-Aug-11 17:57pm
v2

You can make use of ADO .NET in the following steps:

1. Form a connection string.
2. Form your query
3. Add parameters
4. Execute the query with the help of DataReader

These two articles will help you:

Using ADO.NET for beginners[^]

The ADO.NET OleDbDataReader class[^]


EDIT: You can get values like this:

C#
SqlConnection connection = new SqlConnection("YOUR CONNECTION STRING");

ArrayList valuesList = new ArrayList();


connection.Open();
//Read from the database
SqlCommand command = new SqlCommand("YOUR SQL QUERY HERE", connection);

SqlDataReader dataReader = command.ExecuteReader();

while (dataReader.Read())
{
    valuesList.Add(Convert.ToInt32(dataReader[0].ToString()));
}
connection.Close();


Note that for the above code, replace the index of dataReader with the column. Like to get the values of the second column give it a 1. Use an arraylist to get everything into an array.
 
Share this answer
 
v4
Comments
RaisKazi 23-Aug-11 0:59am    
This should be helpfull. My 5.
Nithin Sundar 23-Aug-11 1:35am    
Thanks!
Member 8105842 23-Aug-11 20:17pm    
The generalized reference doesn't help. I have ADO reference. I can use datareader and run the query and get results, but I just don't know how to put the results into an array. The results are integers.
Nithin Sundar 23-Aug-11 23:15pm    
I have updated the solution. Check it.
Member 8105842 24-Aug-11 0:12am    
I just need to dump the results into an integer array.One query calculates the difference in days between 2 date columns and another query calculates how many days ago that one of the dates happened. So neither query reads a column, rather it calculates from either 1 or 2 columns, so I can't reference them.
If your code base is in C# 3.0 and above, LInQ helps

C#
using System.Linq;

var query = from deploy in dbContext.Deployment
            where deploy.EmployeeID == 1 && DeploymentID == 1
            select datediff(deploy.day, deploy.deployeddate, getdate());

return query.toarray();
 
Share this answer
 
v2
You can use DataTable for this purpose. ( since you have only one column, as a result)

1. Fill DataTable, using DataAdapter
2. Now you can access it like this:

Here 'dt' is DatTable filled by DataAdapter:

C#
foreach (DataRow row in dt.Rows) {
    string arrayItem = row[0];  // on every iteration you will be accessing it as an array item
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900