You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin应用直连SQL Server失败:需启动SQL Server Browser

Fixing "Cannot connect to SQL Server Browser" in Xamarin Android

Hey, let's sort out that SQL Server connection error in your Xamarin Android app. The message about the SQL Server Browser service is the first clue, but there are a few key configurations you need to check to get this working. Here's what to do:

1. Start the SQL Server Browser Service

This is the immediate fix for the error message you're seeing:

  • Press Win + R, type services.msc and hit Enter to open the Services window.
  • Look for SQL Server Browser in the list.
  • Right-click it, select Properties.
  • Set the Startup type to Automatic, then click Start to launch the service.
  • Click OK to save changes.

2. Enable Remote Connections in SQL Server

Your SQL Server instance needs to allow remote connections from your Android device:

  • Open SQL Server Configuration Manager.
  • Navigate to SQL Server Network Configuration > Protocols for SQLEXPRESS.
  • Right-click TCP/IP and select Enable.
  • Double-click TCP/IP to open its properties, go to the IP Addresses tab.
  • Scroll down to IPAll, set the TCP Port to 1433 (the default SQL Server port).
  • Make sure all listed IP addresses have Enabled set to Yes.
  • Restart the SQL Server (SQLEXPRESS) service from the Services window to apply these changes.

3. Update Your Firewall Settings

Your Windows firewall might be blocking the connection:

  • Open Windows Defender Firewall > Advanced Settings.
  • Create two new inbound rules:
    • One for Port 1433 (SQL Server's default port) to allow TCP traffic.
    • Another for Port 1434 (used by SQL Server Browser) to allow UDP traffic.

4. Fix Your Connection String

Your current connection string won't work from an Android device—here's how to update it:

  • Replace the machine name (COMP106-1) with the IP address of the computer running SQL Server (you can find this by typing ipconfig in Command Prompt and looking for IPv4 Address).
  • Add SQL Server authentication credentials (Windows auth won't work reliably from a mobile device).
  • Your updated string should look like this:
    string ConString = "Data Source=192.168.1.100\\SQLEXPRESS,1433;Initial Catalog=AVTO_SALON;User ID=your_sql_username;Password=your_sql_password;";
    
    Replace 192.168.1.100, your_sql_username, and your_sql_password with your actual details.

5. Add Internet Permission to Your Android App

Make sure your app has permission to access the network:

  • Open AndroidManifest.xml in your Xamarin project.
  • Add this line inside the <manifest> tag:
    <uses-permission android:name="android.permission.INTERNET" />
    

6. Extra Code Checks

A couple of tweaks to your code to avoid crashes:

  • Always check if reader.Read() returns true before accessing data (in case there are no rows in the Avto table):
    if (reader.Read())
    {
        textView1.Text = reader.GetString(1);
    }
    else
    {
        textView1.Text = "No data found";
    }
    
  • Wrap database operations in a try-catch block to handle any unexpected errors:
    try
    {
        using (SqlConnection sqlCon = new SqlConnection(ConString))
        {
            sqlCon.Open();
            SqlCommand cmd = new SqlCommand(SqlCom, sqlCon);
            SqlDataReader reader = cmd.ExecuteReader();
            if (reader.Read())
            {
                textView1.Text = reader.GetString(1);
            }
            else
            {
                textView1.Text = "No data found";
            }
        }
    }
    catch (Exception ex)
    {
        textView1.Text = $"Error: {ex.Message}";
    }
    

One last thing: Make sure your Android device and the SQL Server computer are on the same Wi-Fi network. Test the IP address connection first using SQL Server Management Studio on another computer to confirm it works before trying again in your app.

内容的提问来源于stack exchange,提问作者daniil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 13:33:10