Pkira
5/22/2015 - 9:03 AM

SQL Server

SQL Server

using (SqlConnection connection1 = new SqlConnection(ConnectionString))
{
    connection1.Open();
    SqlCommand cmd = null;

    var query = "UPDATE Forum SET LastExecutionDate = @LastExecutionDate, ExecutionDuration = @ExecutionDuration, LastPostDate = @LastPostDate Where ForumId = @ForumId";
    cmd = new SqlCommand(query.ToString(), connection1);
    cmd.Parameters.AddWithValue("@LastExecutionDate", Forum.LastExecutionDate);
    cmd.Parameters.AddWithValue("@ExecutionDuration", Forum.ExecutionDuration);

    if (Forum.LastPostDate == DateTime.MinValue)
        cmd.Parameters.AddWithValue("@LastPostDate", DBNull.Value);
    else
        cmd.Parameters.AddWithValue("@LastPostDate", Forum.LastPostDate);

    cmd.Parameters.AddWithValue("@ForumId", Forum.ForumId);

    var result = cmd.ExecuteNonQuery();

    if (result <= 0)
    {
        MailAndLog.SendMailAndLog("[Error] Trying to save forum execution time. ForumId: " + Forum.ForumId + " | MessageError: ", "WiseForumsCrawler Error ", ConfigurationManager.AppSettings["AlertEmail"], MessageLogtype.Error, MailAndLog.SendOptions.MailAndLog);               
        return false;
    }                     
    else
        return true;

}
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
    connection.Open();

    SqlCommand cmd = null;

    var query = "Select * From Topic (NOLOCK) order by Name";

    cmd = new SqlCommand(query.ToString(), connection);

    SqlDataReader reader = cmd.ExecuteReader();
    while (reader.Read())
    {
        long topic_id = 0;
        long.TryParse(reader["TopicId"].ToString(), out topic_id);
        if (topic_id != 0)
            new_topic.TopicId = topic_id;

        new_topic.Name = reader["Name"].ToString();
        
        DateTime aux_date;
        if (DateTime.TryParse(reader["StartDate"].ToString(), out aux_date))
        {
            new_server.StartDate = aux_date;
        }
        
    }
}
using (SqlConnection connection = new SqlConnection(ConnectionString))
{
  connection.Open();

  SqlCommand cmd = null;

  var query = "CountbyForum";

  cmd = new SqlCommand(query.ToString(), connection);
  cmd.CommandType = System.Data.CommandType.StoredProcedure;

  cmd.Parameters.AddWithValue("@ForumId", frm.ForumId);

  SqlDataReader reader = cmd.ExecuteReader();
  while (reader.Read())
  {
  
      frm.ForumName = reader["ForumName"].ToString();

      int nr = 0;
      Int32.TryParse(reader["NrThreads"].ToString(), out nr);
      if (nr != 0)
      {
          frm.NrThreads = nr;
      }
  
  }

}
//Manual

SqlConnection consqlserver = new SqlConnection("Server=server;Database=database;User Id=user;Password=pass;");
consqlserver.Open();

// by APP
private static string ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["WiseForums"].ConnectionString;
//insert on related table
using (SqlConnection connection1 = new SqlConnection(ConnectionStringArticles))
{
    connection1.Open();
    SqlCommand cmd = null;

    var query = "INSERT INTO [RelatedIdsTV] ([Ida],[id],[DTInsert],[score]) VALUES (@Ida, @id, @DTInsert, @score)";
    cmd = new SqlCommand(query.ToString(), connection1);
    cmd.Parameters.AddWithValue("@Ida", id);
    cmd.Parameters.AddWithValue("@id", ida_result);
    cmd.Parameters.AddWithValue("@DTInsert", DateTime.Now);
    cmd.Parameters.AddWithValue("@score", score_result);

    cmd.ExecuteNonQuery();
    connection1.Close();

}