After my last post here revealed and educated me about SQL Injection issues I have been trying to convert my old style code to ado.net and I can't seem to get this right! I'm sure there are tons of errors in this sample (SO NO LAUGHING! - LOL)
Please help...
----------- code sample ----------
<%@ Language=VBScript %>
<%
'here we are getting the info from the login form, scan off apostrophes to prevent sql injection
If InStr(Request.Form("uid"),"'") Then
uid = Replace(Request.Form("uid"),"'"," ")
Else
uid = Request.Form("uid")
End If
If InStr(Request.Form("pwd"),"'") Then
pwd = Replace(Request.Form("pwd"),"'"," ")
Else
pwd = Request.Form("pwd")
End If
' The Query string with replaceable parameters
string strSQL = "Select * From @table Where id = @userid AND pass = @pwd"
' Set the table name we'll work with
string dataTableName = "admins"
' Create a connect string - this one MAY work!
string connectionString = "DSN=ws-admin.data"
'I know I need these but not sure how to cast them
'objConn = Server.CreateObject("ADODB.Connection")
'objConn.Open(connectionString)
' Create a connection to the database
' Confuses me because of the above statements
SqlConnection mySqlConnection = new SqlConnection(connectString)
' Create a command object, set the command string
SqlCommand mySqlCommand = mySqlConnection.CreateCommand()
mySqlCmd.CommandText = strSQL
mySqlCmd.CommandType = CommandType.Text;
' Set the parameter types
mySqlCmd.Parameters.Add("@table", SqlDbType.nChar, 20)
mySqlCmd.Parameters.Add("@userid", SqlDbType.nChar, 20)
mySqlCmd.Parameters.Add("@pwd", SqlDbType.Int)
' Replace the parameters with values
mySqlCmd.Parameters.Add("@table", "admin");
mySqlCmd.Parameters.Add("@userid", uid);
mySqlCmd.Parameters.Add("@pwd", pwd);
' Create the Data Adapter, set the command
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter()
mySqlDataAdapter.SelectCommand = mySqlCmd
' Create a dataset object to hold the results
DataSet myDataSet = new DataSet()
' Open the connection
mySqlConnection.Open()
' Get the query results into our local table
MySqlDataAdapter.Fill(myDataSet, dataTableName)
' See how many rows matched the admin_id and admin_pass
nFailed = myDataTable.Rows.Count
mySqlConnection.Close()
'if the user is found we will set the session okeydokey to TRUE allowing the user to gain entrance
If nFailed = 0 Then
'ooops if we got this far they dont know their login info or
'arent in the database so we send em back to try again
Response.Redirect "backtostart.asp"
Else
Session("admin") = True
'since the admin was found, we'll send them toodling on to the next page
Response.Redirect "whereadminsgo.asp"
End If
%>
--------- end code sample ---------
Please help...
----------- code sample ----------
<%@ Language=VBScript %>
<%
'here we are getting the info from the login form, scan off apostrophes to prevent sql injection
If InStr(Request.Form("uid"),"'") Then
uid = Replace(Request.Form("uid"),"'"," ")
Else
uid = Request.Form("uid")
End If
If InStr(Request.Form("pwd"),"'") Then
pwd = Replace(Request.Form("pwd"),"'"," ")
Else
pwd = Request.Form("pwd")
End If
' The Query string with replaceable parameters
string strSQL = "Select * From @table Where id = @userid AND pass = @pwd"
' Set the table name we'll work with
string dataTableName = "admins"
' Create a connect string - this one MAY work!
string connectionString = "DSN=ws-admin.data"
'I know I need these but not sure how to cast them
'objConn = Server.CreateObject("ADODB.Connection")
'objConn.Open(connectionString)
' Create a connection to the database
' Confuses me because of the above statements
SqlConnection mySqlConnection = new SqlConnection(connectString)
' Create a command object, set the command string
SqlCommand mySqlCommand = mySqlConnection.CreateCommand()
mySqlCmd.CommandText = strSQL
mySqlCmd.CommandType = CommandType.Text;
' Set the parameter types
mySqlCmd.Parameters.Add("@table", SqlDbType.nChar, 20)
mySqlCmd.Parameters.Add("@userid", SqlDbType.nChar, 20)
mySqlCmd.Parameters.Add("@pwd", SqlDbType.Int)
' Replace the parameters with values
mySqlCmd.Parameters.Add("@table", "admin");
mySqlCmd.Parameters.Add("@userid", uid);
mySqlCmd.Parameters.Add("@pwd", pwd);
' Create the Data Adapter, set the command
SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter()
mySqlDataAdapter.SelectCommand = mySqlCmd
' Create a dataset object to hold the results
DataSet myDataSet = new DataSet()
' Open the connection
mySqlConnection.Open()
' Get the query results into our local table
MySqlDataAdapter.Fill(myDataSet, dataTableName)
' See how many rows matched the admin_id and admin_pass
nFailed = myDataTable.Rows.Count
mySqlConnection.Close()
'if the user is found we will set the session okeydokey to TRUE allowing the user to gain entrance
If nFailed = 0 Then
'ooops if we got this far they dont know their login info or
'arent in the database so we send em back to try again
Response.Redirect "backtostart.asp"
Else
Session("admin") = True
'since the admin was found, we'll send them toodling on to the next page
Response.Redirect "whereadminsgo.asp"
End If
%>
--------- end code sample ---------