Solved

How to find SQL server name at runtime using MFC?

Posted on 2013-01-11
14
1,338 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
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

830 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