0

嗨,我是 c# 和 sharpssh 的新手,我正在尝试将 csv 文件导出到 sqlite3 数据库到名为 explor 的外部设备。我已设法将 csv 文件导出到本地 sqlite3 数据库,并能够使用Sharpssh 连接到外部设备的数据库,但我想同时执行这两项操作。1) 连接到外部设备 2) 将 csv 文件导出到设备上的数据库/表。 任何帮助将不胜感激。

1.通过SHARPSSH连接

 string _ftpURL = @"192.178.0.77";  //(FAKE IP)
            string _UserName = "root"; //FAKE User Name of the SFTP server
            string _Password = "preethi"; //FAKE Password of the SFTP server
            int _Port = 2222; //Port No of the SFTP server (if any)
            string _ftpDirectory = "/home/root/systools/WM/WebMobility.db"; //The directory in SFTP server where the files will be uploaded
            string LocalDirectory = "I:\\preethi"; //Local directory from where the files will be uploaded
            string FileName = "exploretest.csv"; //File name, which one will be uploaded
    //        IPHostEntry ip = Dns.GetHostEntry(@"1.1.1.2");
            Sftp Connection = new Sftp(_ftpURL,_UserName, _Password);
            Connection.Connect(_Port);

导出 CSV 文件

 string strFileName = "I:/preethi/exploretest.csv";
            OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; Data Source = " + System.IO.Path.GetDirectoryName(strFileName) + "; Extended Properties = \"Text;HDR=YES;FMT=Delimited\"");
            conn.Open();
            OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + System.IO.Path.GetFileName(strFileName), conn);
            DataSet ds = new DataSet("Temp");
            adapter.Fill(ds);

            DataTable tb = ds.Tables[0];


            //SQLiteConnection.CreateFile("MyDatabase.sqlite");
            SQLiteConnection m_dbConnection;
            m_dbConnection = new SQLiteConnection("Data source = 19.9.9.3; port=2222; database = WebMobility.db; uid=root; pwd=preethi; Convert Zero Datetime=true;");
        m_dbConnection.Open();
        var dt = ds.Tables[0];

        foreach (DataRow dr in dt.Rows)
        {
            var Id = dr["Id"].ToString();
            var VRM = dr["VehicleRegistration"].ToString();
            var Points = Convert.ToInt32(dr["TicketScore"].ToString());
            string sql = "insert into NaughtyList (Id,VRM,Points) values ( '" + Id + "','" + VRM + "'," + Points + ")";
            SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }
        m_dbConnection.Close();
4

0 回答 0