Connection string for SQL Server ( local database )












2















I added a local database to my application in Visual Studio:



database



and I need connection string for it - here it is:



SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=127.0.0.1.;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";

conn.Open();


When I run that block of code, the whole UI thread stops, like infinite for loop. What is wrong with my connection string?



I'm working with Windows Forms, C#, .NET Framework version 4.5.1










share|improve this question




















  • 1





    Going by the screenshot it looks like you're trying to connect to a database file (.mdf) and not to a server instance. if Data Source=<yourcomputername> doesn't work then try putting the path to the mdf file. E.g. Data Source=C:DataFilters.mdf. I'm guessing that the reason the UI thread stops is because the default connection timeout is 30 seconds, so the connection has 30 seconds to respond before .net throws an exception.

    – Handbag Crab
    Nov 21 '18 at 21:03


















2















I added a local database to my application in Visual Studio:



database



and I need connection string for it - here it is:



SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=127.0.0.1.;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";

conn.Open();


When I run that block of code, the whole UI thread stops, like infinite for loop. What is wrong with my connection string?



I'm working with Windows Forms, C#, .NET Framework version 4.5.1










share|improve this question




















  • 1





    Going by the screenshot it looks like you're trying to connect to a database file (.mdf) and not to a server instance. if Data Source=<yourcomputername> doesn't work then try putting the path to the mdf file. E.g. Data Source=C:DataFilters.mdf. I'm guessing that the reason the UI thread stops is because the default connection timeout is 30 seconds, so the connection has 30 seconds to respond before .net throws an exception.

    – Handbag Crab
    Nov 21 '18 at 21:03
















2












2








2








I added a local database to my application in Visual Studio:



database



and I need connection string for it - here it is:



SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=127.0.0.1.;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";

conn.Open();


When I run that block of code, the whole UI thread stops, like infinite for loop. What is wrong with my connection string?



I'm working with Windows Forms, C#, .NET Framework version 4.5.1










share|improve this question
















I added a local database to my application in Visual Studio:



database



and I need connection string for it - here it is:



SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=127.0.0.1.;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";

conn.Open();


When I run that block of code, the whole UI thread stops, like infinite for loop. What is wrong with my connection string?



I'm working with Windows Forms, C#, .NET Framework version 4.5.1







c# sql-server winforms connection-string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 21:42









marc_s

583k13011241270




583k13011241270










asked Nov 21 '18 at 20:46









Aleksa DjuricAleksa Djuric

132




132








  • 1





    Going by the screenshot it looks like you're trying to connect to a database file (.mdf) and not to a server instance. if Data Source=<yourcomputername> doesn't work then try putting the path to the mdf file. E.g. Data Source=C:DataFilters.mdf. I'm guessing that the reason the UI thread stops is because the default connection timeout is 30 seconds, so the connection has 30 seconds to respond before .net throws an exception.

    – Handbag Crab
    Nov 21 '18 at 21:03
















  • 1





    Going by the screenshot it looks like you're trying to connect to a database file (.mdf) and not to a server instance. if Data Source=<yourcomputername> doesn't work then try putting the path to the mdf file. E.g. Data Source=C:DataFilters.mdf. I'm guessing that the reason the UI thread stops is because the default connection timeout is 30 seconds, so the connection has 30 seconds to respond before .net throws an exception.

    – Handbag Crab
    Nov 21 '18 at 21:03










1




1





Going by the screenshot it looks like you're trying to connect to a database file (.mdf) and not to a server instance. if Data Source=<yourcomputername> doesn't work then try putting the path to the mdf file. E.g. Data Source=C:DataFilters.mdf. I'm guessing that the reason the UI thread stops is because the default connection timeout is 30 seconds, so the connection has 30 seconds to respond before .net throws an exception.

– Handbag Crab
Nov 21 '18 at 21:03







Going by the screenshot it looks like you're trying to connect to a database file (.mdf) and not to a server instance. if Data Source=<yourcomputername> doesn't work then try putting the path to the mdf file. E.g. Data Source=C:DataFilters.mdf. I'm guessing that the reason the UI thread stops is because the default connection timeout is 30 seconds, so the connection has 30 seconds to respond before .net throws an exception.

– Handbag Crab
Nov 21 '18 at 21:03














2 Answers
2






active

oldest

votes


















1














Part of your problem is you have a trailing '.' in your IP address. Remove that like so:



"Data Source=127.0.0.1;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";


Also, I would strongly suggest that you wrap your connection object in a using statement like this:



using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString =
"Data Source=127.0.0.1.;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";

conn.Open();
}


Lastly, define your connection in a string and pass it into your SqlConnection object when you instantiate it, like this:



string sqlConnection = "Data Source=127.0.0.1;Initial Catalog=Filter;Integrated Security=SSPI;"

using (SqlConnection conn = new SqlConnection(sqlConnection)
{
conn.Open();
}


This approach does several things for you:




  1. It makes your code much, much easier to read, and clean.

  2. It ensures that your connection object will get handled by Dispose even if there is an exception thrown in the using block.

  3. It is just a good habit to get into early.


More on the SqlConnection class here, and more on using can be found here.






share|improve this answer


























  • I removed '.' and still don't work

    – Aleksa Djuric
    Nov 21 '18 at 21:04











  • See the comment above, in your original question. Are you pointing to the right (or any) .mdf file?

    – Brian
    Nov 21 '18 at 21:05



















1














Remove the last dot of the IP address.



"Data Source=127.0.0.1.;" +   


Should be:



"Data Source=127.0.0.1;" +





share|improve this answer


























  • Still don't work

    – Aleksa Djuric
    Nov 21 '18 at 20:59






  • 2





    Make sure sql server is up and running and you can connect to the database using windows authentication with SSMS. If it works, see connectionstrings.com/sql-server for proper syntax

    – MLeblanc
    Nov 21 '18 at 21:04











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420230%2fconnection-string-for-sql-server-local-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Part of your problem is you have a trailing '.' in your IP address. Remove that like so:



"Data Source=127.0.0.1;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";


Also, I would strongly suggest that you wrap your connection object in a using statement like this:



using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString =
"Data Source=127.0.0.1.;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";

conn.Open();
}


Lastly, define your connection in a string and pass it into your SqlConnection object when you instantiate it, like this:



string sqlConnection = "Data Source=127.0.0.1;Initial Catalog=Filter;Integrated Security=SSPI;"

using (SqlConnection conn = new SqlConnection(sqlConnection)
{
conn.Open();
}


This approach does several things for you:




  1. It makes your code much, much easier to read, and clean.

  2. It ensures that your connection object will get handled by Dispose even if there is an exception thrown in the using block.

  3. It is just a good habit to get into early.


More on the SqlConnection class here, and more on using can be found here.






share|improve this answer


























  • I removed '.' and still don't work

    – Aleksa Djuric
    Nov 21 '18 at 21:04











  • See the comment above, in your original question. Are you pointing to the right (or any) .mdf file?

    – Brian
    Nov 21 '18 at 21:05
















1














Part of your problem is you have a trailing '.' in your IP address. Remove that like so:



"Data Source=127.0.0.1;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";


Also, I would strongly suggest that you wrap your connection object in a using statement like this:



using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString =
"Data Source=127.0.0.1.;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";

conn.Open();
}


Lastly, define your connection in a string and pass it into your SqlConnection object when you instantiate it, like this:



string sqlConnection = "Data Source=127.0.0.1;Initial Catalog=Filter;Integrated Security=SSPI;"

using (SqlConnection conn = new SqlConnection(sqlConnection)
{
conn.Open();
}


This approach does several things for you:




  1. It makes your code much, much easier to read, and clean.

  2. It ensures that your connection object will get handled by Dispose even if there is an exception thrown in the using block.

  3. It is just a good habit to get into early.


More on the SqlConnection class here, and more on using can be found here.






share|improve this answer


























  • I removed '.' and still don't work

    – Aleksa Djuric
    Nov 21 '18 at 21:04











  • See the comment above, in your original question. Are you pointing to the right (or any) .mdf file?

    – Brian
    Nov 21 '18 at 21:05














1












1








1







Part of your problem is you have a trailing '.' in your IP address. Remove that like so:



"Data Source=127.0.0.1;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";


Also, I would strongly suggest that you wrap your connection object in a using statement like this:



using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString =
"Data Source=127.0.0.1.;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";

conn.Open();
}


Lastly, define your connection in a string and pass it into your SqlConnection object when you instantiate it, like this:



string sqlConnection = "Data Source=127.0.0.1;Initial Catalog=Filter;Integrated Security=SSPI;"

using (SqlConnection conn = new SqlConnection(sqlConnection)
{
conn.Open();
}


This approach does several things for you:




  1. It makes your code much, much easier to read, and clean.

  2. It ensures that your connection object will get handled by Dispose even if there is an exception thrown in the using block.

  3. It is just a good habit to get into early.


More on the SqlConnection class here, and more on using can be found here.






share|improve this answer















Part of your problem is you have a trailing '.' in your IP address. Remove that like so:



"Data Source=127.0.0.1;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";


Also, I would strongly suggest that you wrap your connection object in a using statement like this:



using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString =
"Data Source=127.0.0.1.;" +
"Initial Catalog=Filter;" +
"Integrated Security=SSPI;";

conn.Open();
}


Lastly, define your connection in a string and pass it into your SqlConnection object when you instantiate it, like this:



string sqlConnection = "Data Source=127.0.0.1;Initial Catalog=Filter;Integrated Security=SSPI;"

using (SqlConnection conn = new SqlConnection(sqlConnection)
{
conn.Open();
}


This approach does several things for you:




  1. It makes your code much, much easier to read, and clean.

  2. It ensures that your connection object will get handled by Dispose even if there is an exception thrown in the using block.

  3. It is just a good habit to get into early.


More on the SqlConnection class here, and more on using can be found here.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 22:20

























answered Nov 21 '18 at 21:01









BrianBrian

4,65472942




4,65472942













  • I removed '.' and still don't work

    – Aleksa Djuric
    Nov 21 '18 at 21:04











  • See the comment above, in your original question. Are you pointing to the right (or any) .mdf file?

    – Brian
    Nov 21 '18 at 21:05



















  • I removed '.' and still don't work

    – Aleksa Djuric
    Nov 21 '18 at 21:04











  • See the comment above, in your original question. Are you pointing to the right (or any) .mdf file?

    – Brian
    Nov 21 '18 at 21:05

















I removed '.' and still don't work

– Aleksa Djuric
Nov 21 '18 at 21:04





I removed '.' and still don't work

– Aleksa Djuric
Nov 21 '18 at 21:04













See the comment above, in your original question. Are you pointing to the right (or any) .mdf file?

– Brian
Nov 21 '18 at 21:05





See the comment above, in your original question. Are you pointing to the right (or any) .mdf file?

– Brian
Nov 21 '18 at 21:05













1














Remove the last dot of the IP address.



"Data Source=127.0.0.1.;" +   


Should be:



"Data Source=127.0.0.1;" +





share|improve this answer


























  • Still don't work

    – Aleksa Djuric
    Nov 21 '18 at 20:59






  • 2





    Make sure sql server is up and running and you can connect to the database using windows authentication with SSMS. If it works, see connectionstrings.com/sql-server for proper syntax

    – MLeblanc
    Nov 21 '18 at 21:04
















1














Remove the last dot of the IP address.



"Data Source=127.0.0.1.;" +   


Should be:



"Data Source=127.0.0.1;" +





share|improve this answer


























  • Still don't work

    – Aleksa Djuric
    Nov 21 '18 at 20:59






  • 2





    Make sure sql server is up and running and you can connect to the database using windows authentication with SSMS. If it works, see connectionstrings.com/sql-server for proper syntax

    – MLeblanc
    Nov 21 '18 at 21:04














1












1








1







Remove the last dot of the IP address.



"Data Source=127.0.0.1.;" +   


Should be:



"Data Source=127.0.0.1;" +





share|improve this answer















Remove the last dot of the IP address.



"Data Source=127.0.0.1.;" +   


Should be:



"Data Source=127.0.0.1;" +






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 20:50









Brian

4,65472942




4,65472942










answered Nov 21 '18 at 20:49









MLeblancMLeblanc

46126




46126













  • Still don't work

    – Aleksa Djuric
    Nov 21 '18 at 20:59






  • 2





    Make sure sql server is up and running and you can connect to the database using windows authentication with SSMS. If it works, see connectionstrings.com/sql-server for proper syntax

    – MLeblanc
    Nov 21 '18 at 21:04



















  • Still don't work

    – Aleksa Djuric
    Nov 21 '18 at 20:59






  • 2





    Make sure sql server is up and running and you can connect to the database using windows authentication with SSMS. If it works, see connectionstrings.com/sql-server for proper syntax

    – MLeblanc
    Nov 21 '18 at 21:04

















Still don't work

– Aleksa Djuric
Nov 21 '18 at 20:59





Still don't work

– Aleksa Djuric
Nov 21 '18 at 20:59




2




2





Make sure sql server is up and running and you can connect to the database using windows authentication with SSMS. If it works, see connectionstrings.com/sql-server for proper syntax

– MLeblanc
Nov 21 '18 at 21:04





Make sure sql server is up and running and you can connect to the database using windows authentication with SSMS. If it works, see connectionstrings.com/sql-server for proper syntax

– MLeblanc
Nov 21 '18 at 21:04


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420230%2fconnection-string-for-sql-server-local-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?