Solved

How to find SQL server name at runtime using MFC?

Posted on 2013-01-11
14
1,335 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 69

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 42

Expert Comment

by:EugeneZ
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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:enthurao
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 69

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:enthurao
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 69

Assisted Solution

by:Qlemo
Qlemo earned 250 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:
enthurao 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:enthurao
ID: 38797849
I  request experts to download code of code project article
0
 

Author Closing Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

803 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