Azure sql and Visual Studio

2021-10-19T21:11:48.403+00:00

how can i connect a database that is on azure servers to my visual studio uwp project. the goal of the project is to make the application work on different PCs from 1 database over the network

Azure SQL Database
0 comments No comments

1 answer

Sort by: Newest
  1. KalyanChanumolu-MSFT 8,136 Reputation points
    2021-10-20T04:27:15.157+00:00

    @Руслан Савченков Welcome to Microsoft Q&A forums.

    We have plenty of code samples here to help you get started.
    Here is a code snippet from one of the sample.

    using System;  
    using System.Text;  
    using System.Data.SqlClient;  
      
    namespace AzureSqlSample  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {  
                string sql;  
                  
                // Build connection string  
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();  
                builder.DataSource = "your_server_name.database.windows.net";   // update me  
                builder.UserID = "your_user";              // update me  
                builder.Password = "your_password";      // update me  
                builder.InitialCatalog = "your_database_name";  // update me  
      
                // Connect to Azure SQL  
                 using (SqlConnection connection = new SqlConnection(builder.ConnectionString))  
                {  
                    try  
                    {  
    		    Console.WriteLine("Connect to Azure SQL and demo Create, Read, Update and Delete operations.");  
      
                        // Connect to Azure SQL  
                        Console.Write("Connecting to Azure SQL ... ");  
                        connection.Open();  
                        Console.WriteLine("Done.");           
      
                        // READ demo  
                        Console.WriteLine("Reading data from table, press any key to continue...");  
                        Console.ReadKey(true);  
                        sql = "SELECT Id, Name, Location FROM Employees;";  
                        using (SqlCommand command = new SqlCommand(sql, connection))  
                        {  
      
                            using (SqlDataReader reader = command.ExecuteReader())  
                            {  
                                while (reader.Read())  
                                {  
                                    Console.WriteLine("{0} {1} {2}", reader.GetInt32(0), reader.GetString(1), reader.GetString(2));  
                                }  
                            }  
                        }  
                    }  
                    catch (SqlException e)  
                    {  
                        Console.WriteLine(e.ToString());  
                    }  
                    finally  
                    {  
                        Console.WriteLine("Cleaning up table.");  
                        using (SqlCommand command = new SqlCommand("DROP TABLE IF EXISTS Employees", connection))  
                        {  
                            command.ExecuteNonQuery();  
                        }  
                    }  
                }  
      
                Console.WriteLine("All done. Press any key to finish...");  
                Console.ReadKey(true);  
            }  
        }  
    }  
    

    Please let us know if you face any issues.

    ----------

    If an answer is helpful, please click on 130616-image.png or upvote 130671-image.png which might help other community members reading this thread.

    Was this answer helpful?

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.