如何使用Xamarin通过表单连接到数据库来创建移动应用

时间:2018-07-18 06:39:19

标签: sql-server xamarin xamarin.forms sql-server-2012-express

我是Microsoft Visual Studio Xamarin的新手 我想用一个带有连接按钮的数据源,用户名,数据库和密码输入测试字段创建一个表单,以连接到Microsoft sql server数据库

我已经创建了连接到sqlserver数据库的接口,但是遇到了一些问题。

strings.xml

<?xml version = "1.0" encoding = "utf-8"?>
<resources>
  <string name = "HelloXamarin">Server</string>
  <string name = "ApplicationName">connect</string>
  <string name = "app_name">connect</string>
  <string name = "ButtonClick">Connect</string>
  <string name = "username">Username</string>
  <string name = "database">Database</string>
  <string name = "password">Password</string>
</resources>

activity_main.axml

<?xml version = "1.0" encoding = "utf-8"?>
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
   android:orientation = "vertical"
   android:background = "#d3d3d3"
   android:layout_width = "fill_parent"
   android:layout_height = "fill_parent">
  <TextView
     android:text = "@string/HelloXamarin"
     android:textAppearance = "?android:attr/textAppearanceLarge"
     android:layout_width = "match_parent"
     android:layout_height = "wrap_content"
     android:id = "@+id/textView2"
     android:textColor = "@android:color/black" />
  <EditText
    android:id="@+id/plain_text_input"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:inputType="text"/>
  <TextView
 android:text = "@string/database"
 android:textAppearance = "?android:attr/textAppearanceLarge"
 android:layout_width = "match_parent"
 android:layout_height = "wrap_content"
 android:id = "@+id/database"
 android:textColor = "@android:color/black" />
  <EditText
    android:id="@+id/database_text_input"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:inputType="text"/>
  <TextView
   android:text = "@string/username"
   android:textAppearance = "?android:attr/textAppearanceLarge"
   android:layout_width = "match_parent"
   android:layout_height = "wrap_content"
   android:id = "@+id/username"
   android:textColor = "@android:color/black" />
  <EditText
    android:id="@+id/username_text_input"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:inputType="text"/>
    <TextView
   android:text = "@string/password"
   android:textAppearance = "?android:attr/textAppearanceLarge"
   android:layout_width = "match_parent"
   android:layout_height = "wrap_content"
   android:id = "@+id/password"
   android:textColor = "@android:color/black" />
  <EditText
    android:id="@+id/password_text_input"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:inputType="text"/>
  <Button
      android:id = "@+id/MyButton"
      android:layout_width = "fill_parent"
      android:layout_height = "wrap_content"
      android:text = "@string/ButtonClick" />
</LinearLayout>

Mainactivity.Cs

    using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Data.SqlClient;
//using System.Data.Sql;

namespace first
{
    [Activity(Label = "@string/app_name", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            SetContentView(Resource.Layout.activity_main);
            Button button = FindViewById<Button>(Resource.Id.MyButton);
            button.Click += delegate {
                // button.Text = "Hello world I am your first App";
                string connectionString = @"Server='test';Database='connect';User Id='test';Password='test';Trusted_Connection=true";
                string databaseTable = "Connect";
                //string referenceAccountNumber = "0001134919";
                string selectQuery = String.Format("SELECT * FROM {0} ", databaseTable);
                try
                {
                    using (SqlConnection connection = new SqlConnection(connectionString))
                    {
                        //open connection
                        connection.Open();

                        button.Text = "Connection Successful";
                        SqlCommand command = new SqlCommand(selectQuery, connection);

                        command.Connection = connection;
                        command.CommandText = selectQuery;
                        var result = command.ExecuteReader();
                        //check if account exists
                        var exists = result.HasRows;
                    }
                }
                catch (Exception exception)
                {
                    #region connection error
                    AlertDialog.Builder connectionException = new AlertDialog.Builder(this);
                    connectionException.SetTitle("Connection Error");
                    connectionException.SetMessage(exception.ToString());
                    connectionException.SetNegativeButton("Return", delegate { });
                    connectionException.Create();
                    connectionException.Show();
                    #endregion
                }
            };
        }
    }
}

但是程序给出一个错误,即找不到类型或名称空间名称'SqlConnection'(您是否缺少using指令或程序集引用)

但是我已经定义了程序集
 使用System.Data.SqlClient;

任何人都知道错在哪里吗?

1 个答案:

答案 0 :(得分:0)

已解决。从以下位置使用system.data.dll添加对system.data.sqlclient的引用:C:\ Windows \ Microsoft.NET \ Framework \ v2.0.50727

参考文献->添加参考文献->浏览

要确保应用程序可以连接到sqlserver,请使用以下连接字符串:  字符串连接字符串= @“服务器=服务器名称;数据库=数据库名称;用户ID =用户名称;密码=密码”;