Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
i want to use sql in visual studio 2012 , for exp : in homecontroller.cs

or make sqlclass which will be called globally when default page is called..

i m using MVC .

how to do this
Posted

Hope it helps:

C#
public ActionResult Index()
        {
        	string connectionString = "Data Source=Whatever; Initial Catalog=Northwind; Integrated security=True;";
			// your "normal" sql here
			string commandText = "select" +
									" _c.CompanyName" +
								" from Customers _c" +
								" inner join Orders _o on _o.CustomerID = _c.CustomerID" +
								" Where _o.ShipCity = @city";
			List<string> result = new List<string>();
			using (SqlConnection connection = new SqlConnection(connectionString))
			{
				SqlCommand cmd = new SqlCommand(commandText, connection);
				cmd.Parameters.Add("@city", SqlDbType.NVarChar);
				cmd.Parameters["@city"].Value = "Madrid";

				connection.Open();
				using (SqlDataReader reader = cmd.ExecuteReader())
				{
					while (reader.Read())
					{
						result.Add(reader.GetString(0));
					}
				}
			}

        	return View(result);
        }
</string></string>
 
Share this answer
 
1.create connection string & all.
2.Create Action Result
3.Route or map path View with Action result
 
Share this answer
 
Comments
maulikshah1990 10-Apr-14 8:45am    
can u give example code ...................i tried to find but not found...........
Mukesh Ghosh 10-Apr-14 9:01am    
http://www.codeproject.com/Articles/25057/Simple-Example-of-MVC-Model-View-Controller-Design

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900