好吧,這應該添加一個新的hashtag到數據庫,如果它尚不存在,否則它應該增加計數器。SQL Server參數C#
但是,到目前爲止它所做的只是增加新的,即使它們是相同的。所以我有很多相同的主題標籤,全部都有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搜索被實現爲這樣
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;
}
}
HashTagSearch是如何實現的? – alexn
也許你需要的是'++ i' – V4Vendetta
變量'我'在其他地方修改?在提供的樣本中,它將始終具有相同的值。另外,在客戶端實現自動增量,這是非常非常糟糕的做法。 – Dennis