3
Answers

Generate year into connection string C#

Goran Bibic

Goran Bibic

2y
921
1

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)