views:

108

answers:

4

I am a PHP programmer and I want to develop a website using visual web developer .net I knew that asp.net has many different approaches in dealing with database. However, I am familer with PHP way in programming for example:

$query = "select * from table where user_id > 2"
$result = mysql_query($query);

while($row = mysql_fetch_array($result)){

echo "username is: ".$row["username"]."<br>"; }

the question is "can I deal with asp.net in a PHP way?"

+4  A: 

Sure...learn C# (somewhat similar syntax) Or develop using one of the following:

Other .NET Powered Languages

My honest suggestion to you is that if you want to develop ASP.NET or .NET in general, you should learn C#. I did, and I am happy having done it.

Jonathan Sampson
+1 for Phalanger, interesting project.
roe
thanks for this useful information, I think I will try to learn C# because I like to adopt a mature technologies and these project you have listed here should be fine as a solution for PHP programmers however It seems that it is not well supported, thanks again
ahmed
+2  A: 

Maybe you should consider the way you are programming. Mixing logic, presentation and data access is not a good idea in any language. The example you showed is not "PHP way". Yes, it is possible to write like that in php but it's not a good practice. I think that by learning asp.net you may also learn a lot about programming and you really shouldn't try to program like that in asp.

empi
thanks for the good advise, I believe you are right however sometimes I need to get things done no matter which way I use - esp if I have to hand in a university assignment :), thanks
ahmed
+1  A: 

There are a lot of ways in asp.net. It's a little confusing, but they are all basically the same. The difference is the type of data access you use.

Using Enterprise Library (you need to download this) is probably one of the closest ways:

Database database = DatabaseFactory.CreateDatabase();
string sqlCommand = @"SELECT * FROM Users WHERE UserID=@UserID";
System.Data.Common.DbCommand dbCommand = database.GetSqlStringCommand(sqlCommand);

DataSet dataSetGroup = database.ExecuteDataSet(dbCommand);
DataTable dt = dataSetGroup.Tables[0];

foreach (DataRow dr in dt.Rows)
{
    Response.Write("User name is: " + dr["Username"].ToString() + "<br/>");
}

Enterprise Library is just a Microsoft library that simplifies data access. If you do not use it you can access the database directly but it takes a few more lines that handle the opening and closing of a data connection and you have to be more careful about opening and closing connections.

Other ways to do it are:

  1. Use strongly typed datasets
  2. Use LinqToSql or LinqToEntities and the ADO.net Framework

They are all different but similar in structure where you'll query the database and then do a foreach (you can also use while) to loop through your data.

I've been using LinqToSql recently and prefer that method. Scott Gu's blog has a good series of posts on how to use LinqToSql (linked to the last post so you can see all the previous ones): http://weblogs.asp.net/scottgu/archive/2007/09/07/linq-to-sql-part-9-using-a-custom-linq-expression-with-the-lt-asp-linqdatasource-gt-control.aspx

metanaito
thanks for this useful information, and for the blog
ahmed
+2  A: 

C# 3.0 offers very powerful syntax that allows you to interact with the database in a way that is both natural and safe (using parameterized queries). Your query in C# with LINQ2SQL would look like:

var query = from t in context.table
            where t.user_id > 2
            select t;

You'd probably use a Repeater in your view so you could just provide your query as the datasource (there are ways to do this in mark up, too) in your codebehind.

// use ToList so context can be disposed
nameRepeater.DataSource = query.ToList();

Your mark up would look like:

<asp:Repeater runat="server" ID="nameRepeater">
   <ItemTemplate>
   username is:
   <asp:Label runat="server" 
              ID="nameLabel"
              Text='<%= Bind("username") %>' />
   <br />
</asp:Repeater>

(lines split for readability)

Or you could use ASP.NET MVC in which case the selection code would be in your controller and you could use something more like your PHP-syntax in a strongly-typed view.

<% foreach (Table t in ViewData.Model) { %>
   username is: <%= t.username %><br/>
<% } %>
tvanfosson
thanks, LINQ2SQL and MVC both are great, I think that is what I am looking for, Thanks
ahmed