0

I am trying to use DbParameter class in my vb.net code

Dim ParamPass As New DbParameter
    ParamPass.ParameterName = "@UserPass"
    ParamPass.Value = "xxxxx"

I got this message

new cannot be used on a class that is declared mustinherit

if I removed "New" I got anther error

Object reference not set to an instance of an object

I am building an VB.net application to work with 2 different kinds of databases and I need to use this class to create general parameters to pass to anther function.

Any help?

3 Answers 3

2

DbParameter is an abstract class, which means you cannot create instances of it. You need to work with one of the derived classed for the db system you work with, for example SqlParameter if you are working with SQL Server.

https://learn.microsoft.com/de-de/dotnet/api/system.data.sqlclient.sqlparameter?view=netframework-4.8

Sign up to request clarification or add additional context in comments.

1 Comment

The application works with 2 types of databases; SQL server in Client server mode and SQL server compact in stand alone mode. I am using DbProviderFactory to handle connection to the different databases. I want to write generic code - as much as I can - to work with the different databases types. for example I am using DbDataReader class to create data reader works with the two types of database. That is why I need generic Parameter and so so on
1

If you are creating parameters explicitly then you need to use the same provider for both databases, so you can create the same type of parameter, e.g. OleDbParameter or OdbcParameter. If you are using different providers then you should call the CreateParameter method of your command and it will create the appropriate type. You can assign the result to a DbParameter variable as they will both inherit that class.

6 Comments

I am Using different providers and using CreateParameter is a good but but the problem is I have a public function in a class that handles all data connection. For example I have a function called ExecuteNoneQuery which take two parameters, the first one is SQL statement and the other one is Array of parameters. I want to create a generic parameter to pass to the function
Well don't. That's just bad design. Either create the command with parameters and pass that in or pass in the SQL and the parameter values. The command and the parameters should be created in the same place, either both outside the method or both inside.
Could you please give an example of creating a parameter using CreateParameter ? I googled a lot with no luck. Thank you,
could you please have a look of the code below? Thank you
@jmcilhinney i think the bad design is in .NET. DbParameter requiring the management of a DbCommand, which requires the managing of a DbConnection often makes things awkward. all because DbParameter is an abstract class
|
0

I tried this code and it works fine

dim SQL as string ="Select UserID from Users where pass=@UserPass"
Dim Cmd As New SqlServerCe.SqlCeCommand
    Cmd.CommandText = SQL

    Dim Param = Cmd.CreateParameter
    Param.ParameterName = "@UserPass"
    Param.DbType = DbType.String
    Param.Value = TxtPass.Text

    Cmd.Parameters.Add(Param)

    Dim xdr = PF.ExecuteReader(Cmd)
    If xdr.Read() Then
        UserID = xdr("UserID")
        xdr.Close()
end if

Is this code is OK? anther question, I used to use SqlDbType.NVarChar as parameter data type because I am using Arabic text. can I use DbType.String to replace it with Arabic text. Thank you

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.