Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Easwar
Yak Posting Veteran
59 Posts |
Posted - 2007-04-07 : 03:46:34
|
CNo|CompanyName---------------------1 Infosis2 TCS3 CTS4 Patni5 Wipro-------------This is my example table i need Company Name Only in to String is possible..public string[] company(){SqlDataAdapter da= new SqlDataAdapter("select CompanyName from Company", con);da.Fill(ds,"Company);return ds;}This coding possible.........otherwise need coding .... help me |
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-07 : 10:22:09
|
you are declaring that you will return a string[], but you are just returning a dataset .here is one way to do it; warning, I'm just typing this in, this is not run or tested, possibly errors or mispellings or base casing ... If you can't follow the concept and adapt it, then you should step back and be sure that you understand C# basics by reading a good intro book because these are really very core concepts to understand when writing C# code.public string[] company(){ SqlDataAdapter da= new SqlDataAdapter("select CompanyName from Company", con); da.Fill(ds,"Company); DataTable dt = da.Tables[0]; string[] ret = new string[dt.Rows.Count]; for (int = 0; i< dt.rows.count; i++) ret[i]= dt.rows[0]["CompanyName"].ToString(); return ret;} - Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-04-07 : 22:29:13
|
also, rather than returning string[], you could return List<string> or similar. that way you don't have to know how big the collection will be when you create it. This has the advantage that you don't have to bring the data entirely into memory twice. The way you are doing it, you bring it into memory once in the datatable, and then again in a separate data structure, the string[]. If the list is small, it doesn't really matter. but if it's big, you'll notice a significant perf difference because datatables are pigs as far as memory and perf is concerned.if you use SqlDataReader, then you don't bring everything into memory at once, but just one row at a time:public IList<string> FetchCompanies(SqlConnection connection){ List<string> list = new List<string>(); using (SqlCommand command = new SqlCommand("select CompanyName from Company", connection)) using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { if (reader.IsDBNull(0)) continue; list.Add(reader.GetString(0)); } } return list;} www.elsasoft.org |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-04-08 : 08:47:54
|
Great point, jezemine ... though you may want to mention that you must be using .net 2.0 or above to use generics. Also, I am not so sure that the concept of generics will be easy to grasp if this fellow is having trouble working with arrays.I also never understand why everyone fills up datasets when all they ever need is usually (at worst) a DataTable or (more likely) a DataReader, both of which take up less memory and resources.- Jeffhttp://weblogs.sqlteam.com/JeffS |
|
|
jezemine
Master Smack Fu Yak Hacker
2886 Posts |
Posted - 2007-04-08 : 09:55:45
|
ok, then they could use StringCollection or even ArrayList (also a pig, but less so than dataset) instead of List<>. I think those both exist in CLR 1.0. The point is not to bring data into memory twice. www.elsasoft.org |
|
|
|
|
|
|
|