有人可以帮助我解决使用参数化查询的问题吗? [英] Please can someone help me with some light on using parameterized query?

查看:70
本文介绍了有人可以帮助我解决使用参数化查询的问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何在SQL Server CE上使用ADO进行参数化查询。

我的问题是,在将参数附加到参数集合之后,comnand对象如何告诉每个参数的位置查询。



假设我有以下查询:





INSERT INTO客户(CustomerName,CustomerAddr,CustomerCity)值(?,?,?)



它如何告知参数的orde,因为没有给出关于参数的信息这是在创建参数时以及它们是appendes时。或者它是从参数的顺序推导出来的。如果是的话,必须在编写本文时遵守该命令。即在示例查询中。上面,字段名称是否必须按参数的附加顺序排序?

解决方案

它通过必须按该顺序指定参数值来告诉顺序。



但是,更强大的解决方案是使用命名参数,例如

 SqlCeCommand cmd = < span class =code-keyword> new  SqlCeCommand(  INSERT INTO Customers(CustomerName,CustomerAddr,CustomerCity) )值(@ name,@ addr,@ city)); 
cmd.Parameters.Add( @ name,SqlDbType.NVarChar, 120 )。值= 客户名称;
cmd.Parameters.Add( @ addr,SqlDbType.NVarChar, 120 )。值= 客户地址;
cmd.Parameters.Add( @ city,SqlDbType.NVarChar, 120 )。值= 客户城市;


I am learning how to use parameterized query using ADO on SQL Server CE.
My question is, after appending parameters to the parameters collection, how does the comnand object tell the position of each parameter in the query.

Let's say I have the following query:


INSERT INTO Customers (CustomerName,CustomerAddr,CustomerCity) values(? , ?, ?)

How does it tell the orde of the paramers since no information is given about this when parameters were created and when they are appendes.Or does it deduce it from the order in which the parameters are. added?If yes, must that order be observed in writing the quety. I.e. in the example query. above , must the fields name be ordered in the order in which the parameters were appended?

解决方案

It tells the order by having to specify the parameter values in that order.

However, a more robust solution would be to use named parameters, e.g.

SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Customers (CustomerName,CustomerAddr,CustomerCity) values(@name , @addr, @city)");
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 120).Value = "customer name";
cmd.Parameters.Add("@addr", SqlDbType.NVarChar, 120).Value = "customer address";
cmd.Parameters.Add("@city", SqlDbType.NVarChar, 120).Value = "customer city";


这篇关于有人可以帮助我解决使用参数化查询的问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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