Xamarin应用直连SQL Server失败:需启动SQL Server Browser
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, typeservices.mscand 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 typingipconfigin 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:
Replacestring ConString = "Data Source=192.168.1.100\\SQLEXPRESS,1433;Initial Catalog=AVTO_SALON;User ID=your_sql_username;Password=your_sql_password;";192.168.1.100,your_sql_username, andyour_sql_passwordwith your actual details.
5. Add Internet Permission to Your Android App
Make sure your app has permission to access the network:
- Open
AndroidManifest.xmlin 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()returnstruebefore accessing data (in case there are no rows in theAvtotable):if (reader.Read()) { textView1.Text = reader.GetString(1); } else { textView1.Text = "No data found"; } - Wrap database operations in a
try-catchblock 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

