好的,如果它不存在,这应该向数据库添加一个新的标签,否则它应该增加计数器。
然而,到目前为止,它所做的只是添加新的,即使它们是相同的。所以我有很多相同的主题标签,都是 1。有什么建议吗?
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();
HashTag Search 就是这样实现的
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;
}
}