Answer the question
In order to leave comments, you need to log in
How to set timer interval in xml config file?
Hello, there is a code in which there is a timer for executing an sql query every 20 seconds, you need to set the timer parameters in the xml configuration file, a complete newbie and would like to ask you for help with working in the config file.
Source:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace SQL_DB
{
public partial class Form1 : Form
{
private SqlConnection sqlConnection = null;
private SqlDataAdapter adapter = null;
private DataTable table = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["KeyDB"].ConnectionString);
sqlConnection.Open();
adapter = new SqlDataAdapter("SELECT * FROM Postavki", sqlConnection);
table = new DataTable();
adapter.Fill(table);
Timer t = new Timer();
t.Tick += T_Tick;
t.Interval = 20000;
t.Start();
dataGridView1.DataSource = table;
if (sqlConnection.State == ConnectionState.Open)
{
MessageBox.Show("Подключение установлено");
}
}
private void T_Tick(object sender, EventArgs e)
{
button3.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand(
$"INSERT INTO [Postavki] (Name_Of_Product, Price, CapacityCBM, Provider, ProviderAddress, ProviderPhone, ProviderEmail, Date_Of_Delivery, Quantity, Number_Of_Contract) VALUES (@Name_Of_Product, @Price, @CapacityCBM, @Provider, @ProviderAddress, @ProviderPhone, @ProviderEmail, @Date_Of_Delivery, @Quantity, @Number_Of_Contract)",
sqlConnection);
DateTime date = DateTime.Parse(textBox8.Text);
command.Parameters.AddWithValue("Name_Of_Product", textBox1.Text);
command.Parameters.AddWithValue("Price", textBox2.Text);
command.Parameters.AddWithValue("CapacityCBM", textBox3.Text);
command.Parameters.AddWithValue("Provider", textBox4.Text);
command.Parameters.AddWithValue("ProviderAddress", textBox5.Text);
command.Parameters.AddWithValue("ProviderPhone", textBox6.Text);
command.Parameters.AddWithValue("ProviderEmail", textBox7.Text);
command.Parameters.AddWithValue("Date_Of_Delivery", $"{date.Month}/{date.Day}/{date.Year}");
command.Parameters.AddWithValue("Quantity", textBox9.Text);
command.Parameters.AddWithValue("Number_Of_Contract", textBox10.Text);
MessageBox.Show(command.ExecuteNonQuery().ToString());
}
private void button2_Click(object sender, EventArgs e)
{
table.Clear();
adapter.Fill(table);
dataGridView1.DataSource = table;
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button3_Click_1(object sender, EventArgs e)
{
SqlCommand command = new SqlCommand(
$"INSERT INTO [Postavki] (Name_Of_Product, Price, CapacityCBM, Provider, ProviderAddress, ProviderPhone, ProviderEmail, Date_Of_Delivery, Quantity, Number_Of_Contract) VALUES ((SELECT TOP 1 Name_Of_Product FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Price FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 CapacityCBM FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Provider FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderAddress FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderPhone FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderEmail FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Date_Of_Delivery FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Quantity FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Number_Of_Contract FROM Postavki ORDER BY NEWID()))", sqlConnection);
command.ExecuteNonQuery();
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question