Click here to Skip to main content
15,904,934 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello everybody!,

I recently decided to start program in another language than PHP.
I have read up on SQLClient but can't rly figure it out properly.
I was hopeing that someone here had a grasp of both languages and could help me understand how to work with a sql server in c#.

I don't think i need much to get a grasp on how to use it, if someone could help me translate the following code into C# and mssql i would have enough to keep working with databases only this time in C#.

PHP
$result = mysql_query("SELECT * FROM mytable");
while($row = mysql_fetch_array($result))
{
   echo $row["Name"].", Room ".$row["Room"].", Tel ".$row["Telephone"] ;
}


Thank you for reading my question, and thanks in advance for every reply that this post gets.

Regards.
Leon
Posted
Updated 22-May-13 8:53am
v2
Comments
Sergey Alexandrovich Kryukov 22-May-13 14:56pm    
There is no such thing as "translation" form PHP to C#. The concepts are different. You should write the code. Do you see the difference? If you need help, you should rather explain what exactly do you want to achieve...
—SA
[no name] 22-May-13 15:08pm    
What i want it to do is the following:

1. Query the sql database
2. Translate the query into an array and loop it
3. For each row in the sql array (loop) i want to grab values from that row and add it into the code.



Sergey Alexandrovich Kryukov 22-May-13 15:12pm    
OK, I answered, please see.
—SA

Please see my comment to the question.

Short answer: you need to use ADO.NET. This is a good introductory article which can help you to get started immediately: Using ADO.NET for beginners[^].

—SA
 
Share this answer
 
Comments
[no name] 22-May-13 15:17pm    
This was perfect!
Thank you :)
Sergey Alexandrovich Kryukov 22-May-13 16:13pm    
You are welcome. Say thank you for the author of the article which helped many beginners.
—SA
I don't mean this to sound offensive, but have you bothered to do much research?
There is a huge wealth of information, on this site alone, let alone on the far reaches of the internet.

Google is your best friend for learning some of the basics as is the Microsoft website.

But to help you out a bit I am providing this link:

http://msdn.microsoft.com/en-us/library/haa3afyz(v=vs.71).aspx[^] (note the link is in vb.net because it is set to framework 1.1, change the framework at the top of the page to see c# examples)

This talks about an SQLDataReader which appears to be similar to the "Mysql_Fetch_array" in PHP. There is a detailed example of how to use it, and along with that there are links to other SQLClient components such as connections/commands which you will want to read up on.

I hope this helps you with your transition.

-------------

Additional advice:

People on this site are more likely to give detail help when you have tried stuff yourself. If you do some research, knock up some code and then come back with specific errors/ sections where you are stuck the advice provided on this site by people (I find) is invaluable. But people are generally not prepared to write whole solutions for people when it looks like they aren't trying themselves.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 22-May-13 15:14pm    
I basically agree, but at least you could post somewhat newer link. I don't think anything earlier at least v.2.0 is actual or can be considered... I voted 4.
—SA
Pheonyx 22-May-13 15:19pm    
Wasn't intentional, I forgot to change the .net framework before copying the link was all. :) thanks for the vote.
[no name] 22-May-13 15:26pm    
Same here as Sergey's solution, the ADO.net DataReader was the piece that i was missing.
But now that i know it was that part i was missing i feel rly stupid, did a re-search with the reader added to c# mssql and got the same pages that you sent me. Thanks alot, and i did do research it was just that i was stuck on the sqlcommand part and couldn't properly get it to a workable array. I do hope i wasnt too annoying. /Leon

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