Solved

How to find SQL server name at runtime using MFC?

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

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
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 

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 70

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 70

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

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

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.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

632 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