1

How do I copy SQL-Server data into an array in c#? What I want to do is that to retrive the data from SQL-Server and store into an array in c# Right now i am having array but its a fixed one i want to copy data from SQL Server in this one.

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        hcVendas.YAxis.Add(new YAxisItem { title = new Title("Y") });
        hcVendas.XAxis.Add(new XAxisItem { categories = new[] { "1994", "1995", "1996", "1997", "1998", "1999", "2000", "2001", "2002" } });

        //New data collection
        var series = new List<Serie>();
        series.Add(new Serie { data = new object[] { 400, 435, 446, 479, 554, 634, 687, 750, 831 } });

        //bind 
        hcVendas.DataSource = series;
        hcVendas.DataBind();


    }

Now how to bind data into new [] and new object [] from particular column of SQL Server

3
  • 1
    What have you got so far? Have you done any kind of data query? Commented Nov 20, 2013 at 9:48
  • Well I am able to sort the data from the server that particular column. but dont know how to store it into an array. I have created the datatable but i want a simple c# array Commented Nov 20, 2013 at 9:49
  • Yep, share the code, so we can use it to show you how to do what you want. Commented Nov 20, 2013 at 9:53

2 Answers 2

2

Well , you don't need to get all the data to c# and then parse it

Let SQL do the hard work. ( which is merely converting to json string).

For example, you can use this sp(though i'm sure there are other alternatives as well) which generates json string representation :

Example :

enter image description here

output :

enter image description here

Now , when you get this into c# , just pass it to javascript as a ready object.

that's all really.

So - create a field :

string myJson = GetFromDb();   //Lazy would be great here.(offtopic)

And in the client side :

var myObj=JSON.parse(<%=myJson %>); //ie8+
//do whatever with myObj....
Sign up to request clarification or add additional context in comments.

10 Comments

to be frank I am not aware with json at all. so this example, is it to be executed in the server it self?
@Saumil The sp does run at the sql server side. but instead of returning raw data (which includes much overhead) to c# - just return the output result of the query which is the json itself. so you wont need json.net or something like that.
can you guide me to any example which will allow me to learn json from the scratch?
@Saumil JSON can be considered in many contexts. : ajax , web services . etc. the whole point is a small structured representation ( as opposed to XML)of data . just google json , youll get a lot of stuff. and ofcourse- you can always ask here.
Database servers is not usually the best place to do xml/json/etc transformations
|
0

Something like this (assuming you have a SqlConnection and SqlCommand object set up):

SqlDataReader reader = cmd.ExecuteReader();
List<string> column_as_list = new List<string>();
while (reader.Read())
{
    column_as_list.Add(reader.GetString(0));
}

Obviously this gets you a list instead of an array. If you desperately need it as an array you probably need to get a count with your select so you know how big to make the array before you loop through.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.