Hi, I am totally new to ASP.NET 2.0 using C# and MS SQL 2005. I am coming from a PHP MySQL background. I just started a new job and now I have to do everything in asp. I am having some issues with MSSQL. It just seem very confusing on how to do the database side of things. There just seems many ways of doing things with data readers and data sets and I am not sure what is the best way or the best way to start. Can someone tell me the best way and give me some complete working sample code in C# to get me started. I know this is pretty simple but a little time consuming. I have put up 250 pts.
This is the way I am used to doing things in php and mysql.
I am using a free php class ezsql
http://www.jvmultimedia.com/portal/node/6 but this is the jist of it.
// connect to database;
$db = new ezSQL_mysql('db_user','db_
password',
'db_name',
'db_host')
;
// get single record (set variable with result)
$userid = $db->get_var("SELECT userid FROM users WHERE userid = 1");
echo "$userid"; // print userid
// get row (return object)
$user = $db->get_row("SELECT * FROM users WHERE userid = 1");
echo "$user->username"; // print username
// get results (return object)
$users = $db->get_results("SELECT * FROM users");
// print out all users
foreach ($users as $myrow)
{
echo "$myrow->username<br />";
}
// execute and insert or update or delete
$sql= "UPDATE `schedule` SET `results` = '$result',`winner` = '$winner' WHERE `game_id` = $game_id LIMIT 1";
$db->query("$sql");
// Class variable that I used all the time as well
$db->num_rows Number of rows that were returned (by the database) for the last query (if any)
$db->insert_id -- ID generated from the AUTO_INCRIMENT of the previous INSERT operation (if any)
$db->rows_affected -- Number of rows affected (in the database) by the last INSERT, UPDATE or DELETE (if any)
So I need to know how to do all this in ASP.NET 2.0 using C# connecting to MS SQL
I would like full code on how to do this. If you have any good links I should read let me know also.
This is one example I got to work, I am not sure if this is a good way or not.
public void insertUser(string username, string email)
{
string con_string = "Data Source=.\\SQLEXPRESS;Attac
hDbFilenam
e=\"C:\\Pr
ogram Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Da
ta\\helpde
sk.mdf\";I
ntegrated Security=True;Connect Timeout=30;User Instance=True";
string sql = string.Format(
"INSERT INTO users (username, email) VALUES ('{0}', '{1}')",username, email);
SqlCommand cm = new SqlCommand(sql, new SqlConnection(con_string))
;
cm.Connection.Open();
cm.ExecuteNonQuery();
cm.Connection.Close();
}
I really appericate any help you can give.
Thanks Onestar