请教sql的优化写法

michaelmodna 2009-12-24 12:59:03

Dim request As New IB1001Request(reqBuffer)
Dim strSQL As String

Dim conn As SqlConnection = SqlConnFactory.CreateConn()
Dim cmd As SqlClient.SqlCommand

conn.Open()

Dim AccNum As String
Dim dr As SqlDataReader

If request.ACNO.Trim <> String.Empty Then
AccNum = request.ACNO.Trim
Else
If request.SETACCNO.Trim <> String.Empty Then
AccNum = request.SETACCNO.Trim
End If
End If

Try
strSQL = String.Format("select * from CPAccounts where cpa_accnum = '{0}'", AccNum)

cmd = New SqlCommand(strSQL, conn)
dr = cmd.ExecuteReader(CommandBehavior.Default)

If dr.Read() Then '查找是否已参加服务A
IS_CP = True
cp_account = dr.Item("cpa_accnum")

Else
IS_CP = False
cp_account = String.Empty
End If

dr.Close()

strSQL = String.Format("select * from AMAccounts where ama_accnum = '{0}'", AccNum)

cmd = New SqlCommand(strSQL, conn)
dr = cmd.ExecuteReader(CommandBehavior.Default)

If dr.Read() Then '查找是否已参加服务B
IS_AM = True
am_account = dr.Item("ama_accnum")
Else
IS_AM = False
am_account = String.Empty
End If
dr.Close()

strSQL = String.Format("select * from IBAccounts where iba_accnum = '{0}'", AccNum)

cmd = New SqlCommand(strSQL, conn)
dr = cmd.ExecuteReader(CommandBehavior.Default)

If dr.Read() Then '查找是否已参加服务C
IS_IB = True
ib_account = dr.Item("iba_accnum")
Else
IS_IB = False
ib_account = String.Empty
End If

Catch ex As Exception
Debug.WriteLine(ex.ToString)
Throw
Finally
dr.Close()
cmd.Dispose()
conn.Close()
End Try


想确认一下,上述查询中,是否存在能优化的地方,如用一个select 即可之类的做法,而且目前应用较频繁,经常出现

超时时间已到。超时时间已到,但是尚未从池中获取连接。出现这种情况可能是因为所有池连接均在使用,并且达到了最大池大小

我查过相关文章,说是可以将 datareader 改为 DataAdapter和DataSet,请问是有这回事吗?先谢过各位了!
...全文
135 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
a2bvchen 2009-12-25
  • 打赏
  • 举报
回复
额 VB?
michaelmodna 2009-12-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wang_wei_jie 的回复:]
select a=(case when exists(select * from CPAccounts  where  cpa_accnum = '{0}') then 1 else 0 end),
b=(case when exists(select * from AMAccounts  where  ama_accnum = '{0}') then 1 else 0 end ),
c=(case when exists(select * from IBAccounts  where  iba_accnum = '{0}') then 1 else 0 end )
[/Quote]

感谢 5楼的 朋友,谢谢!!!
wuyq11 2009-12-24
  • 打赏
  • 举报
回复
使用数据库操作类
wang_wei_jie 2009-12-24
  • 打赏
  • 举报
回复
这个肯定是有结果的。。。你可以看一下dr.read的结果,如果a=0则没有参加服务A,=1则参加。。。
wang_wei_jie 2009-12-24
  • 打赏
  • 举报
回复
select a=(case when exists(select * from CPAccounts where cpa_accnum = '{0}') then 1 else 0 end),
b=(case when exists(select * from AMAccounts where ama_accnum = '{0}') then 1 else 0 end ),
c=(case when exists(select * from IBAccounts where iba_accnum = '{0}') then 1 else 0 end )
michaelmodna 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lovely_baby 的回复:]
asp代码??
[/Quote]
是 vb.net的
Lovely_baby 2009-12-24
  • 打赏
  • 举报
回复
asp代码??
michaelmodna 2009-12-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tzs2304 的回复:]
up
select * 写成select 字段1,字段2
[/Quote]

是三不不同的表,然后我分别传帐号过去,如有资料,则赋True,能具体些吗?
tzs2304 2009-12-24
  • 打赏
  • 举报
回复
up
select * 写成select 字段1,字段2

62,250

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧