Goran Bibic

Goran Bibic

  • 480
  • 2.9k
  • 195.8k

Generate year into connection string C#

Dec 15 2022 12:14 PM

I need to put year in database name connection string

Need to be

const string strCon2 = "Data Source=.\\SQLEXPRESS;Initial Catalog=BSS 2022;Integrated Security=True";

I try to solve initial catalog

string year = DateTime.Parse(DateTime.Now.ToString()).Year.ToString();
const string naziv = "BSS ";
string konekcija = string.Concat(naziv, DateTime.Parse(DateTime.Now.ToString()).Year.ToString());

And result

const string strCon2 = "Data Source=.\\SQLEXPRESS;Initial Catalog="+konekcija+";Integrated Security=True";

or

const string strCon2 = "Data Source=.\\SQLEXPRESS;Initial Catalog=BSS "+year+";Integrated Security=True";

But I have error

A field initializer cannot reference the non-static field


Answers (3)