root/Baza Reklam 2 - Faktury/LogoWReklamieForm.cs @ 21

Wersja 2, 4.9 KB (wprowadzona przez dorota, 17 years temu)
Line 
1using System;
2using System.Collections.Generic;
3using System.ComponentModel;
4using System.Data;
5using System.Data.Sql;
6using System.Data.SqlClient;
7using System.Drawing;
8using System.Text;
9using System.Windows.Forms;
10using System.IO;
11using Baza_Reklam.Classes;
12
13namespace Baza_Reklam
14{
15    public partial class LogoWReklamieForm : Form
16    {
17
18                #region Fields (1) 
19
20        private int customerId;
21        private int reklamaId;
22
23        Baza_Reklam.REKLAMADataSetTableAdapters.REKLAMATableAdapter reklamaAdapter = new Baza_Reklam.REKLAMADataSetTableAdapters.REKLAMATableAdapter();
24
25                #endregion Fields 
26
27                #region Constructors (1) 
28
29        public LogoWReklamieForm(int reklamaId)
30        {
31            InitializeComponent();
32
33            this.reklamaId = reklamaId;
34            this.customerId = Utils.customerId(reklamaId);
35           
36            this.kLIENCI_INTERNET_LOGOTableAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
37            this.reklamaAdapter.Connection.ConnectionString = ConnString.getConnString().Value;
38        }
39
40                #endregion Constructors 
41
42                #region Methods (5) 
43
44
45                // Private Methods (5) 
46
47        private bool CheckImageSize()
48        {
49            Image image = Image.FromFile(openFileDialog1.FileName);
50            if (image.Size.Height > 75 || image.Size.Width > 150)
51            {
52                MessageBox.Show("Logo ma nieodpowiednie wymiary (150 x 75)");
53                return false;
54            }
55
56             return true;
57        }
58
59        private void DodajLogoToolStripButton_Click(object sender, EventArgs e)
60        {
61            if (openFileDialog1.ShowDialog() == DialogResult.OK)
62            {
63
64                if (!CheckImageSize())
65                {
66                    return;
67                }
68
69                kLIENCI_INTERNET_LOGOTableAdapter.Insert(customerId,openFileDialog1.SafeFileName,"",ImageToBytes(openFileDialog1.FileName));
70
71                this.kLIENCI_INTERNET_LOGOTableAdapter.FillBy(this.bAZA_REKLAMDataSet.KLIENCI_INTERNET_LOGO, customerId);
72            }
73        }
74
75        private byte[] ImageToBytes(string  fileName)
76        {
77            FileInfo fInfo = new FileInfo(fileName);
78
79            long bytesNumber = fInfo.Length;
80
81            FileStream stream = new FileStream(fileName, FileMode.Open,FileAccess.Read);
82            BinaryReader reader = new BinaryReader(stream);
83
84            byte[] byteArray = reader.ReadBytes((int)bytesNumber);
85
86            reader.Close();
87            stream.Close();
88
89            return byteArray;
90        }
91
92        private void ImageFromDB(int idLoga)
93        {
94
95            Byte[] byteBLOBData = new Byte[0];
96
97            string sqlText = "SELECT logo FROM KLIENCI_INTERNET_LOGO WHERE id = " + idLoga;
98
99            SqlConnection connection = new SqlConnection(ConnString.getConnString().Value);
100            SqlCommand command = new SqlCommand(sqlText, connection);
101
102            connection.Open();
103            SqlDataReader dr = command.ExecuteReader();
104
105            if (dr.Read())
106            {
107                byteBLOBData = (Byte[])dr.GetValue(0);
108            }
109
110            connection.Close();
111            MemoryStream stmBLOBData = new MemoryStream(byteBLOBData);
112            pictureBox1.Image = Image.FromStream(stmBLOBData);
113           
114            stmBLOBData.Close();   
115         
116        }
117
118        private void LogaKlientowForm_Load(object sender, EventArgs e)
119        {
120            this.kLIENCI_INTERNET_LOGOTableAdapter.FillBy(this.bAZA_REKLAMDataSet.KLIENCI_INTERNET_LOGO,customerId);
121
122            //int id = Utils.logoId(reklamaId);
123
124
125            REKLAMADataSet.REKLAMARow row = reklamaAdapter.GetDataByReklamaId(reklamaId)[0];
126
127
128            if (row.logo != false)
129            {
130                ImageFromDB(row.logoID);
131            }
132           
133        }
134
135
136                #endregion Methods 
137
138        private void button3_Click(object sender, EventArgs e)
139        {
140            this.Close();
141        }
142
143        private void button1_Click(object sender, EventArgs e)
144        {
145            if (kLIENCI_INTERNET_LOGOBindingSource.Current != null)
146            {
147                DataRowView logoRow = (DataRowView)kLIENCI_INTERNET_LOGOBindingSource.Current;
148
149                REKLAMADataSet.REKLAMARow row = reklamaAdapter.GetDataByReklamaId(reklamaId)[0];
150
151                row.logo = true;
152                row.logoID = Convert.ToInt32(logoRow["ID"]);
153
154                reklamaAdapter.Update(row);
155
156                ImageFromDB(row.logoID);
157            }
158        }
159
160        private void button2_Click(object sender, EventArgs e)
161        {
162
163         
164            REKLAMADataSet.REKLAMARow row = reklamaAdapter.GetDataByReklamaId(reklamaId)[0];
165
166            row.logo = false;
167           // row.logoID = DBNull.Value;
168
169            reklamaAdapter.Update(row);
170
171            pictureBox1.Image = null;
172        }
173
174
175    }
176}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.