i have an existing database and i want to attach that database one time to sql server and when thee database is attached then it open that database

You can use Visual studio's Server Explorer or Sql Server Management studio express (it is free) to attach/detach database file. Also use ADO.NET core API to connect it programatically.

if i dont want to use Sql Server Management studio express & Visual studio's Server Explorer and i just want to attach the database using c# code then what should i do??

Use SQL Management Objects (SMO)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.