更新存储在MSSQL数据库ntext或nvarchar列中的长字符串的某个部分 [英] Update a certain part of a long string stored in MSSQL database ntext or nvarchar column

查看:119
本文介绍了更新存储在MSSQL数据库ntext或nvarchar列中的长字符串的某个部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题可能与我之前的问题有关

如何使用该字符串中的标记更新长字符串的某个部分 [ ^ ]

由OriginalGriff出色地解决了,虽然这个我将在MsSql数据库的表中放置相同格式的字符串。



所以这是我的case,我有这个服务器客户端应用程序,我需要为它做CRUD实用程序,它使用MSSQL数据库,并有一个包含ntext列的主表,现在这个应用程序大多数时间读取ntext列,几乎不使用其他字段,但是ntext列的内容来自它旁边的字段。例如,一个包含六个字段pkfield,field1,field2,field3,field4和field5的表,其中field5的内容将类似于:

This question maybe related to my previous question
How to I update a certain part of a long string using a tag within that string[^]
which was brilliantly solved by OriginalGriff, though this one I would be placing the same format of string in a table within a MsSql Database.

So here's my case, I have this server-client application that I need to make CRUD utility for, it uses MSSQL database and has a main table that contains a ntext column, now this application reads the ntext column most of the time and barely uses the other fields, but the contents of the ntext column came from the fields next to it. For instance a table that has six fields pkfield, field1, field2, field3, field4, and field5 where the contents of field5 will be something like:

[pkfield:"value",field1:"value",field3:null,fied4:"value"]



现在如果我需要更新field1,2或3我还需要更新他们在field5中的价值



i可以使用RegEx解决方案,也可以通过mssql中的原始脚本看起来像这样


now if i need to update either field1,2,or 3 i also need to update their value in field5

i can do this with the RegEx solution, also via raw script in mssql that looks like this

update table1 set
field5 = replace( convert(varchar(5000), field5), 'field5:"value"', 'field5:"newvalue that can be very long string"' )
where pkfield='sdfajklfsdjfl'





我需要知道的是如果还有其他更简单的更新方法,顺便说一下我使用实体框架连接到我的数据库。



提前感谢



what i need to know is if there are other simpler method for an update, by the way I'm using entity framework to connect to my database.

thanks in advance

推荐答案

因为field5值可以从其他列值的值生成,我认为您不需要在您的数据库中存储field5。



例如



since field5 value can generate from the values of other column values, I think you don't need to store field5 in your database.

for example

string field5 = string.Format("[pkfield:'{0}',field1:'{1}',field3:'{2}',fied4:'{3}']", pkfield,field1,field3, fied4);





将生成field5所需的字符串。



然后你不需要复杂的正则表达式或字符串操作来进行更新。您只需要在where where条件中设置其中一列的值。



will generate the string you need as field5.

Then you don't need to to complex regex or string manipulation for updating. You only need to set value of one of the column with where condition.


这篇关于更新存储在MSSQL数据库ntext或nvarchar列中的长字符串的某个部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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