I have to replace an OracleCommand by a SqlCommand in my code in the context of a database migration. In my code, I have the use of
myOracleCommand.BindByName = true;. The property BindByName doesn't exist in the class SqlCommand so I was wondering if the SqlCommand is 'bindbyname by default' when it comes to parameters and I could thus just delete this line.
1 Answer
You can ignore/not transfer that line (BindByName = true)
Parameters can only be bound by name in SqlCommand and never by position (which is what happens when you set BindByName = true in the OracleCommand).
You will have to change all the parameter names in both the query and possibly the SqlParameter instances as well.
In Oracle its :paramName, in sql server its @paramName. The parameter name in the SqlParameter instance can start with @ or not, if not it will be added by the command when the execution occurs.
2 Comments
Dan Guzman
I just want to add that one can bind parameters by position with a SQL Server backend using
System.Data,Odbc or System.Data,OleDb. It is only System.Data,SqlClient that binds by name, which is the preferred API to access SQL Server from managed code.Igor
@DanGuzman - good point, I updated my answer to specify SqlCommand instead of Sql Server.
SqlParameterinstances as well.SqlParameter. But when it comes to the line of code I mentionned in my post, you agree that I can just delete it?:for parameters, when MS SQL requires@, e.g.select * from MyTable where id = @prm_id->select * from MyTable where id = :prm_id