在这个例子中共享安全吗? [英] Is shared safe in this example?

查看:53
本文介绍了在这个例子中共享安全吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,快问题 - 我需要一点清晰。


如果我有一个只有公共共享功能的公共类,在APS.NET网络应用程序中(对于常见程序)是否安全(因为多个网页,可以随时访问其中的代码)


以下示例 - 填充的简单例程下拉列表或列表。通过APSX页面多次访问是否安全?

我想是这样,但我需要一个更有见识的意见。


公共类MyUtils

公共共享函数FillList(ByVal objList As Object,ByVal sSql As String ,ByVal sConnection As String)As

Boolean

Dim dr As OleDb.OleDbDataReader,Li As ListItem

Dim cn As New OleDb.OleDbConnection( sConnection)

Dim cmd作为OleDb.OleDbCommand

尝试

cn.Open()

cmd =新OleDb .OleDbCommand(sSql,cn)

dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)

while dr.Read

Li = New ListItem

Li.Text = dr(0)

Li.Value = dr(1)

objList.Items.Add(Li)

结束时

Catch ex As Exception

停止

返回错误

最后

cmd.Dispose()

如果dr Is Nothing = False那么dr.Close()

cn.Close()

结束尝试

返回真实

结束功能

结束课程


用法......

FillList(cmbListOfNames,AppSettings(" SQL.PEOPLE"),AppSettings(" CONSTRING.M ANAGEMENT"))

解决方案

嗨Stevie,


我从未使用它,我不知道你要归档什么,这个共享

功能。不过也许我错了,我没有直接看到它的原因

共享所以也许你可以告诉我们。


代码中的一些东西。

在我看来,你可以将列表作为列表框传递,而不是你没有使用

后期绑定,现在你使用的资源超过了所需的资源。 (因为即使在读取循环中它也是
我认为真的很多时间)。当你想以一个组合框作为列表框时,你可以这样做,但是你可以这样做,但是你需要将b / b
填充到listcontrol中我的意见。


为了防止下次这个小事,在你的程序中设置选项严格,而不是你在这种情况下被警告事情。


处理命令没有任何意义,而我认为在这个连接中

可以更好地处置因为connection.pooling作为Angel总是

说。


只是一些想法,


:-)


Cor


公共类MyUtils
公共共享函数FillList(ByVal objList As Object,ByVal sSql As
String,ByVal sConnection As String )作为布尔值
Dim dr As OleDb.OleDbDataReader,Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
试试
cn.Open()
cmd =新的OleDb.OleDbCommand(sSql,cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
而dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
结束时
抓住ex Exception
停止
返回错误
最后
cmd.Dispose()
如果dr is Nothing = False那么dr.Close()
cn.Close()
结束尝试
返回True
结束功能
结束类

用法......
FillList(cmbListOfNames,
AppSettings(" SQL.PEOPLE"),AppSettings(" CONSTRING.M ANAGEMENT"))



Hi Stevie,


我从来没用过它,我也不知道你想要归档什么共享

功能。不过也许我错了,我没有直接看到它的原因

共享所以也许你可以告诉我们。


代码中的一些东西。

在我看来,你可以将列表作为列表框传递,而不是你没有使用

后期绑定,现在你使用的资源超过了所需的资源。 (因为即使在读取循环中它也是
我认为真的很多时间)。当你想以一个组合框作为列表框时,你可以这样做,但是你可以这样做,但是你需要将b / b
填充到listcontrol中我的意见。


为了防止下次这个小事,在你的程序中设置选项严格,而不是你在这种情况下被警告事情。


处理命令没有任何意义,而我认为在这个连接中

可以更好地处置因为connection.pooling作为Angel总是

说。


只是一些想法,


:-)


Cor


公共类MyUtils
公共共享函数FillList(ByVal objList As Object,ByVal sSql As
String,ByVal sConnection As String )作为布尔值
Dim dr As OleDb.OleDbDataReader,Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
试试
cn.Open()
cmd =新的OleDb.OleDbCommand(sSql,cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
而dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
结束时
抓住ex Exception
停止
返回错误
最后
cmd.Dispose()
如果dr is Nothing = False那么dr.Close()
cn.Close()
结束尝试
返回True
结束功能
结束类

用法......
FillList(cmbListOfNames,
AppSettings(" SQL.PEOPLE"),AppSettings(" CONSTRING.M ANAGEMENT"))



Hi Cor&感谢花时间回答。


Object而不是ListBox的原因是该函数将接受DropDown或ListBox。这不是

问题 - 我知道我可以(也许应该)使用重载,但速度在这里绝不是问题。


像这样的共享类的原因是,它将包含我们在一段时间内放在一起的所有有用函数

,如FillList,SelectListItem,FindLastItemInList,EmptyList,SortList等等


这些将在许多不同的类/网页等中被许多不同的函数使用。比如调用常见的

库。


进口MyUtils

类SomeWebPage

.. 。

.. 。


Private Sub Page_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)处理MyBase.Load

如果IsPostBack = False那么

FillList(cmbList1,sSqlQuery,sConStr)''<<<函数调用MyUtills.FillList

SortList(cmbList1,True)''<<<函数调用MyUtills.SortList

SelectListItem(cmbList1,sUserInput)''<<<函数调用MyUtills.SelectListItem

结束如果

结束子

.. 。

.. 。

结束班级

我的问题是它是否安全 - 由于会对相同的共享功能进行许多不同的调用 - 是否存在问题

以这种方式编码

" Cor Ligthert" <无********** @ planet.nl>在消息新闻中写道:或者************* @ TK2MSFTNGP12.phx.gbl ...

嗨Stevie,

我从来没用过它和我不知道你想要存档什么,这个共享
功能。不过也许我错了,我没有直接看到让它共享的理由
所以也许你可以告诉我们。

你代码中的一些东西。
在我看来您可以将该列表作为列表框传递,而不是使用
后期绑定,现在您使用的资源超过了所需的资源。 (因为它甚至在读取循环中我认为真的很多时间)。如果你想做一个组合框作为列表框,那么你可以这样做,但是在你看来填充它时你应该把它变成listcontrol。

为了防止下次这个小事,在你的程序之上设置选项严格,而不是在这种情况下警告你。

处理命令没有我认为在这种情况下,连接可以更好地处理因为连接。天使总是说。

只是一些想法,

:-)


公共类MyUtils
公共共享函数FillList(ByVal objList As Object,ByVal sSql As


String,ByVal sConnection As String)as

Boolean
Dim dr As OleDb.OleDbDataReader,Li As ListItem
Dim cn As New OleDb.OleDbConnection (sConnection)
Dim cmd作为OleDb.OleDbCommand
试试
cn.Open()
cmd =新OleDb.OleDbCommand(sSql,cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
而dr.Read
李=新ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
结束时
Catch ex Exception
停止
返回错误
最后
cmd.Dispose()
如果dr is Nothing = False那么dr.Close()
cn。关闭()
结束尝试
返回真实
结束功能
结束类

用法......
FillList(cmbListOfNames,

AppSettings(" SQL.PEOPLE"),AppSettings(&#CONSTRING.M ANAGEMENT"))




Hi all, quick question - I need a bit of clarity.

If I have a public class with only Public Shared functions in it, in an APS.NET web app (for common procedures) is it
safe (since multiple web pages, can at any time access the code within)

Example below - a simple routine that fills a dropdownlist or list. Would it be safe being accessed at multiple times by
the APSX pages? I think so, but I need an a more knowledgeable opinion.

Public Class MyUtils
Public Shared Function FillList(ByVal objList As Object, ByVal sSql As String, ByVal sConnection As String) As
Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function
End Class

Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.M ANAGEMENT"))

解决方案

Hi Stevie,

I never used it and I do not know what you want to archive, with this shared
function. However maybe I am wrong, I do not direct see a reason to make it
shared so maybe you can tell us that.

Some things in your code.
In my opinion you can pass that List as Listbox and than you are not using
late binding, now you use more resources than needed. (And because it is
even in the read loop I think really a lot of time). When you want to do as
well a combobox as a listbox, than you can do it this way, however than you
should cast it to a listcontrol when filling it in my opinion.

To prevent this small things the next time, set option strict on in top of
your program, than you are warned on this kind of things.

Disposing of the command has no sence, while I think in this the connection
can better be disposed because of the connection.pooling as Angel always
says.

Just some thoughts,

:-)

Cor


Public Class MyUtils
Public Shared Function FillList(ByVal objList As Object, ByVal sSql As String, ByVal sConnection As String) As Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function
End Class

Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.M ANAGEMENT"))



Hi Stevie,

I never used it and I do not know what you want to archive, with this shared
function. However maybe I am wrong, I do not direct see a reason to make it
shared so maybe you can tell us that.

Some things in your code.
In my opinion you can pass that List as Listbox and than you are not using
late binding, now you use more resources than needed. (And because it is
even in the read loop I think really a lot of time). When you want to do as
well a combobox as a listbox, than you can do it this way, however than you
should cast it to a listcontrol when filling it in my opinion.

To prevent this small things the next time, set option strict on in top of
your program, than you are warned on this kind of things.

Disposing of the command has no sence, while I think in this the connection
can better be disposed because of the connection.pooling as Angel always
says.

Just some thoughts,

:-)

Cor


Public Class MyUtils
Public Shared Function FillList(ByVal objList As Object, ByVal sSql As String, ByVal sConnection As String) As Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function
End Class

Usage...
FillList(cmbListOfNames, AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.M ANAGEMENT"))



Hi Cor & thanks for taking the time to answer.

The reason for Object rather than ListBox is that the function will accept either DropDown or ListBox. This isn''t the
issue - I know I could (perhaps should) use overloading, but speed is by no means an issue here.

The reason for a Shared class like this is that, it would contain all of the useful functions we put together over time
like FillList, SelectListItem, FindLastItemInList, EmptyList, SortList etc etc etc

These would be used in many by different functions in many different Classes / Web pages etc. Like calls to a common
library.

Imports MyUtils
Class SomeWebPage
.. . .
.. . .

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If IsPostBack = False Then
FillList(cmbList1, sSqlQuery, sConStr) ''<<< Function call to MyUtills.FillList
SortList(cmbList1,True) ''<<< Function call to MyUtills.SortList
SelectListItem(cmbList1,sUserInput) ''<<< Function call to MyUtills.SelectListItem
End If
End Sub
.. . .
.. . .
End Class
My question was Is It Safe - Since many different calls will be made to the same ''shared'' functions - is there a problem
in coding this way
"Cor Ligthert" <no**********@planet.nl> wrote in message news:Or*************@TK2MSFTNGP12.phx.gbl...

Hi Stevie,

I never used it and I do not know what you want to archive, with this shared
function. However maybe I am wrong, I do not direct see a reason to make it
shared so maybe you can tell us that.

Some things in your code.
In my opinion you can pass that List as Listbox and than you are not using
late binding, now you use more resources than needed. (And because it is
even in the read loop I think really a lot of time). When you want to do as
well a combobox as a listbox, than you can do it this way, however than you
should cast it to a listcontrol when filling it in my opinion.

To prevent this small things the next time, set option strict on in top of
your program, than you are warned on this kind of things.

Disposing of the command has no sence, while I think in this the connection
can better be disposed because of the connection.pooling as Angel always
says.

Just some thoughts,

:-)

Cor


Public Class MyUtils
Public Shared Function FillList(ByVal objList As Object, ByVal sSql As


String, ByVal sConnection As String) As

Boolean
Dim dr As OleDb.OleDbDataReader, Li As ListItem
Dim cn As New OleDb.OleDbConnection(sConnection)
Dim cmd As OleDb.OleDbCommand
Try
cn.Open()
cmd = New OleDb.OleDbCommand(sSql, cn)
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
While dr.Read
Li = New ListItem
Li.Text = dr(0)
Li.Value = dr(1)
objList.Items.Add(Li)
End While
Catch ex As Exception
Stop
Return False
Finally
cmd.Dispose()
If dr Is Nothing = False Then dr.Close()
cn.Close()
End Try
Return True
End Function
End Class

Usage...
FillList(cmbListOfNames,


AppSettings("SQL.PEOPLE"),AppSettings("CONSTRING.M ANAGEMENT"))




这篇关于在这个例子中共享安全吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆