检查文件流是否已打开或未使用vb.net中的if语句 [英] check file stream is open or not using if statement in vb.net
本文介绍了检查文件流是否已打开或未使用vb.net中的if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有人能说明检查文件流当前是否打开的条件是什么?
Can anyone show what would be the condition to check if the filestream is currently open?
fs = New FileStream(sfile, FileMode.Open, FileAccess.Read)
Dim picByte As Byte() = New Byte(fs.Length - 1) {}
fs.Read(picByte, 0, System.Convert.ToInt32(fs.Length))
fs.Close()
u_qry = "update nstudent set yer='" & cmb_year.Text & "',photo = @photo where stid='" & txt_sID.Text & "'"
'store image as Binary value using SQLParameter
Dim imgP As New SqlParameter
imgP.SqlDbType = SqlDbType.Binary
imgP.ParameterName = "photo"
imgP.Value = picByte
ucmd = New SqlCommand(u_qry, con)
ucmd.Parameters.Add(imgP)
ucmd.ExecuteNonQuery()
Dim st = "select * from nstudent"
scmd = New SqlCommand(st, con)
Dim dt As New DataTable
dt.Load(scmd.ExecuteReader)
With dgrid
.AutoGenerateColumns = False
.DataSource = dt
Call dgrid_col()
End With
MsgBox("Details Successfully Updated with Image")
这是一个常见的程序,代码性质是更新。我有两个选择。更新照片,更新没有照片。
低于我检查的条件。
Its a common procedure and the code nature is update. I''ve 2 option. update with photo, update without photo.
below the condition i''ve checked.
if(sfile <= 0 ) then 'Here sfile = OpenFileDialog1.FileName
{
' here update qry without image field
}
else {
'Here upadate qry with image field.
}
i不知道上述情况是否正确。请任何人说正确的方法来检查和操作。
i dont know the above condition is correct.? pls anyone say the correct way to check and do operation.
推荐答案
首先,正如我所看到的,你只是在阅读你用它的名字打开的文件。在这个和其他简单的情况下,你最好使用类System.IO.StreamReader
或System.IO.BinaryReader
:
http://msdn.microsoft.com /en-us/library/system.io.streamreader.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx [ ^ ]。
现在,根据检查流状态的技术的想法是错误的。相反,您应该只保证正确的状态:在阅读或写作之前创建一个阅读器或流,在完成后关闭它。更确切地说,请注意所有这些类实现接口System.IDisposable
。这意味着您始终必须
保证在完成任何流时调用System.IDisposable.Disposed
/ reader / writer对象。最好的方法是使用使用
语句:
http://msdn.microsoft.com/en-us/library/htd05whh.aspx [ ^ ]。
顺便说一下,声明是在读者的例子上说明。这正是你应该如何使用的。
祝你好运,
-SA
First of all, as I can see, you are just reading the file which you open using its name. In this and other simple cases case, you should better use either the classSystem.IO.StreamReader
orSystem.IO.BinaryReader
:
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^],
http://msdn.microsoft.com/en-us/library/system.io.binaryreader.aspx[^].
Now, the idea of the technique depending on checking the stream status is wrong. Instead, you should simply guarantee right status: create a reader or stream before reading or writing, close it when you are done. More exactly, please pay attention that all those classes implement the interfaceSystem.IDisposable
. It means that you alwaysmust
guarantee that theSystem.IDisposable.Disposed
is called when you are done with any of stream/reader/writer objects. The best way to do this is using theusing
statement:
http://msdn.microsoft.com/en-us/library/htd05whh.aspx[^].
By the way, the statement is illustrated on the example of the reader. This is exactly how you should use it.
Good luck,
—SA
这篇关于检查文件流是否已打开或未使用vb.net中的if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文