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
John IwaszPosted Dec 29, 2022, 6:21 PM
Using a placeholder ({0}) with string format gives you precise control.
Or you could use:
Vishal YelvePosted Dec 15, 2022, 3:05 PM
Declare static readonly year;
and use _year in your connection string.
Pankajkumar PatelPosted Dec 15, 2022, 12:49 PM
Hi Goran Bibic,
Hence, instead of const you can use readonly in following way:
Use it in another class:
For more details:
Hope this will help you!