简体   繁体   中英

Convert basic PHP/MySql to ASP to query MS Sql database

I have, what I would think, would be a very easy conversion....I have a database that has 1 table with 4 fields....I query the table to fill the webpage....I need to convert this PHP to .aspx I've looked and it seems that most of what I find is much more advanced than i need. I know NOTHING about asp. Thank you very much. Here is what I have:

<?php
include("config.php");

$result = mysql_query("SELECT * FROM TestMeas ORDER BY name ASC",$connect);
while($myrow = mysql_fetch_array($result))
         {
?>                          

<?php 

echo "<a href=\"".$myrow['url']."\" target=\"_new\">".$myrow['name']. "</a>";  ?>

<?php echo $myrow['desc'];
        }
?>

For starters, there is a big difference between ASP and ASP.net . In very simple terms, ASP is a scripting language whereas ASP.net is an application framework. Since ASP and PHP are both scripting languages, the usage is fairly similar. ASP.net is a whole different beast with almost no similarity.

Given the differences between working in a scripting language and working within the structure of the .Net framework, it's difficult to say "just do XYZ to convert your PHP to .Net". The .Net framework is largely built around separating presentation from content, which is not a focus in PHP unless you use a framework like CakePHP or CodeIgniter (or others).

Hopefully the linked tutorial in the comments got you on your way. There is also an MSDN article with a simple example of connecting to SQL Server.

Since this appears to be a "one-off" favor for a friend, your simplest option is probably to build a string in the code and pass it to your view.

string output = "";
using ( SqlConnection conn = new SqlConnection( connectionString ) )
{
    conn.Open();
    using( SqlCommand command = new SqlCommand( "SELECT url, name FROM TestMeas ORDER BY name ASC", 
                                                conn ) )
    {
        using( SqlDataReader reader = command.ExecuteReader() )
        {
            if( reader.HasRows )
            {
                while ( reader.Read() )
                {
                    output += String.Format( "<a href=\"{0}\" target=\"_new\">{1}</a>",
                                             reader[ 0 ],
                                             reader[ 1 ] );
                }
            }
            reader.Close();
        }
    }
    conn.Close();
}

// Do something with output

You could do this in a code-behind, you could do it in a Controller if you're using MVC, you could do it right in the form/view/page if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM