Witam
Na wstępie z góry przepraszam za może nie czytelny kod. Mam do zrobienia programik który pobiera z bazy danych (mysql) dane i "wysyła" automatycznie co kilka sekund na drukarkę.
Automatyczny wydruk mam zrobiony oraz połączenie z baza (niestety w oddzielnych button`ach - nie potrafię tego połączyć wszystkiego razem - tak aby podczas automatycznego wygenerowania wydruku - w chwili obecnej dane są statyczne - pobierane były dane z bazy i drukowane)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Timers;
using System.IO;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private PrintDocument printDocument;
private PrintDialog printDialog;
int timeLeft;
DateTime date1 = DateTime.Now;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Data
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
// Zapis do ramki
textBox1.Text = "[" + date1 + "]" + " Uruchomienie programu... " + "\r\n";
// Zapis do pliku
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Etykiety.txt", true))
{
file.WriteLine("[" + date1 + "]" + " Uruchomienie programu... ");
}
// Data
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
textBox1.Text += "[" + date1 + "] " + "Drukowanie etykiety..." + "\r\n";
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Etykiety.txt", true))
{
file.WriteLine("[" + date1 + "] " + "Drukowanie etykiety...");
}
printDocument = new PrintDocument();
printDialog = new PrintDialog();
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);// wywoluje metode w ktorej opisane jest jak ma drukowac strony
printDialog.Document = printDocument; //do okienka ustawien trzeba wskazac jaki dokument mialby byc drukowany
printDocument.Print();
timeLeft = 50;
timer1.Start();
}
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
// string textToPrint = string.Format("Imie: {0}\nNazwisko: {1}\nRok urodzenia: {2}\nMiasto: {3}", textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text);
string textToPrint = string.Format("Imie: {0}\nNazwisko: {1}\nRok urodzenia: {2}\nMiasto: {3}", "1", "2", "3", "4");
using (Font font = new Font("Arial", 12))
{
// Drukowanie
g.DrawString(textToPrint, font, Brushes.Black, 10, 10);// dzieki obiektowi Graphics niejako malujemy nasz wydruk
}
}
private void button2_Click_1(object sender, EventArgs e)
{
// Zapis do ramki
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
textBox1.Text += "[" + date1 + "] " + "Wyjście z programu..." + "\r\n";
// zapis do pliku
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Etykiety.txt", true))
{
file.WriteLine("[" + date1 + "] " + "Wyjście z programu...");
}
Application.Exit();
}
// Timier ///////////////////////////////////////////////////////////////
private void timer1_Tick(object sender, EventArgs e)
{
if (timeLeft > 0)
{
timeLeft = timeLeft - 1;
button1.Text = timeLeft + " sek";
button1.Enabled = false;
}
else
{
timer1.Stop();
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
textBox1.Text += "[" + date1 + "] " + "Drukowanie etykiety..." + "\r\n";
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Etykiety.txt", true))
{
file.WriteLine("[" + date1 + "] " + "Drukowanie etykiety...");
}
printDocument = new PrintDocument();
printDialog = new PrintDialog();
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);// wywoluje metode w ktorej opisane jest jak ma drukowac strony
printDialog.Document = printDocument; //do okienka ustawien trzeba wskazac jaki dokument mialby byc drukowany
printDocument.Print();
timeLeft = 5;
timer1.Start();
}
}
private void button3_Click(object sender, EventArgs e)
{
string connectionString = "datasource=192.168.0.228;port=3306;username=...;password=...;database=kolektory;";
string query = "SELECT * FROM `etykiety` where (KntGid = 8801) OR (KntGid = 8212) OR (KntGid = 8237) OR (KntGid = 8236) and Status = 0 limit 1";
MySqlConnection databaseConnection = new MySqlConnection(connectionString);
MySqlCommand commandDatabase = new MySqlCommand(query, databaseConnection);
commandDatabase.CommandTimeout = 60;
MySqlDataReader reader;
try
{
databaseConnection.Open();
reader = commandDatabase.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
string[] row = { reader.GetString(0), reader.GetString(1), reader.GetString(2), reader.GetString(3) };
// Pokaz
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
textBox1.Text += reader.GetString(0) + " " + reader.GetString(1) + " " + reader.GetString(2) + " " + reader.GetString(3) + " " + "\r\n";
printDocument = new PrintDocument();
printDialog = new PrintDialog();
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);// wywoluje metode w ktorej opisane jest jak ma drukowac strony
printDialog.Document = printDocument; //do okienka ustawien trzeba wskazac jaki dokument mialby byc drukowany
printDocument.Print();
// Update
string query2 = "UPDATE `etykiety` SET `Status`='1' WHERE ID = " + reader.GetString(0) + "";
MySqlConnection databaseConnection2 = new MySqlConnection(connectionString);
MySqlCommand commandDatabase2 = new MySqlCommand(query2, databaseConnection2);
commandDatabase.CommandTimeout = 60;
MySqlDataReader reader2;
try
{
databaseConnection2.Open();
reader2 = commandDatabase2.ExecuteReader();
databaseConnection2.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
else
{
DateTime date1 = DateTime.Now;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
textBox1.Text += "[" + date1 + "] " + "Brak rekordów w bazie..." + "\r\n";
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Etykiety.txt", true))
{
file.WriteLine("[" + date1 + "] " + "Brak rekordów w bazie...");
}
}
databaseConnection.Close();
}
catch (Exception ex)
{
DateTime date1 = DateTime.Now;
this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
textBox1.Text += "[" + date1 + "] " + ex.Message + "\r\n";
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"Etykiety.txt", true))
{
file.WriteLine("[" + date1 + "] " + ex.Message);
}
}
}
}
}
Bardzo proszę o wskazówki i info jak mogę to zrobić.
Pozdrawiam