==========================================================================================

C#

==========================================================================================


<C#>



=MSSQL


http://itsmart333.tistory.com/attachment/cfile21.uf@25726F4A55937E1E20B15B.pdf



<MSSQL_WORK>


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.IO; //파일 클래스 사용
using System.Data.SqlClient;

namespace MSSQL_WORK
{
    
public partial class Form1 : Form
    {
        FileInfo f 
= null;// 파일 정보 검색
        //서버연결 문자열
        string Constr = "server=localhost;uid=sa;pwd=1234;database=mook";
        
public Form1()
        {
            InitializeComponent();
        }

        
private void btnFile_Click(object sender, EventArgs e)
        {
            
if(this.ofdFile.ShowDialog() == DialogResult.OK)
            {
                
try
                {
                    
this.txtFile.Text = this.ofdFile.FileName;
                    f 
= new FileInfo(this.ofdFile.FileName);
                    
this.lbldisName.Text = f.Name;
                    
this.lbldisSize.Text = f.Length.ToString() + " byte";
                    
this.lbldisDate.Text = f.LastWriteTime.ToString();
                }
                
catch
                {
                    
return;
                }
            }
        }

        
private void btnUpload_Click(object sender, EventArgs e)
        {
            var fs 
= f.OpenRead();
            var bytebuffer 
= new byte[fs.Length];
            fs.Read(bytebuffer, 
0, Convert.ToInt32(fs.Length));
            var conn 
= new SqlConnection(Constr);
            conn.Open();
            var cmd 
= conn.CreateCommand();
            cmd.CommandType 
= CommandType.Text;
            var Sql 
= "update Table_File set M_File=@M_File, M_FileName='" +
                
//this.ofdFile.FileName + "' where M_Num=1";
                this.lbldisName.Text + "' where M_Num=1";

            cmd.CommandText 
= Sql;
            cmd.Parameters.Add(
new SqlParameter("@M_File", System.Data.SqlDbType.Image)).Value = bytebuffer;
            var iResult 
= cmd.ExecuteNonQuery();
            conn.Close();
            fs.Close();
            
if((iResult > 0&& (DataSave() > 0))
            {
                MessageBox.Show(
"저장이 정상적으로 되었습니다.","알림",
                MessageBoxButtons.OK, MessageBoxIcon.Information);
                
this.Close();
            }
            
else
            {
                MessageBox.Show(
"저장이 되지 않았습니다.","에러",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        
private int DataSave()
        {
            var Conn 
= new SqlConnection(Constr);
            Conn.Open();
            var strSQL 
= "update Table_Update set M_Date=M_Date+1";
            var myCom 
= new SqlCommand(strSQL, Conn);
            var i 
= myCom.ExecuteNonQuery();
            Conn.Close();
            
return i;
        }


    }
}


<mook_AutoMain>


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.IO; // 파일 클래스 사용
using System.Data.SqlClient; //SqlConnection 클래스 사용
using System.Diagnostics; //Process 클래스 사용
namespace mook_AutoMain
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

        
private void Timer_Tick(object sender, EventArgs e)
        {
            
this.lbldisUpdate.Text = DateTime.Now.ToString();
            var srt 
= File.OpenText("setup.txt");
            var str 
= srt.ReadLine();
            
if(Convert.ToInt32(str) == Convert.ToInt32(DataCheck()))
            {
                srt.Close();
                
return;
            }
            
else if(Convert.ToInt32(str) < Convert.ToInt32(DataCheck()))
            {
                srt.Close();
                
this.Timer.Enabled = false;
                var dlr 
= MessageBox.Show("최신 프로그램을 다운로드 받으시겠습니까?",
                    
"알림", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                
if(dlr == System.Windows.Forms.DialogResult.Yes)
                {
                    var myproces 
= new Process();
                    myproces.StartInfo.FileName 
= "mook_AutoUpdate.exe";
                    myproces.Start();
                    Application.Exit();
                }
                
else
                {
                    
this.Timer.Enabled = true;
                    
return;
                }
            }
            
else
            {
                srt.Close();
                MessageBox.Show(
"잘못된 프로그램입니다.""알림",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
                
this.Close();
            }
        }

        
private string DataCheck()
        {
            var Constr 
= "server=localhost;uid=sa;pwd=1234;database=mook";
            var Conn 
= new SqlConnection(Constr);
            Conn.Open();
            var Comm 
= new SqlCommand("Select * from Table_Update", Conn);
            var myRead 
= Comm.ExecuteReader();
            
if(myRead.Read())
            {
                var str 
= myRead["M_Date"].ToString();
                myRead.Close();
                Conn.Close();
                
return str;
            }
            
else
            {
                myRead.Close();
                Conn.Close();
                
return "1";
            }
        }

        
private void cbTime_SelectedIndexChanged(object sender, EventArgs e)
        {
            string[] temp 
= cbTime.Text.Split(new Char[] { ' ' });
            Timer.Interval 
= Convert.ToInt32(temp[0]) * 1000;
        }

        
private void Form1_Load(object sender, EventArgs e)
        {
            cbTime.SelectedIndex 
= 0;
            Timer.Interval 
= 1000;
        }
    }
}


<mook_AutoUpdate>


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.Data.SqlClient;
using System.IO;
using System.Threading;
using System.Diagnostics;

namespace mook_AutoUpdate
{
    
public partial class Form1 : Form
    {
        
private Thread myDownload;
        
private string Constr = "server=localhost;uid=sa;pwd=1234;database=mook";
        
// This delegate enables asynchronous calls for setting
        // the text property on a TextBox control.
        delegate void SetProgCallback1( int totalLen, int Val);
        delegate 
void SetProgCallback2( int Val);
        delegate 
void SetProgCallback3();

        
public Form1()
        {
            InitializeComponent();
        }

        
private void Exit()
        {
            
// InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.InvokeRequired)
            {
                SetProgCallback3 d 
= new SetProgCallback3(Exit);
                
this.Invoke(d, new object[] {});
            }
            
else
            {
                
this
.Close();
            }
        }

        
private void DataDownLoading()
        {
            byte[] bytes 
= DatabaseDownload(1);
            var fs 
= new FileStream("mook_AutoMain.exe", FileMode.Create);
            fs.Write(bytes, 
0, bytes.Length);
            fs.Close();
            Exit();
            
//this.Close();
        }

        
private void SetMax(int totalLen, int Val)
        {
            
// InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.ProgressBar.InvokeRequired)
            {
                SetProgCallback1 d 
= new SetProgCallback1(SetMax);
                
this.Invoke(d, new object[] { totalLen, Val });
            }
            
else
            {
                
this.ProgressBar.Maximum = totalLen;
                
this.ProgressBar.Value =
 Val;
            }
        }


        
private void SetVal(int Val)
        {
            
// InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.ProgressBar.InvokeRequired)
            {
                SetProgCallback2 d 
= new SetProgCallback2(SetVal);
                
this.Invoke(d, new object[] { Val });
            }
            
else
            {
                
this.ProgressBar.Value =
 Val;
            }
        }


        
private byte[] DatabaseDownload(int M_Num)
        {
            
using(var Conn = new SqlConnection(Constr))
            {
                Conn.Open();
                var sql 
= "select DataLength(M_File) From Table_File where M_NUM=@M_NUM";
                SqlParameter param 
= new SqlParameter("@M_NUM", SqlDbType.Int);
                param.Value 
= M_Num;
                SqlCommand cmd 
= new SqlCommand(sql,Conn);
                cmd.Parameters.Add(param);
                
int totalLength = Convert.ToInt32(cmd.ExecuteScalar());
                
//this.ProgressBar.Maximum = totalLength;
                //this.ProgressBar.Value = 0;
                this.SetMax(totalLength, 0
);
                sql
= "select M_File From Table_File where M_Num=@M_Num";
                param 
= new SqlParameter("@M_Num", SqlDbType.Int);
                param.Value 
= M_Num;
                cmd 
= new SqlCommand(sql,Conn);
                cmd.Parameters.Add(param);
                SqlDataReader reader 
= cmd.ExecuteReader(CommandBehavior.SequentialAccess);
                reader.Read();
                
long remainder = totalLength;
                
int bufferSize = 2048;
                
if(totalLength < bufferSize)
                    totalLength 
= bufferSize;
                byte[] buf 
= new byte[(int)totalLength];
                
int startIndex = 0;
                
long retval = reader.GetBytes(0,startIndex, buf, 0, bufferSize);
                remainder -
= retval;
                
while(remainder >0)
                {
                    startIndex +
= bufferSize;
                    
if(remainder < bufferSize)
                        bufferSize 
= (int)remainder;
                    retval 
= reader.GetBytes(0, startIndex, buf, startIndex, bufferSize);
                    remainder -
=retval;
                    
//this.ProgressBar.Value = startIndex;
                    this
.SetVal(startIndex);
                    Thread.Sleep(
100);
                }
                
//this.ProgressBar.Value = totalLength;
                this
.SetVal(totalLength);
                reader.Close();
                
return buf;

            }
        }

        
private void Form1_Load(object sender, EventArgs e)
        {
            
try
            {
                var tProcess 
= Process.GetProcessesByName("mook_AutoMain");
                
if (tProcess.Length == 1)
                    tProcess[
0].Kill();
            }
            
catch { }
            var fs 
= new FileStream("setup.txt", FileMode.Create);
            var sw 
= new StreamWriter(fs);
            sw.WriteLine(DataCheck());
            sw.Close();
            myDownload 
= new Thread(DataDownLoading);
            myDownload.Start();
        }

        
private string DataCheck()
        {
            var Conn 
= new SqlConnection(Constr);
            Conn.Open();
            var Comm 
= new SqlCommand("select * from Table_Update", Conn);
            var myRead 
= Comm.ExecuteReader();
            
if(myRead.Read())
            {
                string Update 
= myRead["M_Date"].ToString();
                myRead.Close();
                Conn.Close();
                
return Update;
            }
            
else
            {
                myRead.Close();
                Conn.Close();
                
return "1";
            }
        }

        
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            
if(this!=null)
            {
                myDownload.Abort();
                var myProcess 
= new Process();
                myProcess.StartInfo.FileName 
= "mook_AutoMain.exe";
                myProcess.Start();
            }
        }
    }
}



=테스트






=초기값 변경










=버전 1 파일 업로드

















=> 테스트를 위해 M_Date => 1로 셋팅




=자동업데이트 버전 1 실행




=자동업데이트 버전 2파일을 서버에 업로드










=자동업데이트 버전 1에서 업데이트 감지











=자동업데이트 버전 2자동 실행






=학생클래스를 

1. 데이타테이블로 => 그리드에 뿌리기

2. 링크 => 그리드에 뿌리기


'2015 스마트 콘트롤러 > 업무일지' 카테고리의 다른 글

20150706  (0) 2015.07.07
20150703  (0) 2015.07.03
20150630  (0) 2015.06.30
2015029  (0) 2015.06.30
20150626  (0) 2015.06.27
Posted by ahj333
,