String Manipulations – Stripping a string
-
Hi fellow C#-ers I have a question, hope there's someone out there that might give me a possible solution to this issue. Basically I'm trying to parse a string (the query string) to my returnResults method which in turn returns a DataSet object. What I'm trying to achieve is to strip down the query string and work with a certain number of character which in turn will be parsed to my SqlDataAdapter to provide it with the table name. So, let’s say my query string looks like this:
SELECT * FROM Product
All I basically need is the table name, in this case "Product". How is it possible to only strip the string of the character "Product" ?public DataSet returnResults(string query) { SqlDataAdapter sqlDA = null; DataSet ds = null; StringBuilder str = new StringBuilder(); string tableName = null; str.Insert(0, query); tableName = str. //sqlDA.Fill(ds, ""); return ds; }
Your feedback will be much appreciated. Thanks! R -
Hi fellow C#-ers I have a question, hope there's someone out there that might give me a possible solution to this issue. Basically I'm trying to parse a string (the query string) to my returnResults method which in turn returns a DataSet object. What I'm trying to achieve is to strip down the query string and work with a certain number of character which in turn will be parsed to my SqlDataAdapter to provide it with the table name. So, let’s say my query string looks like this:
SELECT * FROM Product
All I basically need is the table name, in this case "Product". How is it possible to only strip the string of the character "Product" ?public DataSet returnResults(string query) { SqlDataAdapter sqlDA = null; DataSet ds = null; StringBuilder str = new StringBuilder(); string tableName = null; str.Insert(0, query); tableName = str. //sqlDA.Fill(ds, ""); return ds; }
Your feedback will be much appreciated. Thanks! RYou can use a regex that looks for the word 'FROM' and then grabs the word after it. You could even find that with string mashing It's a shame your code is dealing with SQL this directly
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
-
Hi fellow C#-ers I have a question, hope there's someone out there that might give me a possible solution to this issue. Basically I'm trying to parse a string (the query string) to my returnResults method which in turn returns a DataSet object. What I'm trying to achieve is to strip down the query string and work with a certain number of character which in turn will be parsed to my SqlDataAdapter to provide it with the table name. So, let’s say my query string looks like this:
SELECT * FROM Product
All I basically need is the table name, in this case "Product". How is it possible to only strip the string of the character "Product" ?public DataSet returnResults(string query) { SqlDataAdapter sqlDA = null; DataSet ds = null; StringBuilder str = new StringBuilder(); string tableName = null; str.Insert(0, query); tableName = str. //sqlDA.Fill(ds, ""); return ds; }
Your feedback will be much appreciated. Thanks! RHi, there are basically two ways: 1) string operations there is String.IndexOf(str) to locate a substring, String.Substring(start, length) to take a substring, String.Trim() to remove white space at start and end, and String.Split(separator, times) to split a string by a separator In your case, I would try something like:
int i=s.IndexOf(" FROM ");
s=s.Substring(i+5); // s now starts right after FROM
string[] sa=s.Split(' ', 2); // split once, at the first space
s=sa[0].Trim(); // first word following FROMalternatively you could split first, then search which array element contains FROM and use the next array element. Or just assume it always must be the fourth element... You should carefully choose your startegy to locate the stuff you need. 2) regular expressions very powerful, not very intuitive; in this case I would prefer string operations :)
Luc Pattyn
-
Hi fellow C#-ers I have a question, hope there's someone out there that might give me a possible solution to this issue. Basically I'm trying to parse a string (the query string) to my returnResults method which in turn returns a DataSet object. What I'm trying to achieve is to strip down the query string and work with a certain number of character which in turn will be parsed to my SqlDataAdapter to provide it with the table name. So, let’s say my query string looks like this:
SELECT * FROM Product
All I basically need is the table name, in this case "Product". How is it possible to only strip the string of the character "Product" ?public DataSet returnResults(string query) { SqlDataAdapter sqlDA = null; DataSet ds = null; StringBuilder str = new StringBuilder(); string tableName = null; str.Insert(0, query); tableName = str. //sqlDA.Fill(ds, ""); return ds; }
Your feedback will be much appreciated. Thanks! Rwatch out from sql injection[^]
-
You can use a regex that looks for the word 'FROM' and then grabs the word after it. You could even find that with string mashing It's a shame your code is dealing with SQL this directly
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
Hi Chistian, Thank you for your response. I also would like to have your ideas when you said
Christian Graus wrote:
It's a shame your code is dealing with SQL this directly
What are your recommendations? I'm open and flexible to advice and ideas. Thanks. R
-
Hi, there are basically two ways: 1) string operations there is String.IndexOf(str) to locate a substring, String.Substring(start, length) to take a substring, String.Trim() to remove white space at start and end, and String.Split(separator, times) to split a string by a separator In your case, I would try something like:
int i=s.IndexOf(" FROM ");
s=s.Substring(i+5); // s now starts right after FROM
string[] sa=s.Split(' ', 2); // split once, at the first space
s=sa[0].Trim(); // first word following FROMalternatively you could split first, then search which array element contains FROM and use the next array element. Or just assume it always must be the fourth element... You should carefully choose your startegy to locate the stuff you need. 2) regular expressions very powerful, not very intuitive; in this case I would prefer string operations :)
Luc Pattyn
Hi Luc, Thank you for providing me with a solution. It makes sense now! I have to be honest, I haven’t had to deal with string manipulation in a long time. I appreciate the feedback. Thanks. R.
-
Hi Chistian, Thank you for your response. I also would like to have your ideas when you said
Christian Graus wrote:
It's a shame your code is dealing with SQL this directly
What are your recommendations? I'm open and flexible to advice and ideas. Thanks. R
I always like to see the data layer confined to stored procedures.
Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert