noclue123 noclue123 - 1 month ago 14
SQL Question

How to access a SQL database in visual studio? (Razor cshtml, C#)

I Already connected my SQL database with visual studio via server explorer And i filled some tables with values.

Now, I want to access/search through a table from my SQL database in my visual studio project.
For example: in my table i have products from different categories and i only want to display products from a specific category

But, how can in do that?


There is many ways by which you can access.If you are using connected mode Than you have to create object for connection string

 SqlConnection con = new SqlConnection(@"Data Source=nameof_server;Initial Catalog=name_of_database;Integrated Security=True");
SqlCommand cmd = new SqlCommand("sql_command",con);
//perform action based on your requirment
ExecuteNonQuery() // Use this for insert,update,delete

ExecuteScalar() // Use this for select single column

ExecuteReader() // Use this for select multiple records 

Please check this link