【问题标题】:Inserting Record into SQL Server using Xamarin使用 Xamarin 将记录插入 SQL Server
【发布时间】:2016-06-02 10:14:06
【问题描述】:
SqlConnection con = new SqlConnection(@"server=MOON\SQLEXPRESS;Initial Catalog=Moon;Integrated Security=True");

protected override void OnCreate(Bundle bundle)
{
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.Main);

        // Get our button from the layout resource,
        // and attach an event to it
        Button button = FindViewById<Button>(Resource.Id.btnsubmit);

        button.Click += button_Click;
}

void button_Click(object sender, EventArgs e)
{
        try
        {
            EditText etuser = FindViewById<EditText>(Resource.Id.txtuser);
            EditText etpass = FindViewById<EditText>(Resource.Id.txtpass);
            con.Open();
            SqlCommand cmd = new SqlCommand("insert into Login values('" + etuser.Text + "','" + etpass.Text + "')", con);
            cmd.ExecuteReader();
            con.Close();
        }
        catch(Exception ex)
        {
            string result;
            result = ex.Message;
            Toast.MakeText(this, result, ToastLength.Short).Show();
        }
}

请帮帮我,我想插入一条记录,但出现错误

无法解析主机MOON

请帮帮我。我正在使用本地数据库,我的服务器名称也是MOON\SQLExpress

【问题讨论】:

  • SQL Injection alert - 您应该将您的 SQL 语句连接在一起 - 使用 参数化查询 来避免 SQL 注入
  • 您需要为您的服务器指定一个 IP 或完全限定的域名。另请注意,从您的移动应用程序直接连接到数据库服务器是一个可怕的想法。您通常应该使用 Web 服务层来代替。
  • 直接从您的移动应用程序连接到您的数据库不是一个好主意。创建一些将提供所需功能的基本 Web API。或者为移动应用程序使用一些云 API。除非您正在为移动设备开发 sql 管理工作室。
  • 非常感谢您的帮助

标签: sql-server sql-server-2008 xamarin xamarin.android


【解决方案1】:
  1. 在您的 sqlserver 计算机上创建(自托管)网络服务。

  2. 以管理员身份运行您的服务

  3. 使用 SlSvcUtil 创建服务消费类

(下载安装地址:https://www.microsoft.com/de-DE/download/details.aspx?id=28359

  1. 把这个类放到你的 xamarin 项目中

  2. 在您的应用中使用服务

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多