c\#데이터베이스 와 TXT 가 져 오기 내 보 내기 인 스 턴 스


     private void button1_Click(object sender, EventArgs e)        
     {            
     if (openFileDialog1.ShowDialog() == DialogResult.OK)  
     {         
     using (FileStream fs = File.OpenRead(openFileDialog1.FileName)) 
     {              
     using (StreamReader sr = new StreamReader(fs, System.Text.Encoding.GetEncoding("GB2312")))   
     {
     //<span style="color:#3333ff;"> System.Text.Encoding.GetEncoding("GB2312"),
     string name = arr[0] name </span>                           using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='|DataDirectory|\dd.mdf';
     Integrated Security=True;User Instance=True"))  
     {
     //<span style="color:#3333ff;">DataDirectory ,winForm Program.cs , .NET , why</span>                            
     conn.Open();                    
     using (SqlCommand cmd = conn.CreateCommand())      
     {                         
     cmd.CommandText = "insert into T_Persons values(@Name,@Age)";
     string line = "";      
     while ((line = sr.ReadLine()) != null)  
     {                
     string[] arr = line.Split('|');      
     string name = arr[0];           
     int age = Convert.ToInt32(arr[1]); 
     cmd.Parameters.Clear();//          
     cmd.Parameters.Add(new SqlParameter("Name", name)); 
     cmd.Parameters.Add(new SqlParameter("Age", age));  
     cmd.ExecuteNonQuery();       
     }                    
     }                    
     }                   
     }               
     }               
     MessageBox.Show("txt !");   
     }               
     }        
     private void button2_Click(object sender, EventArgs e)    
     {            
     if (saveFileDialog1.ShowDialog() == DialogResult.OK)     
     {                 
     using (FileStream fs = File.OpenWrite(saveFileDialog1.FileName)) 
     {                  
     using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding("GB2312")))    
     {                     
     using (SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename='|DataDirectory|\dd.mdf';Integrated Security=True;User Instance=True"))                         
     {                         
     conn.Open();       
     using (SqlCommand cmd = conn.CreateCommand())     
     {                             
     cmd.CommandText = "select * from T_Persons";   
     using (SqlDataReader sdr = cmd.ExecuteReader())    
     {                                            
     while (sdr.Read())              
     {                              
     string name = sdr.GetString(sdr.GetOrdinal("Name"));
     int age = sdr.GetInt32(sdr.GetOrdinal("Age"));  
     string line =name+"|"+age;                     
     sw.WriteLine(line);                          
     sw.Flush();                       
     }                                     
     }                      
     }                 
     }                  
     }             
     }          
     MessageBox.Show(" txt !");  
     }        
     }
     </span> 
이것 은 Program.cs 파일 에 추가 할 코드 입 니 다.winForm 과 win 콘 솔 에 만 유효 합 니 다.

static void Main()        
{          
string dataDir = AppDomain.CurrentDomain.BaseDirectory;      
if (dataDir.EndsWith(@"\bin\Debug\") || dataDir.EndsWith(@"\bin\Release\"))  
{                
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;  
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);    
}               
Application.EnableVisualStyles(); 
Application.SetCompatibleTextRenderingDefault(false);    
Application.Run(new Form1());    
}

좋은 웹페이지 즐겨찾기