I am trying to run an insert statement into a DataTable (I'm doing it this way so that all functions in the statement will be executed so I can get the intended values)
private void insertintoDT(DataTable destination, string InsertStatement)
{
SqlCommand comm = new SqlCommand(InsertStatement);
SqlDataReader reader = new SqlDataReader();
destination.Load(reader);
}
}
This DataTable will then be loaded into a DataGrid so I can see the values that will be inserted. I tried breaking up the insert statement like so:
private void breakupInsert(String insert)
{
DataTable dt = new DataTable();
dgvInsert.Rows.Clear();
string stemp = insert;
int len = stemp.Length;
int itemp = 0;
stemp = stemp.Remove(0, 12);
itemp = stemp.IndexOf("VALUES") - 1;
gvtable = stemp.Substring(0, itemp);
stemp = stemp.Remove(0, itemp + 9);
int itemcount = stemp.Count(x => x == ',') + 1;
string[] values = new string[itemcount - 1];
//populate values
int h = 0;//Used as a start index
int itemc = 0; //item index
for (int k = 0; k < stemp.Length; k++)
{
if (k == stemp.Length - 2)
{
values[itemc] = ExecuteSQL(stemp.Substring(h, (k + 1) - h));
break;
}
else
if (stemp.Substring(k, 2) == (", "))
{
itemp = stemp.IndexOf(", ");
values[itemc] = ExecuteSQL(stemp.Substring(h, k - h));
h = k + 2;
itemc = itemc + 1;
k = k + 1;
}
else if (stemp.Substring(k, 2) == ("),"))
{
itemp = stemp.IndexOf("),");
values[itemc] = ExecuteSQL(stemp.Substring(h, k - h));
h = k + 2;
itemc = itemc + 1;
k = k + 1;
}
else if (stemp.Substring(k, 2) == ("',"))
{
itemp = stemp.IndexOf("',");
values[itemc] = ExecuteSQL(stemp.Substring(h, (k - h) + 1));
h = k + 2;
itemc = itemc + 1;
k = k + 1;
}
}
for (int j = 0; j < values.Length; j++)
{
this.dgvInsert.Rows.Insert(j, j + 1, values[j], values[j].Length);
}
}
However this breaks when a function is in the insert statement ( cast for example)
Is there a way to do it or is this a lost cause?
To Clarify: I'm essentially trying to provide the user with a text box that they can paste an SQL statement into, this statement will then be reflected in a datagrid. So if an insert statement is put in, the datatable will reflect the values that are being inserted. But without ever inserting them into the database.