Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
U

User 14071496

@User 14071496
About
Posts
2
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • How do I take datatable values into postgresql
    U User 14071496

    Thanks for the suggestions. Here is an update that would explain everything in more detail. I have a datatable with values, the first column will always be a string and the rest (5 columns) will have numbers. I have used the code that I shared to make it work for a datagridview but now I want to make it work for a datatable. The way I though was best was to take each row into the postgresql server and store it until its needed later. It seems like a sqladapter seems to be a better way to handle this and therefore I had some help trying to figure this out with the following code:

    using (NpgsqlDataAdapter adapter = new NpgsqlDataAdapter("SELECT num1, num2, num3, num4, num5, num6 FROM " + Table + ";", Conn))
    {
    NpgsqlCommand insert = new NpgsqlCommand("INSERT INTO " + Table + " VALUES (@num1, @num2, @num3, @num4, @num5, @num6);", Conn);
    insert.Parameters.Add("@num1", NpgsqlDbType.Text, 200, "1");
    insert.Parameters.Add("@num2", NpgsqlDbType.Text, 50, "2");
    insert.Parameters.Add("@num3", NpgsqlDbType.Text, 50, "3");
    insert.Parameters.Add("@num4", NpgsqlDbType.Text, 50, "4");
    insert.Parameters.Add("@num5", NpgsqlDbType.Text, 50, "5");
    insert.Parameters.Add("@num6", NpgsqlDbType.Text, 50, "6");

    adapter.InsertCommand = insert;
    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    
    DataTable table = new DataTable();
    
    // Generate the DataTable schema.
    adapter.FillSchema(table, SchemaType.Source);
    
    // Add the new rows to the DataTable, e.g.
    DataRow row = table.NewRow();
    
    //Where I´m unsure what to do. This is obivously not the way to write the code but would appreciate it if I get some help to write this the right way
    row\["1"\] = row.Table.Rows\[0\]\[0\].ToString(); //Does not continue after this line
    row\["2"\] = row.Table.Rows\[0\]\[1\].ToString();
    row\["3"\] = row.Table.Rows\[0\]\[2\].ToString();
    row\["4"\] = row.Table.Rows\[0\]\[3\].ToString();
    row\["5"\] = row.Table.Rows\[0\]\[4\].ToString();
    row\["6"\] = row.Table.Rows\[0\]\[5\].ToString();
    
    table.Rows.Add(row);
    
    // Save the changes.
    adapter.Update(table);
    

    }
    }

    The code seems to work until the actual strings/values need to be put inside the SQL server table(see inside of code to know where the code breaks out). If anyone knows how to make the code work or is willing to show whats missing I would gladly appreciate it.

    C# postgresql question

  • How do I take datatable values into postgresql
    U User 14071496

    I´m trying to take the entire datatable with its values to postgresql but I seem to do something wrong. Code is as follows:

    foreach (DataRow row in dt_IS.Rows)
    {
    using (NpgsqlCommand cmdCopy = new NpgsqlCommand("INSERT INTO " + Table + " VALUES(@num1));", Conn))
    {
    object num1 = row.Table.Rows[0][0].ToString();
    cmdCopy.Parameters.AddWithValue("@num1", num1);
    Conn.Open();
    cmdCopy.ExecuteNonQuery();
    Conn.Close();
    }
    }

    C# postgresql question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups