在保持数字部分不变的同时仅更新无脂部分? [英] update only the aphabhetic part while keeping the numeric part constant?

查看:74
本文介绍了在保持数字部分不变的同时仅更新无脂部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在更新到数据库时,我陷入了一个小项目.

我在书本表中有"book-0001","book-0002","book-0003"等数据,其中alpha部分将是相同的,数字部分将自动递增.
例如下一个数据可以是"copy-0004","copy-0005"

而更新条件是,对于包含"book"的所有项目,只需将字母部分从"book"更改为"abcd",其数字部分将与"0001"相同.
例如 更新后应该像
"abcd-0001","abcd-0002","abcd-0003"


任何有关的帮助将不胜感激.


在此先感谢:)

i am stuck with a small project while updating into database.

i have data as " book-0001", "book-0002", "book-0003" in the book table where alpha part would be same and numeric part is auto incremented.
for eg next data could be "copy-0004", "copy-0005"

while updating condition is Only the alphabetic part has to be changed like "book" to "abcd" for all items containing "book" and its numeric part would be same as "0001".
for eg.
after update it should be like
"abcd-0001", "abcd-0002" , "abcd-0003"


any help regarding would be greatly appreciated.


thanks in advance :)

推荐答案

UPDATE yourtable SET yourcolumn = REPLACE ( yourcolumn , "book" , "abcd" )

http://msdn.microsoft.com/en-us/library/ms186862.aspx [ ^ ]
UPDATE yourtable SET yourcolumn = REPLACE ( yourcolumn , "book" , "abcd" )

http://msdn.microsoft.com/en-us/library/ms186862.aspx[^]


我也获得了子字符串.但是问题是仅在保留数字部分的情况下才更新Alpha部分.

同时仅更新书"部分.

就像
"book-0001","book-0002"

之后 更新
"bok1-0001","bok1-0002"
在我的情况下,alpha部分的长度是恒定的.
数字部分会自动递增.

此数据是从先前形式的datagridview中获取的. N个值需要以另一种形式更新.
应该对包含"book"的所有项目运行更新查询.

这是从datagrid读取数据的代码片段
SqlConnection cin =新的SqlConnection(str);
cin.Open();
SqlCommand cmd =新的SqlCommand(从书中选择代码,其中ISBN =""+ selectedrow +"",cin); //代码为"book-0001,book-0002"
//字符串codebook = cmd.ExecuteScalar().ToString();
SqlDataReader dr = cmd.ExecuteReader();


字符串codebook =";
字符串速率=";
while(dr.Read()&& Dr.HasRows)
{
codebook + = dr ["code"].ToString()+"\ n";
}

字符串alphaedit = codebook.Substring(0,8);
this.textBox_Bookcode.Text = alphaedit;
字符串numedit = codebook.Substring(9,4);
this.textBox_numcode.Text = numedit;

现在我将代码(即book-0001)更新为bookcodefull

字符串bookcodefull = textBox_Bookcode.Text +-" + numedit;
//textbox_bookcode是textbox =,其中用户输入用于Cook更新的新名称


在这样做的同时
我的结果是

"cook-0001"
"cook-0001"

但必须是
"cook-0001"
"cook-0002"
i have obtained the sub string too. But problem is updating the alpha part only n preserving the numeric part.

while updating only the "book" part should be changed.

like
"book-0001","book-0002"
after
update
"bok1-0001", "bok1-0002"
and the length for alpha part is constant in my case.
and the numerical part is auto incremented.

this data is fetched from datagridview of previous form. N values need to updated in another form.
update query should be run for all item containing "book".

here is snippet of the code to fectch the data from datagrid
SqlConnection cin = new SqlConnection(str);
cin.Open();
SqlCommand cmd = new SqlCommand("select code from book where ISBN=''" + selectedrow + "''", cin); //code is "book-0001, book-0002"
// string codebook = cmd.ExecuteScalar().ToString();
SqlDataReader dr = cmd.ExecuteReader();


string codebook = "";
string rate = "";
while (dr.Read() && dr.HasRows)
{
codebook += dr["code"].ToString() + "\n";
}

string alphaedit = codebook.Substring(0, 8);
this.textBox_Bookcode.Text = alphaedit;
string numedit = codebook.Substring(9, 4);
this.textBox_numcode.Text = numedit;

now i updated the code(ie book-0001) as bookcodefull

string bookcodefull = textBox_Bookcode.Text + "-" + numedit;
// textbox_bookcode is textbox= where user input new name for update for cook


while doing so
my result was

"cook-0001"
"cook-0001"

but required is
"cook-0001"
"cook-0002"


感谢大家的答复.

替换工作:)
thank you all for the replies.

Replace worked :)


这篇关于在保持数字部分不变的同时仅更新无脂部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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