'New' cannot be used on an interface

202 Views Asked by At

I am just learning to use Dapper for an SQL CE database.

I have typed the following code to open a connection:

    Dim con As IDbConnection
    con.ConnectionString = "Data Source=D:\test.sdf"
    con.Open()

VS underlines the "con" in the line "con.ConnectionString = "Data Source=D:\test.sdf" and tells me: Variable 'con' is used before it has been assigned a value. A null reference exception might occur.

To get rid of this comment, I have tried

Dim con As new IDbConnection

But that did not compile: 'New' cannot be used on an interface.

How do I deal with this?

Edit:

I have now tried the following:

    Dim nStrongType As System.Data.Common.DbConnection()

    Dim con As IDbConnection
    con = nStrongType

That did not work. The compiler told me:

DBConnection cannot be converted to IDBConnection.

Thank you!

0

There are 0 best solutions below