简体   繁体   中英

Inserting Data in Sql table from a Stream of bytes

I want to find a way to populate a DB SQL table directly from a Stream of bytes. I constructed a MemoryStream and now I would like to use this data to populate a table. Example of file loaded in MemoryStream:

row1|1|1|1|1
row2|2|2|2|2
row3|3|3|3|3
row4|4|4|4|4

I would like to identify the | as a column seaparator and the \\n as a row separator. I found a way of doing this using a DataAdaptor but I am not sure how to use it. Can anyone guide me through the process?

private void Insert(MemoryStream src,string provider, string table)
    {
       // code to populate the table
    }

Assuming that it is Sql Server:

using(var c = new SqlConnection(provider))
{
 c.Open();
 using(var sc = c.CreateCommand())
 {
  sc.CommandText = "INSERT "+table+" VALUES(@data)";
  sc.Parameters.AddWithValue("@data", src.ToArray());
  sc.ExecuteNonQuery();
 }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM