从放入变量的SQL中检索数据 [英] Retrieve data from SQL put in variable

查看:126
本文介绍了从放入变量的SQL中检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试这个但是在这里我会追溯0 ..



我尝试过的事情:



公共函数TotalYarnBalance_ByNtl_no(ByVal objModel)As Double



Dim sql As String =select(isnull(sum(rec_frm_suppl_qty),0 )+ isnull(sum(ret_frm_knit_qty),0)+ isnull(sum(ret_frm_subcont_qty),0)+ isnull(sum(loan_frm_party),0)) - ((isnull(sum(iss_to_knitt_qty),0))+ isnull(sum(sum) iss_to_subcont_qty),0)+ isnull(sum(ret_to_suppl_qty),0)+ isnull(sum(loan_to_party),0)+ sum(isnull(Sale,0)))作为来自trans_yarn的余额,其中ntl_no ='+ objModel.ntl_order + '和yarn_count ='+ objModel.yarn_count +'和yarn_type ='+ objModel.yarn_type +'和brand ='+ objModel.brand +'和lot_no ='+ objModel.lot_no +'



Dim dt_type As DataTable

Dim i As Double

dt_type =新DataTable(ViewYarnBalance)

da =新的SqlDataAdapter(sql,conn)

conn.Open()

da.Fill(dt_type)

conn.Close()



返回i = dt_type.Rows(0)(0)



结束函数

I am trying this but in here i retrive 0 ..

What I have tried:

Public Function TotalYarnBalance_ByNtl_no(ByVal objModel) As Double

Dim sql As String = " select (isnull(sum(rec_frm_suppl_qty),0) +isnull(sum(ret_frm_knit_qty),0)+isnull(sum(ret_frm_subcont_qty),0)+isnull(sum(loan_frm_party),0))-((isnull(sum(iss_to_knitt_qty),0))+isnull(sum(iss_to_subcont_qty),0)+isnull(sum(ret_to_suppl_qty),0)+isnull(sum(loan_to_party),0)+sum(isnull(Sale,0))) as Balance from trans_yarn where ntl_no='" + objModel.ntl_order + "' and yarn_count='" + objModel.yarn_count + "' and yarn_type='" + objModel.yarn_type + "' and brand='" + objModel.brand + "' and lot_no ='" + objModel.lot_no + "' "

Dim dt_type As DataTable
Dim i As Double
dt_type = New DataTable("ViewYarnBalance")
da = New SqlDataAdapter(sql, conn)
conn.Open()
da.Fill(dt_type)
conn.Close()

Return i = dt_type.Rows(0)(0)

End Function

推荐答案

首先,停止这样做!

不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询:

Firstly, stop doing it like that!
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead:
Using con As New SqlConnection(strConnect)
	con.Open()
	Using da As New SqlDataAdapter("SELECT MyColumn1, MyColumn2 FROM myTable WHERE mySearchColumn = @SEARCH", con)
		da.SelectCommand.Parameters.AddWithValue("@SEARCH", myTextBox.Text)
		Dim dt As New DataTable()
		da.Fill(dt)
		...
	End Using
End Using



Se cond,它充满了SUM操作,它将返回null,你的列中有任何一个空值 - 所以任何单个null值都会导致你的总和为零。

看看你的data!


Second, that is stuffed full of SUM operations, which will return null is there is any one null value in your column - so any single value of null will cause your sum to be zero.
Look at your data!


从一开始你的方法就错了。通过串联从UI获取的字符串组成的查询。不仅重复的字符串连接是低效的(因为字符串是不可变的;我是否必须解释为什么它会使重复连接变坏?),但是有更重要的问题:它打开了通向良好的大门已知的漏洞称为 SQL注入



这是它的工作原理: http://xkcd.com/327



你明白了吗?从控件中获取的字符串可以是任何东西,包括......一段SQL代码。



怎么办?只需阅读有关此问题和主要补救措施:参数化语句 http://en.wikipedia.org/ wiki / SQL_injection



使用ADO.NET,使用:http://msdn.microsoft.com/en-us/library/ff648339.aspx



请参阅我过去的答案有更多细节:

在com.ExecuteNonQuery中更新EROR( );

嗨名字没有显示在名称中?



-SA
Your approach is wrong from the very beginning. The query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but there is way more important issue: it opens the doors to a well-known exploit called SQL injection.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

—SA


这篇关于从放入变量的SQL中检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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