Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'SqlConnection' does not contain a constructor that takes 4 arguments

Tags:

c#

sql-server

This is the code:

public void SqlDbConnect()
{
    SqlConnection conn = new SqlConnection("Data Source={0};User ID={1};Password={2};", server, user, password);
    conn.Open();
}

I try to make connection via server name, user and pass.

like image 721
Димитър Грудев Avatar asked May 16 '26 17:05

Димитър Грудев


1 Answers

You need to format the string and then pass that string to the constructor (This is a C# 6 specific feature):

SqlConnection conn = new SqlConnection($"Data Source={server};User ID={user};Password={password};");

Or in older C# versions you can use string.Format:

SqlConnection conn = new SqlConnection(string.Format("Data Source={0};User ID={1};Password={2};", server, user, password));
like image 74
Romano Zumbé Avatar answered May 19 '26 06:05

Romano Zumbé



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!