Microsoft Sql Express Database Connection String in C#

In this article, you can see Microsoft SQL Express Database Connection String in C#


Microsoft SQL Express Database ConnectionString in C#:

ConnectionString provides the required information to the driver that tells where to find the default connection information. Optionally, you are allowed to specify attribute=value pairs in the connection string to override the default values stored in the data source.

1. .NET Data Provider – Standard Connection with default relative path

using System.Data.Odbc;
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "User Id=UserName;" + 
     "Password=Secret;" + 
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();

2. .NET Data Provider – Trusted Connection with default relative path

using System.Data.Odbc;
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "Integrated Security=true;" + 
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();


4. .NET Data Provider – Standard Connection with custom relative path

using System.Data.OleDb;
using System.Data.SqlClient;
AppDomain.CurrentDomain.SetData("DataDirectory", "C:\MyPath\");
var conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "User Id=UserName;" + 
     "Password=Secret;" + 
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();

5. .NET Data Provider — Trusted Connection with custom relative path

using System.Data.OleDb;
using System.Data.SqlClient;
AppDomain.CurrentDomain.SetData("DataDirectory", "C:\MyPath\");
var conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "Integrated Security=true;" + 
     "AttachDbFilename=|DataDirectory|DataBaseName.mdf;"
conn.Open();

6. NET Data Provider – Standard Connection with absolute path

using System.Data.OleDb;
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "User Id=UserName;" + 
     "Password=Secret;" + 
     "AttachDbFilename=C:\MyPath\DataBaseName.mdf;"
conn.Open();

7. .NET Data Provider – Trusted Connection with absolute path

using System.Data.SqlClient;
using System.Data.SqlClient;
var conn = new SqlConnection();
conn.ConnectionString = 
     "Data Source=.\SQLExpress;" + 
     "User Instance=true;" + 
     "Integrated Security=true;" + 
     "AttachDbFilename=C:\MyPath\DataBaseName.mdf;"
conn.Open();

– Article ends here –



In this article, you can see Microsoft Sql Express Database Connection String in C#. Check out the code snippets and integrate into your code.

In this article, I have listed C# code snippets of connection string for Text Database.Text Database Connection String in C#: Connection String provide ...

In this article, I have listed C# code snippets of connection string for Excel Database.Excel Database Connection String in C#: Connection String ...

In this article, I have listed C# code snippets of connection string for Informix Database.Informix Database Connection String in C#: Connection String ...

In this article, I have listed C# code snippets of connection string for Interbase Database.Interbase Database Connection String in C#: Connection ...

In this article, I have listed C# code snippets of connection string for Sybase Database.Sybase Database Connection String in C#: Connection String ...

In this article, find out list of MySql database connection string with C# code snippets. In this article you can see how to create connector.NET connection ...

In this article, I have listed C# code snippets of connection string for IBM DB2 Database.IBM DB2 Database Connection String in C#: Connection String ...

In this article, I have listed C# code snippets of connection string for Oracle Database.Oracle Database Connection String in C#: Connection String ...

In this article, I have listed C# code snippets of connection string for Microsoft Access Database.Microsoft Access Database Connection String in C#: ...

Show next

If you have any questions, please feel free to share your questions or comments on the comment box below.

Share this:
We will be happy to hear your thoughts

      Leave a reply

      www.troubleshootyourself.com
      Logo