简体   繁体   中英

Trying to connect to my local windows SQL database in a UWP application through Visual Studio

I have a UWP app I'm working on in C# using visual studio and when I try to connect to the SQL database the application is returning Error 40 but when I use a windows form application it can connect to it just fine(using the same connection string). The server is a client side server hosted with the windows developer tools.

尝试连接时出现错误消息

This is the relevant code:

SqlConnection conn = new SqlConnection();
//connection string
private string conString = "Server=(local); Database=Diner; User=Temp; Password=12345";

SqlCommand cmd;
conn.ConnectionString = conString;
        cmd = conn.CreateCommand();

        try
        {
            string query = "select * from Meals;";
            cmd.CommandText = query;
            conn.Open();
            SqlDataReader reader = cmd.ExecuteReader();

            lbMeals.Text = "Today's Menu:\n ------------------------------------------\n";

            while (reader.Read())
            {
                int id = (int)reader["MealID"];
                string mealName = (string)reader["MealName"];
                string mealType = (string)reader["MealType"];
                decimal price = (decimal)reader["MealPrice"];
                lbMeals.Text += ($"{ id,5}{mealName,-15} {mealType,-15}{price,10}\n");
            }

            reader.Close();

        }
        catch (Exception ex)
        {
            lbSystemMsg.Text = "Failed to connect to Meals table: " + ex.Message;
            
        }
        finally
        {
            
            cmd.Dispose();
            conn.Close();


        }

This is my first time connecting to a database using UWP so I may not have something configured correctly. As mentioned previously the server is functioning and can be connected to with other applications in visual studio so I'm thinking the issue is some configuration issue with Visual Studio or maybe the incorrect connection string format.

First, please make sure that you've enabled some capabilities in the manifest file, they are Inte.net(Client & Server) , Inte.net(Client) , Private Networks(Client & Server) , and Enterprise Authentication .

Second, I noticed that you are trying to connect to the localhost. UWP apps are running in the sandbox so they are isolated. The local loopback is disabled for UWP apps by default. You need to enable the local loopback for your device.

You could enable the local loopback via this command

c:\>checknetisolation loopbackexempt -a -n=<package family name>

You could disable the local loopback via this command

c:\>checknetisolation loopbackexempt -d -n=<package family name>

Please refer to these documents Enabling loopback for a UWP applicationInterprocess communication (IPC) for more information about the local loopback.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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