sqlserver表列字段逗号转行_SQLServer逗号分隔的字符串转换成表

案例背景说明:系统采用

B/S

架构,现有产品表(

Product

)

,其结构如下:

序号

字段名称

字段类型(长度)

备注

ID

Int

IDENTITY

(

1

,

1

)NOTNULL

ProductCode

Varchar(30)

ProductName

NVarchar(100)

State

Int

数据状态(

0-

未审核,

1-

已审核)

在客户端,请求将批量选中的行标识为【审核通过】

我们可以这样做,将批量选中的行的

ID

用逗号分隔连接成一个

ID

串,发送到服务端,服务

端进行处理。处理代码大致如下(和上述步骤对应)

publicstatic

int

ShenHe(

string

ids,

string

tableName)

{

using

(

SqlConnection

conn =

new

SqlConnection

(connectionString))

{

conn.Open();

{

SqlCommand

cmd =

new

SqlCommand

();

try

{

cmd.Connection = conn;

StringBuilder

cmdText =

new

StringBuilder

();

cmdText.AppendFormat(

"create table #tmp_table(ID int);"

);

string

[] idAry = ids.Split(

','

);

foreach

(

string

id

in

idAry)

{

cmdText.AppendFormat(

"\ninsert into #tmp_table(ID) values({0});"

, id);

}

cmdText.AppendFormat(

"\nupdate

{0}

set

State

=

1

where

ID

in

(select

ID

from

#

tmp_table

);"

,

tableName);

cmdText.AppendFormat(

"\ndrop table #tmp_table;");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值