I have a server program that will store certain data sent by the client. One data is the client's hostname. The server will check if the hostname exist, if not then it will insert that new data. It should look like this.
hostname_id | hostname
------------------------
1 | Admin
2 | Guest_PC
3 | Bob_PC2
My problem is it won't store the newly inserted data. It keeps on returning zero but not storing anything. Here is my code.(Edited to correct version)
string constring = "Database=chtbuster;Data Source=localhost;User Id=root;Password=''";
string count1 = "SELECT COUNT(hostName) FROM chtbuster.hostnametable WHERE hostName=@machineName ";
using (MySqlConnection conDataBase = new MySqlConnection(constring))
{
MySqlCommand cmd1Database = new MySqlCommand(count1, conDataBase);
conDataBase.Open();
long count = (long)cmd1Database.ExecuteScalar();
if (count == 0)
{
string insert_ht = "INSERT INTO chtbuster.hostnametable(hostName) VALUES(@machineName);";
MySqlCommand cmd5Database = new MySqlCommand(insert_ht, conDataBase);
cmd5Database.Parameters.AddWithValue("@machineName", machineName);
cmd5Database.ExecuteNonQuery();
//*test* output.Text += "\n Empty " + count;
}
else
{
//not empty, insert other data
}
}
I have coded PHP database before and is new to C# database, I'm quite confused. Please help. Thank you.