Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to find SQL server name at runtime using MFC?

Posted on 2013-01-11
14
Medium Priority
?
1,371 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:ADITYA RAO
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
  • +2
14 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 38771863
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 43

Expert Comment

by:Eugene Z
ID: 38772449
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
ID: 38773231
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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 

Author Comment

by:ADITYA RAO
ID: 38774750
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 71

Expert Comment

by:Qlemo
ID: 38774908
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
ID: 38775114
localhost is not the name of the server but the name of the current server
0
 

Author Comment

by:ADITYA RAO
ID: 38778786
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 71

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 38779373
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
ID: 38788071
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:
ADITYA RAO earned 0 total points
ID: 38797842
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:ADITYA RAO
ID: 38797849
I  request experts to download code of code project article
0
 

Author Closing Comment

by:ADITYA RAO
ID: 38813695
I  request experts to download code of code project article
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

664 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