Solved

How to find SQL server name at runtime using MFC?

Posted on 2013-01-11
14
1,319 Views
Last Modified: 2013-12-14
I am develoving a project in VC++,MFC  VS 2010. and SQLEXPRESS.
The need of the project is that after deploying it should find SQL server name automatically and that server name has to be passed as string for creation of DSN.
My  question is
  How to find SQL server name at runtime using MFC ?
0
Comment
Question by:enthurao
  • 5
  • 3
  • 2
  • +2
14 Comments
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
In general, searching for all available MSSQL instances is possible. But what happens if you get more than one replying? It is always a good idea to at least be able to provide the server\instance on command line or in a config file/registry entry, if you are not using a fixed name.
0
 
LVL 42

Expert Comment

by:EugeneZ
Comment Utility
it depends how you set your application:
" it should find SQL server name automatically" try to use config files with the  sql server name \DB that you need to use
0
 
LVL 18

Expert Comment

by:vasto
Comment Utility
If you are trying to access SQLServer installed on the same machine then you can use the computer name or "localhost" (without quotes)  - this will give you the default instance
0
 

Author Comment

by:enthurao
Comment Utility
First let me tell you client pc will be in pathology lab where there is hardly possibility of other instance.second it is found that server name is not always local host.And third thing my boss wants me to find out server name.he shown me by coding in c# and asked me to do in MFC.Fourth I have not heard about config file in MFC. Please let me know how to find server name.
0
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
There are no special config files in MFC, just text files containing required information.
Would you mind showing the C# Code here? It might be much easier for us to translate that into MFC function calls.
0
 
LVL 18

Expert Comment

by:vasto
Comment Utility
localhost is not the name of the server but the name of the current server
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:enthurao
Comment Utility
Here is C# code which finds SQL servers installed on Computer
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer;

namespace SQlServerLister
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            DataTable dataTable = SmoApplication.EnumAvailableSqlServers(true);
            LstServer.ValueMember = "Name";
            LstServer.DataSource = dataTable;
        }

        private void LstServer_SelectedIndexChanged(object sender, EventArgs e)
        {
        //    listBox1.Items.Clear();
        //    if (LstServer.SelectedIndex != -1)
        //    {
        //        string serverName = LstServer.SelectedValue.ToString();
        //        Server server = new Server(serverName);
        //        try
        //        {
        //            foreach (Database database in server.Databases)
        //            {
        //                listBox1.Items.Add(database.Name);
        //            }
        //        }
        //        catch (Exception ex)
        //        {
        //            string exception = ex.Message;
        //        }
        //    }

        }
    }
}

Open in new window

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
Comment Utility
SMO is not available with unmanaged code (i.e. using MFC), so you'll have to fall back to less advanced functions.

The accepted answer at http://social.msdn.microsoft.com/Forums/en-us/sqldataaccess/thread/1c9f6c81-0486-426b-875d-6ec31d3a3bf2 shows some code you could use. You would not use a while loop, but stop after querying for the first instance.
0
 
LVL 8

Expert Comment

by:Pramod Bugudai
Comment Utility
Hi,

To get a SQL Server Database you have to have machine name (Or IP address) where the SQL Server Database is created for you application. Once you get the machine name you can have connection string to connect to it to find the database names in it. Once you get your desired database from your program you can login to it and fetch your data. This is the case if the database is in a remote machine.

If it is a local machine then its a differrent issue. You can follow this link for a local machine.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.initialcatalog.aspx?bcsi_scan_B5880CD9EEC47D61=BsZwoQ/AD+vHoFmtANWj8BFEn1AJAAAAv7IWCQ==&bcsi_scan_filename=system.data.sqlclient.sqlconnectionstringbuilder.initialcatalog.aspx

See if this helps you.
0
 

Accepted Solution

by:
enthurao earned 0 total points
Comment Utility
I  found  exact matching article on CodeProject. Only thing is it enumerate all SQL servers On Network along with local server. But for My application it is fine

http://www.codeproject.com/articles/487/sql-server-and-database-enumerator

Its very good article ,I  request experts to keep it in collection as it can be helpful in future.
0
 

Author Comment

by:enthurao
Comment Utility
I  request experts to download code of code project article
0
 

Author Closing Comment

by:enthurao
Comment Utility
I  request experts to download code of code project article
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now