如何将C#代码转换为SQL Server存储过程? [英] How to convert C# code into SQL server stored procedure?

查看:169
本文介绍了如何将C#代码转换为SQL Server存储过程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我试图将C#函数转换为sql server存储过程

i有ac#code,我想转换sql存储过程

i尝试了很多方法,但我无法获得输出。



以下是我的c#代码:



Hi All,

Am trying to Convert C# Function into sql server stored procedure
i have a c# code, i want to convert sql stored procedure
i have tried many ways but i couldn't get the out put.

below is my c# code:

protected void Page_Load(object sender, EventArgs e)
  {
      string S = "pass";
      string str2 = "";
      int num2 = 1;
      int num3 = S.Length;
      for (int i = 0; i < num3; i++)
      {
          str2 = str2 + Convert.ToChar(Convert.ToChar(S.Substring(i, 1)) + ((i + 1) * num2));
          num2 *= -1;
      }
      return str2;

  }









我上面的输出是=





My Output for the above one is =

q_vo







那么什么我想要的是如何将上面的c#代码转换成sql存储过程。



如果你知道请为此解决问题。



提前致谢,



我尝试过:






So what i want is how to convert the above c# code into sql stored procedure.

if you know please drop a solution for this.

Thanks in advance,

What I have tried:

how to convert c# code into sql server stored procedure?

推荐答案

在这种情况下它可以完成它只是字符串操作。主要挑战是将文本转换为ascii代码并返回。 SQL 2008具有以下功能,可以将字符转换为等效的ascii代码

MSDN:ASCII (Transact-SQL)

MSDN:CHAR(Transact-SQL )



您可以使用此片段的内容来创建存储过程

In this case it can be done as its just string manipulation. Main challenge is the conversion of text to ascii code and back. SQL 2008 has following functions that can convert character to equivalent ascii code
MSDN: ASCII (Transact-SQL)
MSDN: CHAR (Transact-SQL)

You can use the guts of this snippet to create the stored procedure
DECLARE @input VARCHAR(50)
DECLARE @output VARCHAR(50)
DECLARE @length INT
DECLARE @i INT
DECLARE @tmp INT
DECLARE @num2 INT

SET @input = 'pass'

-- set defaults for output and internal variables
SELECT @output = '', @i = 1, @num2=1, @length = LEN(@input)

-- made slight change in formula as indexes in sql string arrays start at 1
WHILE @i <= @length
BEGIN
	SET @tmp = ascii(SUBSTRING(@input,@i,1))
	SET @tmp = @tmp + (@i*@num2)
	SET @num2 = @num2 * -1
	SET @output = @output + char(@tmp)

	SET @i = @i+1
end

SELECT @output





如果您需要知道如何创建存储过程,可以看一下:

创建过程(Transact-SQL)


我不建议直接转换C#代码,如果它没有数据依赖性&直接转换它也会导致未知的麻烦。我猜SQL支持CLR(取决于你使用它的版本),文章这里应该能够帮助你。但再次与性能进行权衡。
I would not recommend converting C# code directly if it does not have data dependency & converting it directly can also lead to unknown trouble. I guess SQL supports CLR (depending on version you are using it), Article here should be able to help you on that. But again its tradeoff with performance.


类ValidateSAID {



public int check(String id){

id = id.Trim();

if(!Regex.IsMatch(id,@[0-9] +)){

返回 - 1;

}

if(id.Length!= 10){

返回-1;

}

int type =(int)(id [0] - '0');

if(type!= 2&& type!= 1){

返回-1;

}

int sum = 0;

for(int i = 0; i< ; 10; i ++){

if(i%2 == 0){

String ZFOdd =((int)(id [i] - '0')* 2).ToString()。PadLeft(2,'0');

sum + =(int)(ZFOdd [0] - '0')+(int)(ZFOdd [1] - '0');

} else {

sum + =(int)(id [i] - '0');

}

}

返回(总和%10!= 0)? -1:输入;



}



}
class ValidateSAID {

public int check(String id) {
id = id.Trim();
if (!Regex.IsMatch(id, @"[0-9]+")) {
return -1;
}
if (id.Length != 10) {
return -1;
}
int type = (int)(id[0]-'0');
if (type != 2 && type != 1) {
return -1;
}
int sum = 0;
for (int i = 0; i < 10; i++) {
if (i % 2 == 0) {
String ZFOdd = ((int)(id[i]-'0') * 2).ToString().PadLeft(2, '0');
sum += (int)(ZFOdd[0]-'0') + (int)(ZFOdd[1]-'0');
} else {
sum += (int)(id[i]-'0');
}
}
return (sum % 10 != 0) ? -1 : type;

}

}


这篇关于如何将C#代码转换为SQL Server存储过程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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