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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Göran AnderssonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dotnet0824Author Commented:
please can it be shown by splitting
0
JimBrandleyCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.