简体   繁体   English

在 Xamarin.Forms 中使用 SqlConnection?

[英]Using SqlConnection in Xamarin.Forms?

Can I use SqlConnection in Xamarin.Forms ?我可以在Xamarin.Forms中使用SqlConnection吗?

I tend to use this in Windows and Web applications developed in Visual Studio , but I don't know how it would work in Xamarin.我倾向于在 Windows 和 Web 中使用Visual Studio开发的应用程序,但我不知道它在 Xamarin 中如何工作。

I get namespace name does not exist with using System.Data.SQLClient; using System.Data.SQLClient;时我得到命名空间名称不存在 , even after adding references Mono.Data.Sqlite and System.Data . ,即使在添加引用Mono.Data.SqliteSystem.Data之后。

        SqlConnection conn = new SqlConnection(ConnectionString);
        SqlDataAdapter da = new SqlDataAdapter();
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = SQL;
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();

        conn.Open();

        da.Fill(ds);
        conn.Close();

Can this code snippet be used or does it need to be done another way?可以使用此代码片段还是需要以其他方式完成?

I'm adding the code to a class that's part of the Xamarin.Forms project.我将代码添加到 class,它是Xamarin.Forms项目的一部分。

There is no way to make SQL connection.没有办法建立 SQL 连接。 In general platforms supported by Xamarin have no support for traditional databases (except for SQLite if you can count it in there), so such connection wouldn't make sense.通常,Xamarin 支持的平台不支持传统数据库(SQLite 除外,如果可以的话),因此这种连接没有意义。

If you want to connect to real databases in Xamarin, those databases must have the web interface.如果要连接到 Xamarin 中的真实数据库,这些数据库必须具有 Web 界面。 And again that is not Xamarin limitation but rather limitation of nearly all supported platforms.同样,这不是 Xamarin 的限制,而是几乎所有受支持平台的限制。

  1. Right click on your project,右键单击您的项目,
  2. Select Nuget packages选择 Nuget 包
  3. Look for "System.Data.SqlClient寻找“System.Data.SqlClient
  4. Install it And you are good to go for now.安装它 现在你可以开始了。

I didn't try in Xamarin.Forms only in Xamarin.Android and I found it's possible using a simple trick.我没有仅在 Xamarin.Android 中尝试使用 Xamarin.Forms,我发现可以使用一个简单的技巧。 First I tried without success adding NuGet package System.Data.SqlClient directly to the Android project but neither adding "using System.Data.SqlClient;"首先,我尝试将 NuGet 包 System.Data.SqlClient 直接添加到 Android 项目但没有成功添加“使用 System.Data.SqlClient;” it was possible for me to create a SqlConnection obj.我可以创建一个 SqlConnection 对象。 I solved adding both new standard 2.0 library project and the above NuGet Package to this second project and creating all the connection classes inside this one and then added to the first Android project with Add --> Reference and pointed to the library and all works ok.我解决了将新的标准 2.0 库项目和上述 NuGet 包添加到第二个项目并在其中创建所有连接类的问题,然后使用 Add --> Reference 添加到第一个 Android 项目并指向该库,一切正常.

For Xamarin it looks like you need to go one level higher in the reference - Right click on Reference>Add References - scroll down to System.Data and check the box (click OK).对于 Xamarin,您似乎需要在参考中将 go 提高一级 - 右键单击“参考”>“添加参考”- 向下滚动到 System.Data 并选中该框(单击“确定”)。 Error should go away.错误应该是 go。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM