Advertisement
Guest User

Untitled

a guest
Sep 20th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.38 KB | None | 0 0
  1. public static int Main()
  2. {
  3. try
  4. {
  5. // Setup session options
  6. SessionOptions sessionOptions = new SessionOptions
  7. {
  8. Protocol = Protocol.Scp,
  9. HostName = hostName,
  10. UserName = userName,
  11. Password = passWord,
  12. SshHostKeyFingerprint = sshHostKey
  13. };
  14.  
  15. using (Session session = new Session())
  16. {
  17. // Will continuously report progress of synchronization
  18. session.FileTransferred += FileTransferred;
  19.  
  20. // Connect
  21. session.Open(sessionOptions);
  22.  
  23. // Synchronize files
  24. SynchronizationResult synchronizationResult;
  25. synchronizationResult =
  26. session.SynchronizeDirectories(
  27. SynchronizationMode.Local, localPath, remotePath, false);
  28.  
  29. // Throw on any error
  30. synchronizationResult.Check();
  31.  
  32. Run();
  33. }
  34. return 0;
  35. }
  36. catch (Exception e)
  37. {
  38. Console.WriteLine("Error: {0}", e);
  39. Console.ReadLine();
  40. return 1;
  41. }
  42. }
  43.  
  44. private static void FileTransferred(object sender, TransferEventArgs e)
  45. {
  46. if (e.Error == null)
  47. {
  48. Console.WriteLine("Upload of {0} from remote to local server succeeded", e.FileName);
  49. }
  50. else
  51. {
  52. Console.WriteLine("Upload of {0} from remote to local server failed: {1}", e.FileName, e.Error);
  53. }
  54.  
  55. if (e.Chmod != null)
  56. {
  57. if (e.Chmod.Error == null)
  58. {
  59. Console.WriteLine("Permisions of {0} set to {1}", e.Chmod.FileName, e.Chmod.FilePermissions);
  60. }
  61. else
  62. {
  63. Console.WriteLine("Setting permissions of {0} failed: {1}", e.Chmod.FileName, e.Chmod.Error);
  64. }
  65. }
  66. else
  67. {
  68. Console.WriteLine("Permissions of {0} kept with their defaults", e.Destination);
  69. }
  70.  
  71. if (e.Touch != null)
  72. {
  73. if (e.Touch.Error == null)
  74. {
  75. Console.WriteLine("Timestamp of {0} set to {1}", e.Touch.FileName, e.Touch.LastWriteTime);
  76. }
  77. else
  78. {
  79. Console.WriteLine("Setting timestamp of {0} failed: {1}", e.Touch.FileName, e.Touch.Error);
  80. }
  81. }
  82. else
  83. {
  84. // This should never happen during "local to remote" synchronization
  85. Console.WriteLine("Timestamp of {0} kept with its default (current time)", e.Destination);
  86. }
  87. }
  88.  
  89. public static void Run()
  90. {
  91. dataTable();
  92.  
  93. List<string> items = new List<string>();
  94.  
  95. foreach (string file in Directory.EnumerateFiles(localPath, "*.csv"))
  96. {
  97. if (file.Contains("test"))
  98. { }
  99. else
  100. {
  101. using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
  102. {
  103. using (StreamReader sr = new StreamReader(fs))
  104. {
  105. while (!sr.EndOfStream)
  106. items.Add(sr.ReadLine());
  107.  
  108. foreach (string item in items)
  109. {
  110. var row = dt.NewRow();
  111. string[] columnValues = item.Split(',');
  112. int column = 3;
  113.  
  114. for (int a = 0; a < columnValues.Length; a++)
  115. {
  116. string date = columnValues[29] + " " + columnValues[28];
  117. row[col[1].ColumnName] = DateTime.Parse(date);
  118. string test = file.Split(new[] { splitVal }, StringSplitOptions.None)[1];
  119. row[col[2].ColumnName] = test.Split('.')[0];
  120.  
  121. if (a >= 54)
  122. { }
  123. else
  124. {
  125. if (string.IsNullOrEmpty(columnValues[a]))
  126. {
  127. row[col[column].ColumnName] = DBNull.Value;
  128. }
  129. else
  130. {
  131. try
  132. {
  133. try
  134. {
  135. row[col[column].ColumnName] = columnValues[a].Trim();
  136. }
  137. catch
  138. {
  139. try
  140. {
  141. row[col[column].ColumnName] = Convert.ToDouble(columnValues[a].Trim());
  142. }
  143. catch
  144. {
  145. row[col[column].ColumnName] = int.Parse(columnValues[a].Trim());
  146. }
  147. }
  148. }
  149. catch (Exception ex)
  150. {
  151. Console.WriteLine("Error [" + col[column].ColumnName + "]:" + ex.ToString());
  152. //row[col[column].ColumnName] = DBNull.Value;
  153. }
  154. }
  155. }
  156.  
  157. column++;
  158. }
  159. dt.Rows.Add(row);
  160. }
  161.  
  162. using (SqlConnection con = new SqlConnection(consstring))
  163. {
  164. using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(con))
  165. {
  166. //Set the database table name
  167. sqlBulkCopy.DestinationTableName = dbTable;
  168. con.Open();
  169. try
  170. {
  171. sqlBulkCopy.WriteToServer(dt);
  172. Console.WriteLine(file.Substring(file.LastIndexOf('\') + 1) + " uploaded in the databasen");
  173. }
  174. catch (Exception ex)
  175. {
  176. Console.WriteLine(file.Substring(file.LastIndexOf('\') + 1) + " cannot be uploaded to database. " + ex.ToString());
  177. }
  178. con.Close();
  179. }
  180. }
  181. sr.Dispose();
  182. sr.Close();
  183. }
  184.  
  185. fs.Dispose();
  186. fs.Close();
  187. }
  188. }
  189. }
  190. }
  191.  
  192. foreach (TransferEventArgs transfer in synchronizationResult.Downloads)
  193. {
  194. string file = transfer.Destination;
  195. ...
  196. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement