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. A field on the form is called form number which I would like to be M-'Primary Key'.

Would it be best to add the M- in the C# code or would a calculated column be the best approach? (add another column with M-'PK').

If there are any other (better) options let me know!

An example:

Primary key: 1123

Output on form

Form Number: M-1123
有帮助吗?

解决方案

See example in 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 !
Simple example of the output is : - run this in SQL Server.

select 'M-' + cast(1 as varchar(10));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top