SFTP insert the list into sql database table using c sharp?

LogicLancer

Newbie
Joined
Oct 30, 2023
Messages
7
Reaction score
2
0
I get a list of attributes (filename, size and date) from a sftp client but I need to automatically insert that list into a SQL Server table. I have been able to connect to the database via Visual studio but now I have no idea how to insert that list into a database table. please help!!!!
Here is my c sharp code that lists the attributes

Code:
public static void Main()
        {
string host = @"sftp.com";
string username = "myusername";
string password = @"mypassword";
string remoteDirectory = " ";

using SftpClient sftp = new SftpClient(host, username, password);
            {
try
                {
                    sftp.Connect();
var files = sftp.ListDirectory(".");
foreach (var file in files)

                    {
                        Console.WriteLine(file.Name);
                        SftpFile f = sftp.Get(file.Name);
                        Console.WriteLine(f.Attributes.Size);
                        Console.WriteLine(f.Attributes.LastWriteTime);
                    }
                    sftp.Disconnect();
                }
 catch (Exception e)
                {
Console.WriteLine("An exception has been caught " + e.ToString());
                }
 
Did you find your answer? I suggest just pasting that all into ChatGPT and it will give you what you need.
 
0
I get a list of attributes (filename, size and date) from a sftp client but I need to automatically insert that list into a SQL Server table. I have been able to connect to the database via Visual studio but now I have no idea how to insert that list into a database table. please help!!!!
Here is my c sharp code that lists the attributes

Code:
public static void Main()
        {
string host = @"sftp.com";
string username = "myusername";
string password = @"mypassword";
string remoteDirectory = " ";

using SftpClient sftp = new SftpClient(host, username, password);
            {
try
                {
                    sftp.Connect();
var files = sftp.ListDirectory(".");
foreach (var file in files)

                    {
                        Console.WriteLine(file.Name);
                        SftpFile f = sftp.Get(file.Name);
                        Console.WriteLine(f.Attributes.Size);
                        Console.WriteLine(f.Attributes.LastWriteTime);
                    }
                    sftp.Disconnect();
                }
 catch (Exception e)
                {
Console.WriteLine("An exception has been caught " + e.ToString());
                }
You need to use SQL connection to insert, in ideal you need to use EntityFramework, but now this code is appropriate)
0
I get a list of attributes (filename, size and date) from a sftp client but I need to automatically insert that list into a SQL Server table. I have been able to connect to the database via Visual studio but now I have no idea how to insert that list into a database table. please help!!!!
Here is my c sharp code that lists the attributes

Code:
public static void Main()
        {
string host = @"sftp.com";
string username = "myusername";
string password = @"mypassword";
string remoteDirectory = " ";

using SftpClient sftp = new SftpClient(host, username, password);
            {
try
                {
                    sftp.Connect();
var files = sftp.ListDirectory(".");
foreach (var file in files)

                    {
                        Console.WriteLine(file.Name);
                        SftpFile f = sftp.Get(file.Name);
                        Console.WriteLine(f.Attributes.Size);
                        Console.WriteLine(f.Attributes.LastWriteTime);
                    }
                    sftp.Disconnect();
                }
 catch (Exception e)
                {
Console.WriteLine("An exception has been caught " + e.ToString());
                }
0
I get a list of attributes (filename, size and date) from a sftp client but I need to automatically insert that list into a SQL Server table. I have been able to connect to the database via Visual studio but now I have no idea how to insert that list into a database table. please help!!!!
Here is my c sharp code that lists the attributes

Code:
public static void Main()
        {
string host = @"sftp.com";
string username = "myusername";
string password = @"mypassword";
string remoteDirectory = " ";

using SftpClient sftp = new SftpClient(host, username, password);
            {
try
                {
                    sftp.Connect();
var files = sftp.ListDirectory(".");
foreach (var file in files)

                    {
                        Console.WriteLine(file.Name);
                        SftpFile f = sftp.Get(file.Name);
                        Console.WriteLine(f.Attributes.Size);
                        Console.WriteLine(f.Attributes.LastWriteTime);
                    }
                    sftp.Disconnect();
                }
 catch (Exception e)
                {
Console.WriteLine("An exception has been caught " + e.ToString());
                }
You need to use the SQL connection to insert, in ideal you need to use Entity Framework, but now this code is appropriate)

using System;
using System.Collections.Generic;
using System.Data.SqlClient;

class Program
{
static void Main()
{
List<MyData> dataList = GetYourData(); // Replace this with your data retrieval logic

string connectionString = "Your_SQL_Server_Connection_String";

using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();

foreach (MyData data in dataList)
{
string insertQuery = "INSERT INTO YourTable (Column1, Column2, ...) VALUES (@Value1, @Value2, ...)";

using (SqlCommand cmd = new SqlCommand(insertQuery, connection))
{
cmd.Parameters.AddWithValue("@Value1", data.Property1);
cmd.Parameters.AddWithValue("@Value2", data.Property2);
// Add parameters for each column in your table

cmd.ExecuteNonQuery();
}
}
}
}

// Define your data class
public class MyData
{
public int Property1 { get; set; }
public string Property2 { get; set; }
// Add properties for each column in your table
}

// Replace this with your data retrieval logic
static List<MyData> GetYourData()
{
// Implement your logic to populate the list
return new List<MyData>();
}
}
 
Back
Top