MonoTouch: Can't create DbParameterCollection?
-
26-02-2021 - |
質問
I'm a vb.net guy trying to figure out MonoTouch c#.
I made this data helper:
public static void ExecuteCommand (SqliteConnection cnn, string command, System.Data.Common.DbParameterCollection parameters)
{
using (var c = cnn.CreateCommand()) {
c.CommandText = command;
c.CommandType = CommandType.Text;
foreach (var p in parameters)
{
c.Parameters.Add (p);
}
c.ExecuteNonQuery ();
}
}
And now I want to call the ExecuteCommand...
var parameters = new System.Data.Common.DbParameterCollection();
parameters.Add("@1", DbType.String).Value = "test";
DataConnection.ExecuteCommand ("INSERT INTO mytest (name) VALUES (@)", parameters);
But MonoTouch says...
var parameters = new System.Data.Common.DbParameterCollection(); <-- "Cannot create an instance of the abstract class or interface 'System.Data.Common.DbParameterCollection'"
parameters.Add("@1", DbType.String).Value = "test"; <-- "A local variable 'parameters' cannot be used before it is declared."
I'm sure the answer is pretty easy, but comming from a VB.Net world, this is not obvious to me.
解決
System.Data.Common.DbParameterCollection is abstract as such you cannot create it. You should be creating a (concrete( collection that inherits from it. In SQLite case it would be Mono.Data.Sqlite.SqliteParameterCollection
.
Your second error is likely related to the first, since parameters
could not be compiled correctly.