C# splitting a string and passing to storedproc in loop

hi,
i have a string "12345690-1239949-499-9878-" it may contain any number of studentID's
its being passed to a stored proc variable of string DataType in SQL Server 2000
the prob is if it exceeds 8307 length it would fail.

I want to split the string and loop thru it and let only 8307 (length) of studentIDs be passed to the stored proc in a loop (calling loop multiple times in the loop) how can it be done...

lets say max is 8307 / 8 digits being passed as studentIDs so the no of records passed = 1038 records passed successfuly. So I can display a message to the client that way
dotnet0824Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Göran AnderssonConnect With a Mentor Commented:
As you are reading from a large string, you should avoid changing the original string. Keep an offset instead, that drastically reduces the memory usage:
string students = "12345690-1239949-499-9878-999999999-888888888-7777777777";
int maxLength = 15;
int offset = 0;
do {
   int pos;
   string currentStudents;
   if (students.Length <= offset + maxLength || (pos = students.LastIndexOf('-', offset + maxLength)) < offset) {
      currentStudents = students.Substring(offset);
   } else {
     currentStudents = students.Substring(offset, pos);
   }
   // Call the sp with currentStudents;
   offset += currentStudents.Length;
} while (offset < students.Length);

Open in new window

0
 
JimBrandleyCommented:
This works. Just change maxLength to 8307.

Jim
private string SplitStudents(ref string students, int maxLength)
{
   string retVal = string.Empty;
   if (students.Length <= maxLength)
   {
      retVal = students.Substring(0);
      students = string.Empty;
   }
   else
   {
      int pos = students.LastIndexOf('-', maxLength);
      if (pos == -1)
      {
         retVal = students.Substring(0);
         students = string.Empty;
      }
      else
      {
         retVal = students.Substring(0, pos);
         students = students.Substring(pos + 1);
      }
   }
   return retVal;
}
private void Test_Click(object sender, EventArgs e)
{
   string students = "12345690-1239949-499-9878-999999999-888888888-7777777777";
   int maxLength = 15;
   string currentStudents = SplitStudents(ref students, maxLength);
   while (currentStudents.Length > 0)
   {
      // Call the sp with currentStudents;
      currentStudents = SplitStudents(ref students, maxLength);
   }
}

Open in new window

0
 
dotnet0824Author Commented:
please can it be shown by splitting
0
 
JimBrandleyConnect With a Mentor Commented:
Do you mean using string.Split()? That will work if you send student identifiers through on at a time. I thought you wanted as many as you can send in one SP invocation. If you want to send them one ID at a time,

string[] studentIDs = students.Split('-");

Then you can iterate over the array of individual studentIDs.

Jim
0
All Courses

From novice to tech pro — start learning today.