Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am new baby in mvc3 application.

How can we access database without using LINQ in mvc3 application in asp.net.
I have to bind a gridview.Now I have written the code in Controller like below:

public ActionResult Index()
{
string strcon = ConfigurationManager.ConnectionStrings["conStr"].ToString();
SqlConnection con = new SqlConnection(strcon);
con.Open();

SqlCommand cmd = new SqlCommand("SP_Dis", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);

return View(dt);
}
Is this Possible?

Now I have to bind the dt to a gridview.How can I do it?
Posted
Updated 17-Feb-12 0:49am
v2

1 solution

Use enterprise library.

Download Enterprise Library from here[^]

Add something like this into your web.config

XML
<configSections>
  <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
</configSections>
<dataConfiguration defaultDatabase="Connection String" />
<connectionStrings>
  
  <add name="yourDatabase" connectionString="Database=yourDatabase;Server=(local);Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>


Then access the databse using code like this:

C#
public static User LogOn(string userCode, string password)
        {
            Database db = DatabaseFactory.CreateDatabase("eValue8DB");
            DbCommand dbCommand = db.GetStoredProcCommand("Get_User");
            db.AddInParameter(dbCommand, "UserCode", System.Data.DbType.String, userCode);
            db.AddInParameter(dbCommand, "Password", System.Data.DbType.String, password);

            using (IDataReader reader = db.ExecuteReader(dbCommand))
            {
                if (reader.Read())
                {
                    // create and return the user object from the datareader
                }
                else
                {
                    return null;
                }
            }
        }


P.S. you need to add a reference to the entrprise library in the project in which yo uhave this DB access code
 
Share this answer
 
v2

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