B. Morris
B. Morris

Reputation: 75

Very Simple C# program won't connect to Azure SQL Database

I am trying to write a very simple C# Script that establishes a connection to my Azure SQL Database.

using System;
using System.Data.SqlClient;
namespace first_project
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var cb = new SqlConnectionStringBuilder();
            cb.DataSource = "server.bloo.blah.foobar";
            cb.UserID = "user@database";
            cb.Password = "secret";
            cb.InitialCatalog = "database";
            using (SqlConnection connection = new SqlConnection(cb.ConnectionString))
            {
                try
                {
                    connection.Open();
                    Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
                    Console.WriteLine("State: {0}", connection.State);
                }
                catch(Exception e)
                {
                    Console.WriteLine(e.ToString());
                }
            }
        }
    }
    }

From what I have gathered online, this code should be working? However, I am getting the following error

System.AggregateException: One or more errors occurred. ---> System.IO.IOException: Unable to read data from the transport connection: Operation on non-blocking socket would block. ---> System.Net.Sockets.SocketException: Operation on non-blocking socket would block

Could anyone enlighten me on what is going on? Thanks in advance.

Upvotes: 0

Views: 325

Answers (2)

Kevin Farlee
Kevin Farlee

Reputation: 21

In your connection, instead of cb.UserID = "user@database" use cb.UserID = "user"

That should work.

Upvotes: 0

A_kat
A_kat

Reputation: 1527

First the SQL database has a firewall. You have to add your IP through the azure portal.

Secondly make sure your connection string is correct you can look at another sample online. I remember having syntax errors as well, just copy the string that azure gives you.

Upvotes: 1

Related Questions