Click here to Skip to main content
15,910,123 members
Home / Discussions / Database
   

Database

 
QuestionHow can I... Pin
KORCARI7-Jan-05 13:33
KORCARI7-Jan-05 13:33 
AnswerRe: How can I... Pin
Roger Wright7-Jan-05 14:01
professionalRoger Wright7-Jan-05 14:01 
GeneralMy problem... Pin
KORCARI7-Jan-05 13:30
KORCARI7-Jan-05 13:30 
GeneralRe: My problem... Pin
Colin Angus Mackay8-Jan-05 6:59
Colin Angus Mackay8-Jan-05 6:59 
GeneralSQL Statement Problem Pin
Anonymous7-Jan-05 5:24
Anonymous7-Jan-05 5:24 
GeneralRe: SQL Statement Problem Pin
Mike Ellison7-Jan-05 5:33
Mike Ellison7-Jan-05 5:33 
GeneralRe: SQL Statement Problem Pin
Anonymous7-Jan-05 8:22
Anonymous7-Jan-05 8:22 
GeneralRe: SQL Statement Problem Pin
Mike Ellison7-Jan-05 9:14
Mike Ellison7-Jan-05 9:14 
Hi there. If you are creating a literal SQL statement in code, one thing to remember is that you will be concatenating values into the string, such that the SQL is valid when sent to the database. For example, if your Textbox1 value is "Hello" and your DropDownList1 selected value is "World", you would want your strSQLQuerry variable to contain the literal value:
Select * from Table1 Where Field1 = 'Hello' AND Field2 = 'World'
(assuming Field1 and Field2 are actual fields in Table1). To get such a literal string, you would be concatenating the retrieved values from Textbox1 and DropDownList1 (using the concatenation operator + for C#, or & for VB). For example, in VB this could look something like this:
strSQLQuerry = "Select * From Table1 Where Field1='" & Textbox1.Text _
             & "' AND Field2='" & DropDownList1.SelectedItem.Value & "'"
Or, even better, you could use the String.Format method like this:
strFormatString = "Select * From Table1 Where Field1='{0}' AND Field2='{1}'"
strSQLQuerry = String.Format(strFormatString, Textbox1.Text, DropDownList1.SelectedItem.Value)
I hope these examples help with the conceptualization of building a literal SQL string using form input values. You should be aware though that building these kinds of literal SQL statements (where criteria from a submitted form is embedded directly in the SQL string sent to the database server) leaves you vulnerable to SQL injection attacks. This is where a malevolent user would enter something in an input box on the form specifically designed to screw with your data. One could, for example, enter something like "'; DELETE FROM Table1; --" in your Textbox1. The literal SQL string your code constructs then would look something like this:
Select * From Table1 Where Field1=''; DELETE FROM Table1; --' AND Field2=''
Depending on the database permissions available to the user under which ASP.NET functions, such a statement could mean all your data in Table1 gets deleted.

A better way overall is to use Parameter objects with your SQL statement. The SQL statement you construct in code would use literal parameter placeholders for criteria rather than literal values. These placeholders are database specific. For example, in SQL Server, these are names beginning with the @ symbol; if using the ODBC client, you would use a question mark ? for parameter placeholders. So depending on your database, you might use a string like this:
strSQLQuerry = "Select * From Table1 Where Field1 = @param1 AND Field2 = @param2"
or this
strSQLQuerry = "Select * From Table1 Where Field1 = ? AND Field2 = ?"
You would create a Command object appropriate for your database, set its CommandText property to strSQLQuerry, and add Parameter objects with values to its Parameters collection. For example, assuming you use VB and SQL Server, you may have the following:
'-- setup the select command text
strSQLQuerry = "Select * From Table1 Where Field1 = @param1 AND Field2 = @param2"
 
'--create the SqlCommand object (a connection would need to be set up too)
dim cmd as SqlCommand = new SqlCommand(strSQLQuerry)
 
'--add parameter values retrieved from the submitted form
cmd.Parameters.Add("@param1", Textbox1.Text)
cmd.Parameters.Add("@param2", DropDownBox1.SelectedItem.Value)
 
'--execute the query and fill a dataset with the results
dim da as SqlDataAdapter = new SqlDataAdapter(cmd)
dim ds as DataSet = new DataSet()
da.Fill(ds)
I know this seems like a lot, but parameterized queries are really the way to go. The links I gave you before should help. In the "Server-Side Data Access" quickstart is a section called "Performing a Parameterized Select" that explains this more.

Best wishes and happy new year.
GeneralRe: SQL Statement Problem Pin
Anonymous7-Jan-05 10:23
Anonymous7-Jan-05 10:23 
GeneralRe: SQL Statement Problem Pin
Colin Angus Mackay7-Jan-05 10:39
Colin Angus Mackay7-Jan-05 10:39 
GeneralRe: SQL Statement Problem Pin
Anonymous7-Jan-05 11:07
Anonymous7-Jan-05 11:07 
GeneralRe: SQL Statement Problem Pin
Colin Angus Mackay7-Jan-05 11:13
Colin Angus Mackay7-Jan-05 11:13 
GeneralRe: SQL Statement Problem Pin
Colin Angus Mackay7-Jan-05 10:42
Colin Angus Mackay7-Jan-05 10:42 
GeneralRe: SQL Statement Problem Pin
David Salter7-Jan-05 5:36
David Salter7-Jan-05 5:36 
GeneralRe: SQL Statement Problem Pin
Anonymous7-Jan-05 6:46
Anonymous7-Jan-05 6:46 
GeneralRe: SQL Statement Problem Pin
tecnovate_vivek8-Jan-05 1:37
tecnovate_vivek8-Jan-05 1:37 
Generalinsert record into mysql table Pin
kd83417-Jan-05 3:25
kd83417-Jan-05 3:25 
GeneralRe: insert record into mysql table Pin
David Salter7-Jan-05 5:42
David Salter7-Jan-05 5:42 
QuestionOnly last 50 rows??? Pin
Anonymous7-Jan-05 2:43
Anonymous7-Jan-05 2:43 
AnswerRe: Only last 50 rows??? Pin
Ritesh12347-Jan-05 3:09
Ritesh12347-Jan-05 3:09 
GeneralRe: putting strings into sqlcommand Pin
Colin Angus Mackay6-Jan-05 22:26
Colin Angus Mackay6-Jan-05 22:26 
GeneralExcel text field not taking big CString using VC++ Pin
Anshul Mehra6-Jan-05 20:20
professionalAnshul Mehra6-Jan-05 20:20 
Generalputting strings into sqlcommand Pin
sianatia6-Jan-05 18:34
sianatia6-Jan-05 18:34 
GeneralAngry DBA Pin
Anonymous6-Jan-05 14:24
Anonymous6-Jan-05 14:24 
GeneralODP.NET : How to access function with VARCHAR2 as ReturnValue Pin
arnab19745-Jan-05 16:49
arnab19745-Jan-05 16:49 

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.