noclue123 noclue123 - 5 days ago 7
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?

Answer

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);
con.open()
//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 
con.close()

Please check this link

Comments