[英]Best Approach SQL Server Calculated Column vs In Code Approach
I currently have a CRUD ASP.NET MVC application that connects to SQL Server 2012. In the end the application exports data to a form. 我目前有一个连接到SQL Server 2012的CRUD ASP.NET MVC应用程序。最后,该应用程序将数据导出到表单。 A field on the form is called form number
which I would like to be M-'Primary Key'
. 表单上的一个字段称为form number
,我想成为M-'Primary Key'
。
Would it be best to add the M-
in the C# code or would a calculated column be the best approach? 最好在C#代码中添加M-
还是计算列是最佳方法? (add another column with M-'PK'
). (用M-'PK'
添加另一列)。
If there are any other (better) options let me know! 如果还有其他(更好)的选择,请告诉我!
An example: 一个例子:
Primary key: 1123
主键: 1123
Output on form 表格输出
Form Number: M-1123
See example in C: 请参阅C中的示例:
private DataSet selectID(string studentId)
{
SqlConnection conn = "connecttoserverstring";
SqlCommand cmd = new SqlCommand("select 'M-' + cast(ID as varchar(10)) from table, conn);
SqlParameter p = new SqlParameter("@id", studentId);
}
Even that the id is a int we cast it into a varchar
so we can concatenate
it with the 'M-' or what ever you want ! 即使id是一个int,我们也将其转换为varchar
因此我们可以concatenate
其与'M-'或任何您想要的内容连接起来!
Simple example of the output is : - run this in SQL Server. 输出的简单示例是 :-在SQL Server中运行它。
select 'M-' + cast(1 as varchar(10));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.