Ok, cela devrait ajouter un nouveau hashtag à la base de données si elle n'existe pas déjà, sinon elle devrait incrémenter le compteur.Paramètres SQL Server C#
Cependant, tout ce qu'il fait jusqu'à présent, c'est d'en ajouter de nouveaux, même s'ils sont identiques. J'ai donc beaucoup de hashtags identiques, tous avec 1. Des suggestions?
HashTagReader r = new HashTagReader();
int i;
i=1;
if (r.HashTagSearch(s))
MessageBox.Show("I Found it!");
else
{
SqlCommand myCommand = new SqlCommand("INSERT INTO dbo.Table1 (HashTag, Counter) Values (@HashTag,@Counter)", connection);
myCommand.Parameters.Add("@HashTag", SqlDbType.VarChar, 50).Value = s; //Your hashTagvalue
myCommand.Parameters.Add("@Counter", SqlDbType.VarChar, 50).Value = i++; //Your Counter Value
myCommand.ExecuteNonQuery();
}
connection.Close();
La recherche HashTag est mis en œuvre en tant que telle
public bool HashTagSearch(string hashtagstring)
{
SqlConnection connection = new SqlConnection();
connection.ConnectionString = (@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Jordan Moffat\Desktop\coursework\WindowsFormsApplication1\WindowsFormsApplication1\HashTags.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
// SqlConnection connection = new SqlConnection();
// connection.ConnectionString = "C:/Users/Jordan Moffat/Desktop/coursework/WindowsFormsApplication1/WindowsFormsApplication1/HashTags.mdf"; //Your connection string
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "FindString";
command.Parameters.AddWithValue("@MyString", hashtagstring);
try
{
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
return true;
}
}
catch (Exception)
{
// MessageBox.Show("heel");
}
finally
{
if (connection.State == ConnectionState.Open)
connection.Close();
}
return false;
}
}
Comment HashTagSearch est-il implémenté? – alexn
Peut-être que ce dont vous avez besoin est '++ i' – V4Vendetta
Est-ce que la variable 'i' est modifiée ailleurs? Dans l'échantillon fourni, il aura toujours la même valeur. En outre, la mise en œuvre de l'auto-incrémentation du côté client est une très, très mauvaise pratique. – Dennis