views:

287

answers:

6

I have an asp.net mvc application and i need to connecto to the DB i have saw a tutorial video that connect to DB using wizard by adding DB connection and determine the DB and add a model but i need to know if i can use connection string and query the DB or calling procedures in DB ???

I need any tutorials or step by step article that describe how to connect to DB without wizard and call procedures and query tables.

Thanks in advance and i am a begineer in MVC

+1  A: 

Yes you can (google ado.net for tutorials on ado.net), but it's not the MVC way. The MVC way is to use some sort of ORM (Object-relational mapping) such as NHibernate, Subsonic or Linq for SQL.

svinto
Using an ORM or some sort of middle ware isn't the MVC way, it's the lazy way (and I use them because I don't want to code up regular ado.net).
Min
U got it wrong what i need is to know where to put the connection string is it in the web.config like the past? and secondly how can i call a procedure from model and get the resulted data ? i don't know how to do that in mvc not how to access DB!!!u got my question wrong i have googled this and no explicit answer of where to put the code that accessing DB and how can i get the resulted data to be showed in view
Ahmy
A: 

You can put your connection string in the config, just like you're used to.

You can call procedures the same way you would from any .Net application too. @svinto's advice about using an ORM is another way of doing things and is well worth looking at too. Many of the good examples for ASP.Net MVC use the ORM techniques.

If you don't want to go down that route, you might benefit from creating a seperate class library project that you reference from your MVC application. Your Class Library project can act as your data access layer (DAL) where you handle db calls, etc...

Your Controllers can then call your DAL and processing methods to populate entities for the Views.

grenade
Thanks mr.grenade but after putting the connection string in the web.config how can i call my procedures from the model ? do i have to add a class that include method for calling procedures? if what i am thinking is right and i have created a class in the model folder so how can i let the controller call this class and get data and pass it to the view to be showed? really i am too puzzled and i hate MVC in the begining because of less help in this topic
Ahmy
If you want to enjoy the simpler side of life, you really would do well to follow the advice in @svinto's post.
grenade
No i need to know how to call procedure from model was my thoughts right in creating a class that includes methods call DB procedures? please i need explicit help not laughing my work broke down and i don't know how to resume hope that u understand what i need
Ahmy
No one's laughing at your work. If you want to call stored procedures from MVC just create some helper methods in your controller or in a seperate assembly that do exactly that. Create some models in your Models folder and populate them (either in your controller or in your helper assembly called from your controller).
grenade
thanks for ur attention and i didn't get definite solution for this problem i need to call procedure how can i do that ? and i put that in the controller so what will be the need of model ? i need an example or an application that do this point because i don't need discussion for the MVC i need a code can u help me please
Ahmy
A: 

Ahmy, I think the best advice to give would be before you start off developing using the MVC framework is to have a look into the principles behind the MVC pattern and domain driven design.

Specifically , have a look at repository patterns etc.

You can still add connection strings to your web.config in a block and access them as you would have done in a webforms project, after all Asp.net MVC is based on webforms. Its just likely that you wouldn't really want to do this if you're utilising MVC the way it was intended, its all about seperation of concerns.

www.asp.net has some great intro. examples worth watching.

Lewis
i have downloaded videos from there and watched it as i mentioned in my question but they are connecting to DB through wizard by creating DB and add table with records and query these records but what i need is that the DB is already exist and i need to call procedures and don't know how to do that in the model and how get the data from model in controller to put it in the appropriate view ??????thanks for ur attention hope that u will reply to me please because i got bored from MVC
Ahmy
More than likely any videos you watched there are using the Linq to SQL ORM tool, as mentioned above. If you have Visual Studio you should be able to do the same. Use Server Explorer to point to the DB and then use Linq to SQL to build your ORM model.
Lewis
... if the problem you're having is with the design of your Model and views etc then the problem you are having is not really a DB connectivity one and it's best to read up on the MVC pattern itself. I hope that helps.
Lewis
thanks for ur attention and i didn't get definite solution for this problem i need to call procedure how can i do that ? and i put that in the controller so what will be the need of model ? i need an example or an application that do this point because i don't need discussion for the MVC i need a code can u help me please
Ahmy
Have a look at : http://oxite.codeplex.com/ which is an MVC app. Your data access can happen in the same way as it did with a webforms app, it just isnt the right way to do it. I suggest buying this book:http://www.apress.com/book/view/1430210079
Lewis
Did they fix the oxite code? It's really not a good reference. Read http://codebetter.com/blogs/karlseguin/archive/2008/12/15/oxite-oh-dear-lord-why.aspx for starters.
andymeadows
+1  A: 

how to connect to DB without wizard and call procedures and query tables.

To call a procedure (here it will return no result, just perform some action):

using (SqlConnection connection = new SqlConnection(connectionString))
{
    connection .Open();
    using(SqlCommand command = connection.CreateCommand(nameofthestoredprocedure))
    {
      command.CommandType = CommandType.StoredProcedure;
      command.Parameters.AddWithValue("@someparameter", somevalue);
      maCommande.ExecuteNonQuery();
    }
}
Gregoire
Mr.Gregoie where to put this code(inspit i don't need this code) ????I need to know how will i integrate with model , view and controller ? do i need to creat a controller and put this code inside ? or ni have to put this code inside the model after creating a class inside it ????please helpe me and thanks for ur great attention
Ahmy
You have to put this code in a class in your dataaccess layer. Typically you will have: view<-->Controller<-->Repository<-->DataAccess<-->Storedproc<-->table, the view and controller exchanging a model, the dataacces, repository, controller exchanging some data entities
Gregoire
+1  A: 

I hesitate to respond to this, but EVERY video you have seen is likely using an OR/M to generate the Model and the DAL. The generated DAL will likely encapsulate your calls to the stored procedures that you're asking about.

The thing is -- and here's why you're not getting the answer you're looking for -- each OR/M is going to have a different method of retrieving data from and inserting data into the database. How you retrieve data from the DB using an OR/M is going to be different if you're using Entity Framework, Linq to SQL, SubSonic, NHibernate, or any other OR/M.

So, the question is to you. Are you using a OR/M? If so, which one? If not, then you will use the standard ADO.NET calls to retrieve and store data. This is also reflected in my comment to your original question.

andymeadows
A: 

Try downloading NerdDinner, or even better ... ContactManager iteration 1 (the tutorial directly answers your Q). Those should give you a good idea for how to handle database access. I wouldn't suggest looking at something like Oxite or MS StoreFront though, as these are a bit more complex.

To specifically answer your question: the connection string, like in ASP.NET, can go in your web.config or hard-coded in your application.

Jess