更新数据出错!麻烦帮忙!

giantboy520 2006-01-07 10:51:01
我用这条语句更新太长了,有人提示用参数化更新数据,请问如何写?不用存储过程的如何写?
sql="update articles set title='"+title.Text+"',articler='"+articler.Text+"',url='"+url.Text+"',articles='"+articles.Text+"',leibie='"+leibie.SelectedValue.ToString()+"',images='"+Session["images"].ToString()+"' where id='"+Utility.GetParam("id")+"'";
...全文
80 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
jbeauty 2006-01-08
  • 打赏
  • 举报
回复
马甲收回分
giantboy520 2006-01-07
  • 打赏
  • 举报
回复
怎么这样写后总是提示
异常详细信息: System.Data.OleDb.OleDbException: 必须声明变量 '@title'。

sql="update articles set title=@title, articler=@articler,url=@url,articles=@articles,leibie=@leibie,images=@images";
SqlParameter[] Pars=new SqlParameter[6];
Pars[0]=new SqlParameter("@title",SqlDbType.Text);
Pars[1]=new SqlParameter("@articler",SqlDbType.VarChar,50);
Pars[2]=new SqlParameter("@url",SqlDbType.Text);
Pars[3]=new SqlParameter("@articles",SqlDbType.Text);
Pars[4]=new SqlParameter("@leibie",SqlDbType.Char,16);
Pars[5]=new SqlParameter("@images",SqlDbType.Text);
Pars[0].Value=temp1;
Pars[1].Value=temp2;
Pars[2].Value=temp3;
Pars[3].Value=temp4;
Pars[4].Value=temp5;
Pars[5].Value=temp6;
smartcoffee 2006-01-07
  • 打赏
  • 举报
回复
不用存储过程只能在程序中运态构造动态语句,如在.net中,使用命令对象和参数对象,采用字串替换方式处理.
smartcoffee 2006-01-07
  • 打赏
  • 举报
回复
sp_executesql [@stmt =] stmt
[
{, [@params =] N'@parameter_name data_type [,...n]' }
{, [@param1 =] 'value1' [,...n] }
]

下面示例中的存储过程动态生成并执行一个 INSERT 语句,向适当的表中插入新订单。该存储过程使用订单日期生成应包含数据的表的名称,然后将名称并入 INSERT 语句。(这是 sp_executesql 的一个简单示例。不包含错误检查,也不包括业务规则检查,例如确保两个表之间订单号没有重复。)

CREATE PROCEDURE InsertSales @PrmOrderID INT, @PrmCustomerID INT,
@PrmOrderDate DATETIME, @PrmDeliveryDate DATETIME
AS
DECLARE @InsertString NVARCHAR(500)
DECLARE @OrderMonth INT

-- Build the INSERT statement.
SET @InsertString = 'INSERT INTO ' +
/* Build the name of the table. */
SUBSTRING( DATENAME(mm, @PrmOrderDate), 1, 3) +
CAST(DATEPART(yy, @PrmOrderDate) AS CHAR(4) ) +
'Sales' +
/* Build a VALUES clause. */
' VALUES (@InsOrderID, @InsCustID, @InsOrdDate,' +
' @InsOrdMonth, @InsDelDate)'

/* Set the value to use for the order month because
functions are not allowed in the sp_executesql parameter
list. */
SET @OrderMonth = DATEPART(mm, @PrmOrderDate)

EXEC sp_executesql @InsertString,
N'@InsOrderID INT, @InsCustID INT, @InsOrdDate DATETIME,
@InsOrdMonth INT, @InsDelDate DATETIME',
@PrmOrderID, @PrmCustomerID, @PrmOrderDate,
@OrderMonth, @PrmDeliveryDate

GO

fyming 2006-01-07
  • 打赏
  • 举报
回复
如果不用存储过程那么用参数化改写没有意义
giantboy520 2006-01-07
  • 打赏
  • 举报
回复
自己搞定啦!

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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