Dev/C#2012. 11. 22. 11:46

SQL Server 의 float 형식으로 저장 된 것을 C# 에서 SqlDatareader 를 이용해서 데이터를 가져오는데 문제 발생.
float 형식이니 SqlDataReader reader 의 reader.getFloat(0); 형식으로 가져오기를 했으나 exception 발생.

이유는 SQL Server 의 float 은 C#의 double 형식임.

double doubleData = reader.getDouble(0) 으로 가져오거나
float floatData = (float)reader.getDouble(0) 으로 가져오면 된다!

관련 url : http://msdn.microsoft.com/en-us/library/ms131092.aspx

Posted by 놀란
Dev/C#2012. 10. 18. 12:29

아래 코드 참조.
지메일 셋팅 하고, 파일 첨부해서 '비동기' 방식으로 보내기.
(주석처리된 send 는 동기 방식)
한글을 사용하기 위해 제목(subject), 내용(body) 의 인코딩을 UTF8로 셋팅해준다.

  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Net.Mail;  
  4.   
  5. namespace EmailTestApplication  
  6. {  
  7.     public partial class Form1 : Form  
  8.     {  
  9.         public Form1()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         private void button1_Click(object sender, EventArgs e)  
  15.         {  
  16.             try  
  17.             {  
  18.                 MailMessage mail = new MailMessage();  
  19.                 SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");  
  20.   
  21.                 //set the address  
  22.                 mail.From = new MailAddress("your g-mail address");  
  23.                 mail.To.Add("target e-mail address");  
  24.   
  25.                 //set the contents  
  26.                 mail.Subject = "Subject Test Mail";  
  27.                 mail.Body = "This is for testing SMTP mail from GMAIL";  
  28.                 mail.BodyEncoding = System.Text.Encoding.UTF8;  
  29.                 mail.SubjectEncoding = System.Text.Encoding.UTF8;  
  30.   
  31.                 //set attachment  
  32.                 System.Net.Mail.Attachment attachment;  
  33.                 attachment = new System.Net.Mail.Attachment("D:\\temp\\file.txt");  
  34.                   
  35.                 mail.Attachments.Add(attachment);  
  36.   
  37.                 //setting smtpServer  
  38.                 SmtpServer.Port = 587;  
  39.                 SmtpServer.Credentials = new System.Net.NetworkCredential("your_ID""your_password");  
  40.                 SmtpServer.EnableSsl = true;  
  41.   
  42.                 //SmtpServer.Send(mail);  
  43.                 // MessageBox.Show("mail Send");  
  44.   
  45.                 //async send mail  
  46.                 object userState = mail;  
  47.                 SmtpServer.SendCompleted += new SendCompletedEventHandler(client_SendCompleted);  
  48.                 SmtpServer.SendAsync(mail, userState);  
  49.                   
  50.             }  
  51.             catch (Exception ex)  
  52.             {  
  53.                 MessageBox.Show(ex.ToString());  
  54.             }  
  55.   
  56.         }  
  57.   
  58.           
  59.         void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)   
  60.         {  
  61.             //Get the Original MailMessage object  
  62.             MailMessage mail = (MailMessage)e.UserState;  
  63.   
  64.             //write out the subject  
  65.             string subject = mail.Subject;  
  66.   
  67.             if (e.Error == null)  
  68.             {  
  69.                 // Send succeeded   
  70.                 string message = string.Format("Send Mail with subject [{0}]", subject);  
  71.                 MessageBox.Show(message);  
  72.             }  
  73.             else if (e.Cancelled == true)  
  74.             {  
  75.                 string message = string.Format("Send canceld for mail with subject [{0}]", subject);  
  76.                 MessageBox.Show(message);  
  77.             }  
  78.             else  
  79.             {  
  80.                 // log exception   
  81.                 string message = string.Format("Send Mail Fail - Error: [{0}]", e.Error.ToString());  
  82.                 MessageBox.Show(message);  
  83.             }  
  84.   
  85.         }  
  86.   
  87.     }  
  88. }  

Posted by 놀란
Dev/C#2012. 10. 17. 12:13

솔루션, 프로젝트 파일을 공유하여 작업을 할 때, 각자의 셋팅 환경이 필요한 경우가 있었다.
이 때, 각자의 셋팅을 유지하기 위해서 각자 계정으로 분류하길 원했다.

사용한 메소드는

  1. string currentUser =   
  2.      System.Security.Principal.WindowsIdentity.GetCurrent().Name;  
  3.   
  4. if (currentUser.Contains("hyukjoon"))  
  5.   
  6.     //TO DO  

출처 : http://msdn.microsoft.com/en-us/library/sfs49sw0.aspx

Posted by 놀란
Dev/C#2012. 10. 17. 12:00

String.Contains 메소드 사용

  1. // This example demonstrates the String.Contains() method   
  2. using System;  
  3.   
  4. class Sample   
  5. {  
  6.     public static void Main()   
  7.     {  
  8.     string s1 = "The quick brown fox jumps over the lazy dog";  
  9.     string s2 = "fox";  
  10.     bool b;  
  11.     b = s1.Contains(s2);  
  12.     Console.WriteLine("Is the string, s2, in the string, s1?: {0}", b);  
  13.     }  
  14. }  
  15. /* 
  16. This example produces the following results: 
  17.  
  18. Is the string, s2, in the string, s1?: True 
  19. */  

Posted by 놀란
Dev/C#2012. 6. 19. 18:35

수 계산 중에 올림 값 표현을 위해서 Math.Ceiling 메소드를 사용하게 되었다.

그런데, 부동소수점 으로 인해서 원치 않는 올림이 되는 현상이 발생했다.

ex )

int value = 50;
double percent = 1.1d;
double result = Math.Ceiling(value * percent) ;

50 * 1.1 = 55 이고 올림을 해도 55가 나올 것으로 생각했으나
56 이 나오게 되었다. 알고 보니 부동소수점 밑으로 55.0000000000000007 뭐 이런 식으로 원치 않는 값이 들어와 있었다.

이래 저래 알아본 결과 System.Decimal.Ceiling 을 사용하게 되었다.
c# 에서 제공하는 통화를 위해 제공되고 있는 메소드 이다.

참고 : http://msdn.microsoft.com/ko-kr/library/364x0z75.aspx

 

Posted by 놀란
Dev/C#2012. 3. 23. 21:38

정적 메소드(Static Method)는 클래스의 인스턴스를 생성하지 않아도 호출이 가능한 메소드를 말한다. 정적 메소드는 static 키워드를 이용해서 선언한다.


  1. class Myclass  
  2. {  
  3.     public static void StaticMethod()  
  4.     {  
  5.           //.....  
  6.     }  
  7. }  
  8.   
  9. //.....  
  10.   
  11. Myclass.StaticMethod();  //인스턴스를 만들지 않고도 바로 호출 가능  


인스턴스 메소드(Instance Method)는 이름처럼 클래스의 인스턴스를 생성해야만 호출할 수 있는 메소드를 말한다. 

  1. class Myclass  
  2. {  
  3.     public void StaticMethod()  
  4.     {  
  5.           //.....  
  6.     }  
  7. }  
  8.   
  9. //.....  
  10.   
  11. Myclass obj = new Myclass();  
  12. obj.StaticMethod();  


Posted by 놀란