Click here to Skip to main content
16,005,120 members
Home / Discussions / C#
   

C#

 
GeneralProblem storing data in xml file using serialization Pin
Member 200213128-May-05 20:29
Member 200213128-May-05 20:29 
Questionhow to add icons to context menu ?? Pin
ekynox28-May-05 17:24
ekynox28-May-05 17:24 
AnswerRe: how to add icons to context menu ?? Pin
ekynox1-Jun-05 14:56
ekynox1-Jun-05 14:56 
GeneralProblem with LIKE ' *xxx* ' Pin
quocpt28-May-05 16:18
quocpt28-May-05 16:18 
GeneralRe: Problem with LIKE ' *xxx* ' Pin
Luis Alonso Ramos28-May-05 17:35
Luis Alonso Ramos28-May-05 17:35 
GeneralRe: Problem with LIKE ' *xxx* ' Pin
Colin Angus Mackay28-May-05 22:08
Colin Angus Mackay28-May-05 22:08 
GeneralRe: Problem with LIKE ' *xxx* ' Pin
Luis Alonso Ramos29-May-05 12:52
Luis Alonso Ramos29-May-05 12:52 
GeneralRe: Problem with LIKE ' *xxx* ' Pin
Colin Angus Mackay28-May-05 22:47
Colin Angus Mackay28-May-05 22:47 
As Luis said the wildcard in SQL Server (and most other database systems) is the % percent sign.

Also, he mentioned that you should use parameterised queries, however in your case this is going to be more problematic as you are injecting the table and column names into the SQL statement - something that is not easily parameterised.

What I suggest is that you create a stored procedure for this and in the stored procedure accept the table and column name as parameters. You can then verify that these values are valid.

I have to admit that I couldn't figure it out in a single stage. So here is a safer solution that I present:

CREATE PROCEDURE dbo.BuildSelectStatement
@tableName sysname,
@columnName sysname
AS

-- First, check that the table exists
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES 
               WHERE TABLE_NAME = @tableName 
               AND TABLE_SCHEMA = 'dbo' 
               AND TABLE_TYPE = 'BASE TABLE')
BEGIN
    SELECT '' AS result;
    RETURN
END

-- Second, check that the column exists in the table
IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
               WHERE TABLE_NAME = @tableName 
               AND TABLE_SCHEMA = 'dbo' 
               AND COLUMN_NAME = @columnName)
BEGIN
    SELECT '' AS result;
    RETURN
END

-- Everything is fine, build the SQL Statement.
SELECT 'SELECT * FROM ['+@tableName+'] WHERE ['+@columnName+'] LIKE @comparison' AS result

GO

The above stored procedure will send back an empty string if the data is invalid, or a SQL Statement if the result is fine.

Then in your code you can write something like this:
SqlCommand buildSqlCmd = new SqlCommand("BuildSelectStatement", myConnection);
buildSqlCmd.CommandType = CommandType.StoredProcedure;
buildSqlCmd.Parmaters.Add("@tableName", tableName);
buildSqlCmd.Paramters.Add("@columnName", this.cbColumn.Text);
string sqlStatment = (string)buildSqlCmd.ExecuteScalar();
if (sqlStatment == "")
{
    // Error, the data was invalid - perform error handling
}

SqlCommand runQueryCmd = new SqlCommand(sqlStatement, myConnection);
runQueryCmd.Parameters.Add("@comparison", 
                           string.Format("%{0}%", this.tbKeyword.Text));
SqlDataReader reader = runQueryCmd.ExecuteReader();


Does this help?



DISCLAIMER: I typed all this in directly, it has not been tested, there may be minor errors


My: Blog | Photos
WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More


GeneralRe: Problem with LIKE ' *xxx* ' Pin
quocpt29-May-05 1:53
quocpt29-May-05 1:53 
GeneralReading from a text file and then storing into the database Pin
mhmo28-May-05 9:44
mhmo28-May-05 9:44 
GeneralRe: Reading from a text file and then storing into the database Pin
Robert Rohde28-May-05 9:53
Robert Rohde28-May-05 9:53 
GeneralRe: Reading from a text file and then storing into the database Pin
mhmo28-May-05 19:14
mhmo28-May-05 19:14 
GeneralRe: Reading from a text file and then storing into the database Pin
Robert Rohde28-May-05 20:31
Robert Rohde28-May-05 20:31 
GeneralRe: Reading from a text file and then storing into the database Pin
Uri Lavi28-May-05 10:04
Uri Lavi28-May-05 10:04 
GeneralRe: Reading from a text file and then storing into the database Pin
mhmo28-May-05 21:07
mhmo28-May-05 21:07 
GeneralRe: Reading from a text file and then storing into the database Pin
Uri Lavi28-May-05 23:09
Uri Lavi28-May-05 23:09 
GeneralURGENT URGENT Pin
falandas28-May-05 2:46
falandas28-May-05 2:46 
GeneralRe: URGENT URGENT Pin
WillemM28-May-05 5:20
WillemM28-May-05 5:20 
GeneralRe: URGENT URGENT Pin
Christian Graus28-May-05 11:52
protectorChristian Graus28-May-05 11:52 
Generalenum overloading Pin
hasansheik28-May-05 2:28
hasansheik28-May-05 2:28 
GeneralRe: enum overloading Pin
Robert Rohde28-May-05 3:17
Robert Rohde28-May-05 3:17 
GeneralRe: enum overloading Pin
S. Senthil Kumar28-May-05 19:03
S. Senthil Kumar28-May-05 19:03 
QuestionSql or Xml ? Pin
MoustafaS28-May-05 2:27
MoustafaS28-May-05 2:27 
AnswerRe: Sql or Xml ? Pin
Robert Rohde28-May-05 3:27
Robert Rohde28-May-05 3:27 
GeneralRe: Sql or Xml ? Pin
MoustafaS28-May-05 3:57
MoustafaS28-May-05 3:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.