0

我有一个 xamarin.forms 项目。在 PCL 中,我有一个包含这 3 行代码的类....

1)SqlCommand cmd = new SqlCommand();....(注意:这行代码有效)

2)cmd.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Char, 5)).Direction = ParameterDirection.Input;....(注意:这行代码有效)

3)var userid = cmd.Parameters["@UserID"].Value;....(注意:这行代码不起作用)

我收到这条消息...

““MarshalByRefObject”类型的引用声称它是在“mscorlib”中定义的,但找不到

这 3 行代码在 Android 特定项目中有效,但在 PCL 类中无效

我可以将参数添加到 PCL 类中的 SqlCommand 但无法检索值

在此先感谢您的帮助

4

1 回答 1

0

完整的命名空间 = System.Data.SqlClient.SqlCommand .... 我在 ... C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\MonoAndroid\v1.0 中引用了 System.Data.dll

您引用的 DLL 仅适用于 Mono.Android 项目,不适用于 PCL 项目。它不能用于 PCL 项目。解释请参考这个案例

如果你想在 PCL 中使用 Native Android SQLite,请使用依赖服务

  1. 在您的 PCL 项目中。为您的依赖服务创建一个接口(ISQLDb)并将该服务用于DependencyService.Get<ISQLDb>()

    public interface ISQLDb
    {
        void InitCmd();
    }
    
    private void btnClick_Clicked(object sender, EventArgs e)
    {
        //use the service any where in your PCL project
        ISQLDb db=DependencyService.Get<ISQLDb>();
        db.InitCmd();
    }
    
  2. 在 Mono.Android 项目中为 Service Implementation 创建一个类,并将其注册为 Dependency Service:

    //register the Dependency Service
    [assembly:Xamarin.Forms.Dependency(typeof(SQLDbImpl))]
    namespace SQLCommandDemo.Droid
    {
        //implements the ISQLDb Interface
        public class SQLDbImpl : ISQLDb
        {
            public void InitCmd()
            {
                SqlCommand cmd = new SqlCommand();
                cmd.Parameters.Add(new SqlParameter("@UserID", SqlDbType.Char, 5)).Direction = ParameterDirection.Input;
                var userid = cmd.Parameters["@UserID"].Value;
            }
        }
    }
    
于 2017-03-14T02:49:47.603 回答