Как вы получаете строку из MemoryStream?

Брайан Хупер: Вы почти достигли точки, но у вас есть ошибка в вашем синакс. Ваша вставка никогда не будет работать. Я тестировал свою базу данных, и вот правильный ответ:

INSERT INTO podatki (datum,ura,meritev1,meritev1_sunki,impulzi1,meritev2,meritev2_sunki,impulzi2)
            SELECT '$datum', '$ura', '$meritve1','$sunki1','$impulzi1','$meritve2','$sunki2','$impulzi2'
                FROM dual
                WHERE NOT EXISTS (SELECT datum,ura
                                      FROM podatki
                                      WHERE datum='$datum' and ura='$ura'

Я даю вам мой пример таблицы y. Вставка почти такая же, как писал Биан Хупер, за исключением того, что я помещаю select FROM DUAL ont из другой таблицы. С уважением, Иван

492
задан Erik Schierboom 3 October 2013 в 11:51
поделиться

5 ответов

Этот образец показывает, как считать и записать строку в MemoryStream.

<час>
Imports System.IO

Module Module1
  Sub Main()
    ' We don't need to dispose any of the MemoryStream 
    ' because it is a managed object. However, just for 
    ' good practice, we'll close the MemoryStream.
    Using ms As New MemoryStream
      Dim sw As New StreamWriter(ms)
      sw.WriteLine("Hello World")
      ' The string is currently stored in the 
      ' StreamWriters buffer. Flushing the stream will 
      ' force the string into the MemoryStream.
      sw.Flush()
      ' If we dispose the StreamWriter now, it will close 
      ' the BaseStream (which is our MemoryStream) which 
      ' will prevent us from reading from our MemoryStream
      'sw.Dispose()

      ' The StreamReader will read from the current 
      ' position of the MemoryStream which is currently 
      ' set at the end of the string we just wrote to it. 
      ' We need to set the position to 0 in order to read 
      ' from the beginning.
      ms.Position = 0
      Dim sr As New StreamReader(ms)
      Dim myStr = sr.ReadToEnd()
      Console.WriteLine(myStr)

      ' We can dispose our StreamWriter and StreamReader 
      ' now, though this isn't necessary (they don't hold 
      ' any resources open on their own).
      sw.Dispose()
      sr.Dispose()
    End Using

    Console.WriteLine("Press any key to continue.")
    Console.ReadKey()
  End Sub
End Module
447
ответ дан InteXX 3 October 2013 в 11:51
поделиться

Можно также использовать

Encoding.ASCII.GetString(ms.ToArray());

, я не делаю , думают , это менее эффективно, но я не мог поклясться ему. Это также позволяет Вам выбрать различное кодирование, тогда как с помощью StreamReader необходимо было бы определить это в качестве параметра.

273
ответ дан Adam Dymitruk 3 October 2013 в 11:51
поделиться

используйте StreamReader, тогда можно использовать метод ReadToEnd , который возвращает строку.

35
ответ дан Darren Kopp 3 October 2013 в 11:51
поделиться

Используя StreamReader для преобразования MemoryStream в Строку.

<Extension()> _
Public Function ReadAll(ByVal memStream As MemoryStream) As String
    ' Reset the stream otherwise you will just get an empty string.
    ' Remember the position so we can restore it later.
    Dim pos = memStream.Position
    memStream.Position = 0

    Dim reader As New StreamReader(memStream)
    Dim str = reader.ReadToEnd()

    ' Reset the position so that subsequent writes are correct.
    memStream.Position = pos

    Return str
End Function
99
ответ дан Brian 3 October 2013 в 11:51
поделиться

Слегка измененная версия ответа Брайана позволяет дополнительно управлять запуском чтения. Это, по-видимому, самый простой метод. наверное, не самый эффективный, но простой в понимании и использовании.

Public Function ReadAll(ByVal memStream As MemoryStream, Optional ByVal startPos As Integer = 0) As String
    ' reset the stream or we'll get an empty string returned
    ' remember the position so we can restore it later
    Dim Pos = memStream.Position
    memStream.Position = startPos

    Dim reader As New StreamReader(memStream)
    Dim str = reader.ReadToEnd()

    ' reset the position so that subsequent writes are correct
    memStream.Position = Pos

    Return str
End Function
5
ответ дан 22 November 2019 в 22:33
поделиться
Другие вопросы по тегам:

Похожие вопросы: