【问题标题】:Streamwriter FilleSystem TextFile vb.netStreamwriter 文件系统文本文件 vb.net
【发布时间】:2014-11-24 22:38:55
【问题描述】:

我有一个带有复选框的数据网格,可以让用户进行多次删除。如何将删除的记录存储到逗号分隔的文本文件中?我正在使用 Access 来存储我的数据

我的删除代码是

Try
    con.Open()
    Sql = "DELETE FROM member WHERE ID = ?"

    With cmd
        .Connection = con
        .CommandText = Sql
        .Parameters.AddWithValue("@p1", 0)
    End With

    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells(0).FormattedValue = True Then
            cmd.Parameters("@p1").Value = Convert.ToInt32(row.Cells(1).FormattedValue)
            result = cmd.ExecuteNonQuery
        End If
    Next

    If result = 0 Then
        MsgBox("nothing.")
    Else
        MsgBox("deleted.")

    End If

Catch ex As Exception
    MsgBox(ex.Message)
End Try

con.Close()

【问题讨论】:

  • 请缩进您的代码以获得更易读的帖子
  • 为什么不使用一组 id - 在循环中创建 id 然后用一个语句删除
  • 你想在文本文件中存储什么?只是 ID 还是整个记录?
  • 我已经回滚了您删除大量代码的编辑,因为没有代码,答案毫无意义。

标签: vb.net


【解决方案1】:

在开始循环之前打开文件,使用 StreamWrite 传递所需的实际文件名 在每个循环中准备一个要写入文件的字符串。 (使用 string.Join 为从 gridview 获取的值创建一个逗号分隔的字符串

Using writer = new StreamWriter(.... your file name here ....)
Try
    con.Open()
    sql = "DELETE FROM member WHERE ID = ?"
    With cmd
        .Connection = con
        .CommandText = sql
        .Parameters.AddWithValue("@p1", 0)
    End With
    For Each row As DataGridViewRow In DataGridView1.Rows
       If row.Cells(0).FormattedValue = True Then
           ' Create a string from your current DataGridView,
           ' Note that I don't know in which columns are the data that
           ' that you want to save, so you need to adjust these readings
           Dim curText = string.Join(",", row.Cells(1).FormattedValue, _
                                          row.Cells(2).FormattedValue, _
                                          row.Cells(3).FormattedValue)


            cmd.Parameters("@p1").Value = Convert.ToInt32(row.Cells(1).FormattedValue)
            result = cmd.ExecuteNonQuery

            ' We delete one record for each loop, so we need to check the result for every
            ' loop. If we have a deleted record corresponding to ID passed then we can write
            ' the line that contains the data of the deleted record to the file
            If result > 0 Then
                writer.WriteLine(curText)
            End If
        End If
    Next
    .....

' The End Using closes the file and flushes everything to disk.
End Using

【讨论】:

  • 看起来不错,有什么方法可以帮助我根据我新更新的删除代码调整它,因为我已经更改了它。最后一个效果不佳...
  • 请解释什么不起作用。是否显示任何类型的错误消息?
  • 您重写的示例似乎没有错。所以下一步的行动是启动调试器,并一步一步地按照这段代码来看看这里到底发生了什么。循环是否至少进入了一次?循环内的 True 检查是肯定的吗?
  • 再次检查我的代码。 curText 的声明和单个删除记录的单行写入是在 for 循环的内部而不是在外部。 curText 未在循环外声明且无法访问。我会重写答案以适应你的新代码
  • 真正与众不同的是你愿意学习。如果你坚持下去,技能会随着你的进步而增长。请记住,这个领域是如此之大,以至于没有人可以称自己为整个 IT 行业的专家。参与这个社区也会有很大帮助。在 SO 上再见。
【解决方案2】:

将记录信息存储到临时存储中,然后将其保存到文件中。

Dim deletedRecords As New List(Of String)
Try
    con.Open()
    For Each row As DataGridViewRow In DataGridView1.Rows
        If row.Cells(0).FormattedValue = True Then
         'concatenate the row data into a string
          deletedRecords.Add({this record data})
          sql = "DELETE FROM member WHERE id = '" _
          & CStr(row.Cells(1).FormattedValue) & "'"
          With cmd
                .Connection = con
                .CommandText = sql
          End With

          result = cmd.ExecuteNonQuery
        End If
    Next
    If result = 0 Then
        MsgBox("No Deleted Record.")
    Else
        MsgBox("The Record(s) has been deleted.")
    End If
Catch ex As Exception
    MsgBox(ex.Message)
End Try
con.Close()

'save to text file
Using sw As New StreamWriter({path})
  For Each delRec In deletedRecords
    sw.WriteLine(delRec)
  Next
End Using

【讨论】:

  • 我不知道你想要哪些单元格,所以我留下了那个部分让你弄清楚 - 这不是复制粘贴 - 填写 {} 部分
  • 所以记录不在 DGV 中?在细胞里?
猜你喜欢
  • 2015-10-13
  • 2014-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多