Quantcast
Channel: ASP.NET MVC
Viewing all articles
Browse latest Browse all 4

ASP.NET MVC

$
0
0

I am trying to create instances of my model to pass to my view.

I have a database full of images and their properties. I query the table to find the information, and would like to loop through the results to make a new model. Am I going about this correctly?

Thanks in advance for your help!

        public ActionResult FakePic()
        {
            string strAccessConn = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\website\ImageDatabase.accdb";
            string strQuery;
            OleDbConnection conn_OleDb = null;
            OleDbCommand cmd_OleDb;
            OleDbDataAdapter adapter_OleDB;
            DataSet myData;


            /* attempt to make a connection */
            try { conn_OleDb = new OleDbConnection(strAccessConn); }
            catch (Exception ex) {  }

            /* set the query */
            strQuery = @"
                SELECT
                    f.filePath,
                    c.category,
                    p.title,
                    p.fileName,
                    p.author
                FROM (fakepic p 
                    INNER JOIN folders f 
                        ON p.folderID = f.folderID)
                    INNER JOIN categories c ON p.catID = c.catID";

            /* create a data set to store the query results */
            myData = new DataSet();

            /* attempt to execute the query */
            try
            {
                // setup the command by providing a query and connection
                cmd_OleDb = new OleDbCommand(strQuery, conn_OleDb);

                // assign the command to a data adapter
                adapter_OleDB = new OleDbDataAdapter(cmd_OleDb);

                // open a connection to the database
                conn_OleDb.Open();

                // Adds rows from the dataset into the adapter, label table as fakepics
                adapter_OleDB.Fill(myData, "fakepics");
            }
            catch (Exception ex) { }
            finally { conn_OleDb.Close(); }

            // A dataset can contain multiple tables, so add them all into an array
            DataTableCollection dtc = myData.Tables;

            // Add the rows from fakepics into a collection
            DataRowCollection drc = myData.Tables["fakepics"].Rows;

            foreach (DataRow dr in drc)
            {
                // make a new var of MVCpractice.Models.FakePic()
                var fakepic = new[] {
                    new Models.FakePic()
                    {
                        filePath = "",
                        title = "",
                        fileName = "",
                        author = "",
                    }
                };
            }


-Nothing to see. Move along.


Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images