As title really.
I want to object just one specific value from a colum on one row of a SQL database.
The PHP code I would use would be:
$result = mysql_query("SELECT price FROM delivery WHERE id='$id'");
echo mysql_result($result,0);
That would get me the price fr开发者_运维问答om what ever autoinc id I choose.
As I'm new to C# its quite tricky to get my head around as there seems to be many different methods to obtain the same results.
Any help would be appreciated, thanks!
EDIT
I'm using System.Data.SqlClient as, so far, its done me proud.
If data type of PRICE column you want to retrieve is a scalar value and you'll get only one record then I assume the most efficient way would be ExecuteScalar() method. Here's a little sample code:
string conString="Data Source=local;Initial Catalog=...."
SQLConnection con=new SQLConnection(conString);
SQLCommand cmd=new SQLCOmmand("SELECT price FROM delivery WHERE =@ID",con)
cmd.Parameters.Add("@ID", SqlDbType.Int).Value=125//Off course it just a sample value
con.Open();
float price=(decimal)cmd.ExecuteScalar();
......
con.Close();//don't forget
for example with ADO.NET.
string strSQL = "SELECT COUNT(*) FROM Products WHERE CategoryID=1";
SqlCommand cmd = new SqlCommand(strSQL, con);
int nr = Convert.ToInt32(cmd.ExecuteScalar());
you could always use the TOP function in SQL to retun a single result:
$result = mysql_query("SELECT TOP (1) price FROM delivery WHERE id='$id'");
I am not sure if this will help you to do what you are looking to do, but I hope it helps.
精彩评论