Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://www.mssqltips.com/sqlservertip/5771/querying-sql-server-tables-from-net/
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using System.Data;
- using System.Data.SqlClient;
- /*
- Form-ra kell 1-1 : button1, textBox1, textBox2, label1
- */
- namespace WindowsFormsApp1
- {
- public partial class Form1 : Form
- {
- SqlConnection con;
- String conString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Temp\tesztMSSQLdatabase.mdf;Integrated Security=True;Connect Timeout=30";
- public Form1()
- {
- InitializeComponent();
- textBox1.Multiline = true;
- textBox2.Text = DateTime.Today.ToString("yyyy.MM.dd");
- }
- private void button1_Click(object sender, EventArgs e)
- {
- textBox1.Clear();
- label1.Text = "";
- using (SqlConnection connection = new SqlConnection(conString))
- {
- string queryString = "SELECT datum, osszeg FROM [dbo].[Kurucz] WHERE datum = @datum";
- SqlCommand command = new SqlCommand(queryString, connection);
- command.Parameters.AddWithValue("@datum", textBox2.Text);
- connection.Open();
- SqlDataReader reader = command.ExecuteReader();
- try
- {
- while (reader.Read())
- {
- textBox1.AppendText(reader["datum"] + "\t" + reader["osszeg"] + "\n");
- }
- reader.Close();
- queryString = "SELECT sum(osszeg) osszesen FROM [dbo].[Kurucz] WHERE datum = @datum";
- command = new SqlCommand(queryString, connection);
- command.Parameters.AddWithValue("@datum", textBox2.Text);
- label1.Text = command.ExecuteScalar().ToString();
- }
- finally
- {
- reader.Close();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement