如何在我的代码中实现SaveFileDialog,我正在使用FileSteam对象

时间:2017-09-12 14:44:59

标签: vb.net filestream streamwriter savefiledialog

所以这是我的代码与方法,但我似乎无法弄清楚如何在...内实现保存文件对话...任何信息或指导将受到高度赞赏。

Private Sub btSave_Click(sender As System.Object, e As System.EventArgs) Handles btSave.Click
        If (cbPeriod.SelectedItem IsNot Nothing) Then
            Try
                Using connect As New SqlConnection(connectionString)
                    Dim command As New SqlCommand()
                    command.CommandText = selectQuery
                    command.Parameters.AddWithValue("@Period", cbPeriod.SelectedItem.ToString)
                    command.Connection = connect
                    Dim fileName As String
                    fileName = "Data.txt"
                    Dim seqNo As Integer = 0
                    Dim currDocNo As String = ""
                    Dim prevDocNo As String = ""
                    Using FileObject As New FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None)
                        Using StreamWriterObj As New StreamWriter(FileObject)
                            connect.Open()
                            Using reader As SqlDataReader = command.ExecuteReader()
                                Dim FieldCount As Integer = reader.FieldCount - 1
                                For i = 0 To FieldCount
                                    StreamWriterObj.Write(reader.GetName(i).ToString)
                                    StreamWriterObj.Write(" @ ")
                                Next
                                StreamWriterObj.WriteLine()
                                Do While reader.Read()
                                    currDocNo = reader.GetValue(reader.GetOrdinal("ДокументNo")).ToString
                                    StreamWriterObj.Write(reader.Item(0))
                                    For i = 1 To FieldCount
                                        currDocNo = reader.GetValue(reader.GetOrdinal("ДокументNo")).ToString
                                        If (reader.GetName(i).Equals("ПореденНомер", StringComparison.InvariantCultureIgnoreCase)) Then
                                            If (currDocNo = prevDocNo) Then
                                                seqNo += 1
                                                StreamWriterObj.Write(seqNo)
                                            Else
                                                seqNo = 1
                                                StreamWriterObj.Write(" @ ")
                                                StreamWriterObj.Write(seqNo)
                                            End If
                                        Else
                                            StreamWriterObj.Write(" @ ")
                                            StreamWriterObj.Write(reader.Item(i))
                                        End If
                                    Next
                                    prevDocNo = currDocNo
                                    StreamWriterObj.WriteLine()
                                Loop
                                reader.Close()
                            End Using
                            connect.Close()
                            MessageBox.Show("Export was successful.")
                        End Using
                    End Using
                End Using
            Catch ex As SqlException
                MessageBox.Show(ex.Message.ToString)
            End Try
        Else
            MessageBox.Show("Please select a value!")
        End If
    End Sub

如果您需要进一步的信息,请告诉我们。正如你所看到的,我有File对象和所有内容所以我想我只需要添加一些savefiledialog但是如何将streamwriter的数据输入到savefiledialog中?

1 个答案:

答案 0 :(得分:0)

使用SaveFileDialog获取这样的文件名

let
Source = Json.Document(Web.Contents("http://api.stackexchange.com/2.2/users/6231494/answers?order=desc&sort=activity&site=stackoverflow")),
items = Source[items],
#"Converted to Table" = Table.FromList(items, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
#"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"owner", "is_accepted", "score", "last_activity_date", "creation_date", "answer_id", "question_id"}, {"Column1.owner", "Column1.is_accepted", "Column1.score", "Column1.last_activity_date", "Column1.creation_date", "Column1.answer_id", "Column1.question_id"}),
#"Expanded Column1.owner" = Table.ExpandRecordColumn(#"Expanded Column1", "Column1.owner", {"reputation", "user_id", "user_type", "profile_image", "display_name", "link"}, {"Column1.owner.reputation", "Column1.owner.user_id", "Column1.owner.user_type", "Column1.owner.profile_image", "Column1.owner.display_name", "Column1.owner.link"})
in
#"Expanded Column1.owner"

请勿在{{1​​}}块中包装的一次性对象上显式调用Dim fileName As String ' fileName = "Data.txt" Using sfd As New SaveFileDialog() sfd.Filter = "Text Files (*.txt)|*.txt" If sfd.ShowDialog() = Windows.Forms.DialogResult.OK Then fileName = sfd.FileName Else Throw New Exception("User canceled out of save dialog") End If End Using 。调用Dispose方法并为您关闭@ Close()

最后,您可以使用this constructor删除FileStream并使用StreamWriter创建文件

Using