Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have made a sp_rename column statement, but because I am using an input box I prefer not to have it where someone could use an sql injection.

here is my code:

Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click

       Dim ipb As String
       ipb = InputBox("New column name")

       Try

           Dim sqlcon As New SqlConnection("sqlconnectionstring")

           sqlcon.Open()
           Dim cmd As SqlClient.SqlCommand

           Dim sql As String = "EXEC sp_rename " + "'" + Form4.ListView1.SelectedItems.Item(0).Text.ToString + "." + ListView1.SelectedItems.Item(0).Text.ToString + "'" + ", '" + ipb.ToString + "', " + "'COLUMN'"
           cmd = New SqlClient.SqlCommand(sql, sqlcon)

           MessageBox.Show(sql.ToString)

           cmd.ExecuteNonQuery()
           sqlcon.Close()

       Catch ex As Exception

       End Try

   End Sub


What I have tried:

tried using cmd.parameters.addwithvalue(@columnname, ipd.tostring), but I assume its not working because it dosent have a reference?
Posted
Updated 22-Apr-22 1:54am
Comments
ZurdoDev 2-Aug-17 13:54pm    
I do not quite understand you.
Member 11856456 2-Aug-17 13:58pm    
I am trying to convert my sql statement into a parameters so that I will not have any chance of sql injections. I want to incorporate the addwithvalue using the input box(ipb).

Yes, they can. All you have to do is match up the parameter names.
If you have this stored procedure:
SQL
CREATE PROC [dbo].Sample
@Name varchar(100)
AS
BEGIN
SELECT * FROM myTable WHERE @Name=Username;
END

Then you can call it like this:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("Sample", con))
        {
        com.CommandType = CommandType.StoredProcedure;
        com.Parameters.AddWithValue("@Name", myNameTextBox.Text);
        using (SqlDataReader read = com.ExecuteReader())
            {
            while (read.Read())
                {
                ...
                }
            }
        }
    }
 
Share this answer
 
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
Share this answer
 
Comments
Richard Deeming 3-Aug-17 11:45am    
That's actually the problem the OP is trying to solve! :D
Patrice T 3-Aug-17 12:25pm    
Ooops, not sure the first version was explicit.
Earlier solutions don't really address using sp_rename specifically...
sp_rename has these parameters objname,newname,objtype

To rename a column use:

VB
'Set variables...
Dim sTable as string = "myTable"
Dim sOldFieldName as string= "FieldToRename"
Dim sNewFieldName as string= "NewFieldName"

'Set parameters...
Dim params As New Generic.List(Of system.Data.SqlClient.SqlParameter)
params.Add(New system.Data.SqlClient.SqlParameter("objname", sTable+"."+sOldFieldName))
params.Add(New system.Data.SqlClient.SqlParameter("newname", sNewFieldName))
params.Add(New system.Data.SqlClient.SqlParameter("objtype", "COLUMN"))

'Call function to exec sp
ExecDataParamsList("EXEC sp_rename @objname, @newname, @objtype",params)
--
'The function to exec sp
function ExecDataParamsList(ByVal sql As String, parameters As Generic.List(Of Data.SqlClient.SqlParameter))
		Using mConnection As SqlClient.SqlConnection = New SqlConnection(GetConnectionString)
			mConnection.Open()
			Using cmd As SqlCommand = New SqlCommand(sql, mConnection)
				cmd.CommandType = CommandType.Text
                Cmd.Parameters.AddRange(parameters.ToArray)
				cmd.CommandText = sql
				cmd.ExecuteNonQuery()
				Cmd.Parameters.Clear()
			End Using
		End Using
End function

This will prevent the "Could not find Stored Procedure sp_rename" error
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900