Home c# Connecting to SQL Server on C # for the kettle

Connecting to SQL Server on C # for the kettle




Visual Studio 2012 Ultimate set, SQL Server 2012 was set with her.

Task: Connect to SQL Server , create new local database, execute the database script from the database.sql file (creating tables, procedures, etc. .).

sqlconnection sqlconnection = new sqlconnection ();
SQLConnection = New SqlConnection ("Data Source = Localhost; Integrated Security = SSPI; Initial Catalog = TestDB;");
sqlconnection.open ();
MyClass.ScriptExecute (SqlConnection, "Database.sql");
sqlconnection.close ();
sqlconnection.dispose ();

Problem: Apparently, in the connection bar, and most likely – in server name.

Explain on your fingers, please write there? In the head porridge, I do not understand the fig.
I tried the names of the server name with MSDN and ConnectionString – all the time some errors. Options: localhost, (localhost), (local), localdb, (localdb), (Localdb) \ v.11, were still some, and already completely confused in them all. There are options for connecting to Server Instance – what is this crap? I also do not understand.
As explained to me, you do not need a login password when creating a base, it seems that Windows authentification is used, but again I did not really understand what it was about.

Thank you all for your help.

Answer 1, Authority 100%

SQL Server has two operating modes:

Full instance (named or unnamed).

His presence can be checked in Control Panel / Services:

In brackets written the name of the instance. Mssqlserver is unnamed instances. If you are written there sqlexpress – then the name of the instance and there is sqlexpress.

This instance can be connected by name in the format of the imasserver \ name. The point in the server name is a local machine. Those. To connect to the server from the screenshot, you can use

  • .
  • Localhost – without brackets!
  • Pasha-PC – the name of my car.

Be written there SQLEXPRESS, you could use

  • Localhost \ sqlexpress – without brackets!
  • Pasha-PC \ SQLEXPress – My Machine Name + Instance Name.

The problem with the full instance – access to it must be configured. It allows you to access the one who installed SQL Server, but all other access must be allowed manually. To bypass this problem, there is a second mode of operation:


Localdb is SQL SQL SERVER-A, which exists only for the current user.

To connect to it, the server name is used in the format (LocalDB) \ Name .

To create new instances or view existing ones, you can use the utility C: \ Program Files \ Microsoft SQL Server \ 120 \ Tools \ Binn \ sqllocaldb.exe (the exact path depends on the installed version of SQL Server)

Here is an example of the output of existing instances on my car

& gt; sqllocaldb.exe i

Appropriate connection names – (Localdb) \ MssqlloCaldb , (Localdb) \ ProjectSv12 ….

Usually a pair of instances has already been created by default, in particular instances with the current version – so you meet the mention of (Localdb) \ v11.0 In the examples, it is instance that will almost probably be created by car with 2012 Studio.

Use the service management panel and SQLlocaldb, find out the name of the instance, and enter the corresponding name in Connection String.

Answer 2, Authority 38%

As for authentication for SQL Server . There is two modes authentication on SQL Server-E :

  1. Windows authentication
  2. SQL Server authentication.

Instance can be configured on both connection modes.
To connect to SQL Server-y using the first mode, in the connection string, you must pass

integrated security = sspi;


integrated Security = True;

In this case, built-in security will be used, the check will be taking the account under which you are registered in the OS. In the connection line, the user name SQL Server-A and password are not taken into account and specify them optional.

If you want to go to SQL Server under a specific user registered on sql server-e , then the Integrated Security parameter is missing, Either specify equal to false , and the username parameters and password are specified using the syntax below

user id = *****; password = *****;

In more detail about the syntax of the connection string, depending on the authentication mode, you can read Here

Answer 3, Authority 12%

Outcome. In theory, it was easy to use this server name (highlighted in red). If you do as in my code, before it pre-creating the desired base.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions