简体   繁体   中英

Test SQL Server Connection without creating database

I am using Entity Framework code-first approach. I want to develop a form in which user will enter SQL Server detail like server name, user ID, password and database name which will be created by code first.

But before creating a database I want to test the connection to SQL Server without creating any test database. Is it possible to test SQL Server connection without creating any database?

I tried creating a test database in that server and verify but this method is quite not acceptable as creating a test database and then delete it is not the solution to the issue.

Please provide any logic that can test SQL Server connection without database.

Look for an open listener on port 1433 (the default port). If you get any response after creating a tcp connection there, the server's probably up.-

         try
        {
            TcpClient client = new TcpClient();
            client.Connect("Your Host", 1433);
            Console.WriteLine("Connection open");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Connection not open");
        }

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