Placeholders in C#?
-
Are there anything like C++ "%s" or "%f" in C#? Essentially if you have a big query and you have 10 placeholders, what is the cleanest way of filling it up...for instance, in MFC you could do , LPCSTR lpQuery = "EXEC MySP %s, %s,%s"; CString szQuery; szQuery.Format("a", "b", "c"); which is clean and simple....how would you do something like this in c#?
-
Are there anything like C++ "%s" or "%f" in C#? Essentially if you have a big query and you have 10 placeholders, what is the cleanest way of filling it up...for instance, in MFC you could do , LPCSTR lpQuery = "EXEC MySP %s, %s,%s"; CString szQuery; szQuery.Format("a", "b", "c"); which is clean and simple....how would you do something like this in c#?
You can do a similar thing in C# with the String.Format() method. See: http://search.microsoft.com/gomsuri.asp?n=3&c=rp\_Results& siteid=us/dev&target=http://msdn.microsoft.com/library/en-us/ cpref/html/frlrfSystemStringClassFormatTopic.asp The long and short of it is this: sQuery = String.Format("EXEC MySP {0}", sSPVariable); Steve
-
Are there anything like C++ "%s" or "%f" in C#? Essentially if you have a big query and you have 10 placeholders, what is the cleanest way of filling it up...for instance, in MFC you could do , LPCSTR lpQuery = "EXEC MySP %s, %s,%s"; CString szQuery; szQuery.Format("a", "b", "c"); which is clean and simple....how would you do something like this in c#?
This kind of code can lead to some bugs and security issues. If you want to keep with it or for uses other than SQL queries, use String.Format(), or, for better performance and solve the bugs I've mentioned, use classes like SqlCommand and SqlParameter. lazy isn't my middle name.. its my first.. people just keep calling me Mel cause that's what they put on my drivers license. - Mel Feik