January 10, 2011 at 8:57 am
I need to pass list of values to SP and get output as an comma separated list and insert the output values to a table. Am Using 2005 SQL Server
a c# List <> object holds list of values. I need to Pass this list to a stored procedure.
Inside the SP i need to query a table and the Out of SP should be corresponding country id's in this pattern 2,1,7...etc
Now I am doing it this way by looping through the list, adding commas and inserting to a table. but for security and efficiency im asked to do it using SP. Any help is appreciated.
protected string GetCountryIds(List<string> lstCountries)
{
string strCountryIds = string.Empty;
foreach (string strCountry in lstCountries)
{
using (SqlConnection connection = new SqlConnection(connectionInfo))
{
SqlCommand cmd = new SqlCommand(string.Format("{0}{1}{2}", "select * from tblCountries where Country='", strCountry, "'"), connection);
SqlDataAdapter adp = new SqlDataAdapter(cmd);
cmd.CommandType = CommandType.StoredProcedure;
cmd.ExecuteNonQuery();
DataTable dt = new DataTable();
adp.Fill(dt);
if (dt != null)
{
foreach (DataRow dr in dt.Rows)
{
if (dr["CountryId"] != null)
{
strCountryCodes += string.Format("{0}{1}",dr["CountryId"],",");
}
}
}
}
}
strCountryCodes = strCountryCodes.TrimEnd(',');
return strCountryCodes;
protected void PopulateMainTable(List<Video> allVideos)
{
case "Countries":
List<string> lstCountries = (List<string>)propValue;
listValues = GetCountryIds(lstCountries);
break;
case "Categories":
List<string> lstCategories = (List<string>)propValue;
listValues = GetCategoryIds(lstCategories);
break;
BulkCopy(dt, "tbldata");}
[\code]
January 10, 2011 at 9:41 am
This might help: http://www.sqlservercentral.com/articles/Array/70702/
Depending on how you structure things, you need to parse your procedure.
January 10, 2011 at 7:22 pm
Steve Jones - SSC Editor (1/10/2011)
This might help: http://www.sqlservercentral.com/articles/Array/70702/Depending on how you structure things, you need to parse your procedure.
Heh... No! 😛
--Jeff Moden
Change is inevitable... Change for the better is not.
January 10, 2011 at 7:27 pm
sqldoubt (1/10/2011)
... and get output as an comma separated list and insert the output values to a table.
I have to tell you that's a very bad thing to do in most cases. With exceptions so rare that I can't even think of one right now, storing CSV data in a table goes against the idea of normalized data and a whole bunch of other things that will put you in a "world of hurt" T-SQL wise.
That, not withstanding, here are a couple of articles that will help you in your parsing endeavors...
http://www.sqlservercentral.com/articles/T-SQL/63003/
http://www.sqlservercentral.com/articles/T-SQL/62867/
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 4 posts - 1 through 3 (of 3 total)
You must be logged in to reply to this topic. Login to reply