I was wondering if there is a way to open an SQL file and execute it from a c# application without prior connection to a database and also get the name of the default server that is used when SSMS is opened.

at the moment i can open SSMS but i can't for the life of me figure out how to get the server name as well as opening and running an SQL file which is a DB creation script.

The reason i ask is because i've been trying to add a function, that will run if there isn't a complete connection string, that will open SSMS, retrieve the servername and execute an SQL script to create a DB.

this is just for a personal interest and would appreciate the help

Thanks in advance


Recommended Answers

All 5 Replies

From C# application without prior connection, it is impossible to do that.
Besides connection string there is no way to connect to the database.
Incomplete connection string won't execute(it needs to contain the name of the server/path, name of the database and security details)

If you are in a windows domain and have an Active Directory published SQL Server, then you can retrieve it from the AD. If you have more than one, some one should decide wich one should be used. If yopu need help on how to query AD, please open a separate thread.

If no SQL server is published, and if you are sure that this process will execute on the server himself, then you can always use the 'localhost' name if the TCP server & client protocol is active in the SQL Server Configuration Manager both in the server and in the client configurations.

Then you can use the offline utility SQLCMD.EXE to execute any SQL script.

Hope this helps

With-out knowing the connection string of the server it is impossible to retrive the table. If the server machine is created by you(owned by you) then you can create a page in-order to get the records from the table you want.

With-out knowing the connection string of the server it is impossible to retrive the table.

I don't want to query a table because the script i want to execute creates the DB. i want to open SQL server (which i can do) retreive the server name (to complete the connection string) and then run an SQL script (without using the connection string because the DB does not exist yet) which will create the database (the name of the database will always be the same so that will already be in the connection string). As Lolafuertes said there is more than one server so this will be where my issue lies.

Thanks for your time and replys

Sorry if my last post seems quite rude, i've not had the best morning through no fault but my own.

I want my app and the database (which will be created by running an SQL script which i would like to do automatically when the application is run) installed on any workstation and the connection to be automatically generated if that makes sence?

thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.