Advertisement
kijato

Dátum alapján összegző (MSSQL)

Jul 17th, 2020
1,290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.95 KB | None | 0 0
  1. // https://www.mssqltips.com/sqlservertip/5771/querying-sql-server-tables-from-net/
  2.  
  3. using System;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8.  
  9. /*
  10.  
  11. Form-ra kell 1-1 : button1, textBox1, textBox2, label1
  12.      
  13. */
  14.  
  15. namespace WindowsFormsApp1
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         SqlConnection con;
  20.         String conString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Temp\tesztMSSQLdatabase.mdf;Integrated Security=True;Connect Timeout=30";
  21.  
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.             textBox1.Multiline = true;
  26.             textBox2.Text = DateTime.Today.ToString("yyyy.MM.dd");
  27.         }
  28.  
  29.         private void button1_Click(object sender, EventArgs e)
  30.         {
  31.             textBox1.Clear();
  32.             label1.Text = "";
  33.  
  34.             using (SqlConnection connection = new SqlConnection(conString))
  35.             {
  36.                 string queryString = "SELECT datum, osszeg FROM  [dbo].[Kurucz] WHERE datum = @datum";
  37.                 SqlCommand command = new SqlCommand(queryString, connection);
  38.                 command.Parameters.AddWithValue("@datum", textBox2.Text);
  39.                 connection.Open();
  40.                 SqlDataReader reader = command.ExecuteReader();
  41.                 try
  42.                 {
  43.                     while (reader.Read())
  44.                     {
  45.                         textBox1.AppendText(reader["datum"] + "\t" + reader["osszeg"] + "\n");
  46.                     }
  47.                     reader.Close();
  48.                     queryString = "SELECT sum(osszeg) osszesen FROM  [dbo].[Kurucz] WHERE datum = @datum";
  49.                     command = new SqlCommand(queryString, connection);
  50.                     command.Parameters.AddWithValue("@datum", textBox2.Text);
  51.                     label1.Text = command.ExecuteScalar().ToString();
  52.  
  53.                 }
  54.                 finally
  55.                 {
  56.                     reader.Close();
  57.                 }
  58.             }
  59.         }
  60.  
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement