我可以从asp.net mvc调用sql server中的函数吗?

时间:2016-11-03 17:00:54

标签: sql-server asp.net-mvc encryption

我在sql server中有以下语句:

EXEC StoredProcedureName "email", "pinNumber";

我在用MV#编写的ASP.NET MVC中有以下方法:

name:encryptPinNumber
input parameter (email,pinNumber)
return type: string
return variable: encryptedPinNumber

我可以在SQL Server中调用encryptPinNumber并给它发送电子邮件,pinNumber,然后将encryptedPinNumber存储在我的EXEC语句中吗?

EXEC StoredProcedureName "email", "encryptedPinNumber";

1 个答案:

答案 0 :(得分:0)

如果不是最优雅/最有效的方式如下:

private string encryptPinNumber(string email, string pinNumber)
{

    string connectionString = "Data Source=DBServerName;Initial Catalog=DBName;Integrated Security=true;";
    var con = new System.Data.SqlClient.SqlConnection(connectionString);
    var cmd = new System.Data.SqlClient.SqlCommand("StoredProcedureName", con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@email", email));
    cmd.Parameters.Add(new System.Data.SqlClient.SqlParameter("@pinNumber", pinNumber));
    con.Open();

    string encryptedpinNumber = cmd.ExecuteScalar().ToString();
    con.Close();

    return encryptedpinNumber;

}

但请参阅其他人已发布的链接。