mousemat24
asked on
Manipulate arrays, and placing the resualts to SQL
I've got the following code:
Console.WriteLine(page);
int itemId = 1;
int parentId = 0;
int crtPos = 0;
string allEntries = page.Replace("\n", string.Empty);
allEntries = allEntries.Replace("\r", string.Empty).Replace(" ", string.Empty).Replace("\t" , string.Empty);
string[] values = allEntries.Split('[');
string crtValue = string.Empty;
string query = string.Empty;
string initialCrtValue = string.Empty;
string paveWithIdAndParentId = string.Empty;
string pageWithIdAndParentId = "[";
ArrayList parents = new ArrayList();
for ( int i = 0 ; i < values.Length ; i++ )
{
crtValue = values[i];
if ( crtValue.Length == 0 )
{
continue;
}
initialCrtValue = "[" + crtValue;
itemId ++;
parentId = itemId;
crtPos = crtValue.IndexOf(']');
if ( crtPos == -1 )
{
if ( parents.Count > 0 )
parentId = (int)parents[parents.Count - 1];
parents.Add(itemId);
}
else
{
crtValue = crtValue.Remove(crtPos, 1);
crtPos = crtValue.IndexOf(']');
if ( parents.Count > 0 )
{
parentId = (int)parents[parents.Count - 1];
}
while ( crtPos != -1 )
{
if ( parents.Count > 0 )
{
parents.RemoveAt(parents.C ount - 1);
}
crtValue = crtValue.Remove(crtPos, 1);
crtPos = crtValue.IndexOf(']');
}
}
//query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu _URL,menu_ Target ,menu_description, Id, ParentId) VALUES (%s%d,%d)", values, id, parentId);
query = string.Format("({0}{1},{2} )", crtValue, itemId, parentId);
Console.WriteLine(query);
// execute query string formated above.
int firstComaPos = initialCrtValue.IndexOf(', ');
if ( firstComaPos == -1 ) continue;
initialCrtValue = initialCrtValue.Insert(fir stComaPos + 1, string.Format("{0},{1},", itemId, parentId));
pageWithIdAndParentId += (initialCrtValue);
Do you think you can help me with getting another problem I have, its pretty much the same, but I want to capture all the value and place it in the query string (insert)
So if I had the following
['home.gif', 'Homepage', 'http://www.google.com', 'main','this is a test', '1', '2'
will get converted to:
query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu _URL,menu_ Target ,menu_description, Id, ParentId) VALUES (%a,%b,%c,%d,%e,%f,%g)", icon, title, url, target, desc, itemid, parentid);
Dont know if that made sense?
Console.WriteLine(page);
int itemId = 1;
int parentId = 0;
int crtPos = 0;
string allEntries = page.Replace("\n", string.Empty);
allEntries = allEntries.Replace("\r", string.Empty).Replace(" ", string.Empty).Replace("\t"
string[] values = allEntries.Split('[');
string crtValue = string.Empty;
string query = string.Empty;
string initialCrtValue = string.Empty;
string paveWithIdAndParentId = string.Empty;
string pageWithIdAndParentId = "[";
ArrayList parents = new ArrayList();
for ( int i = 0 ; i < values.Length ; i++ )
{
crtValue = values[i];
if ( crtValue.Length == 0 )
{
continue;
}
initialCrtValue = "[" + crtValue;
itemId ++;
parentId = itemId;
crtPos = crtValue.IndexOf(']');
if ( crtPos == -1 )
{
if ( parents.Count > 0 )
parentId = (int)parents[parents.Count
parents.Add(itemId);
}
else
{
crtValue = crtValue.Remove(crtPos, 1);
crtPos = crtValue.IndexOf(']');
if ( parents.Count > 0 )
{
parentId = (int)parents[parents.Count
}
while ( crtPos != -1 )
{
if ( parents.Count > 0 )
{
parents.RemoveAt(parents.C
}
crtValue = crtValue.Remove(crtPos, 1);
crtPos = crtValue.IndexOf(']');
}
}
//query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu
query = string.Format("({0}{1},{2}
Console.WriteLine(query);
// execute query string formated above.
int firstComaPos = initialCrtValue.IndexOf(',
if ( firstComaPos == -1 ) continue;
initialCrtValue = initialCrtValue.Insert(fir
pageWithIdAndParentId += (initialCrtValue);
Do you think you can help me with getting another problem I have, its pretty much the same, but I want to capture all the value and place it in the query string (insert)
So if I had the following
['home.gif', 'Homepage', 'http://www.google.com', 'main','this is a test', '1', '2'
will get converted to:
query = string.Format("INSERT INTO yourTableHere (menu_Icon,menu_Title,menu
Dont know if that made sense?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
certenly not
but it will work on 'crtValue' variable from the code you posted
A.
but it will work on 'crtValue' variable from the code you posted
A.
ASKER
Thanks Agarici for your help again, man you .NET developers are good!!!
ASKER
string page = "[[null,'dasdas','_self','
Thanks Agarici