|
|
Hello,
I frequently write many SQLs and would like to embed in C# code (SSMS 2005/VS.NET 2005). There was macro which would let me select the code and hit a shortcut to format it into C# code. Has anyone seen where it is now?
Thanks,
Raghu.
You can never try. You either do it or you don't.
|
|
|
|
|
Do I understand that you want a macro to copy SQL statements into a C# file?
I thought it was Ctrl+C, Ctrl+V
only two letters away from being an asset
|
|
|
|
|
Hmm... haven't heard that before, but I'll give it a try...
I was referencing converting a SQL statement to C# string...
But, here's what I came up with in case someone needs this... Assign this macro a short cut of of Ctl+Shift+F3
It converts
select *
From Status
From ActivityStatus ActStsId=StsId
to
string sql = " select * "+
" From Status "+
" From ActivityStatus ActStsId=StsId";
Macro is as follows.
Sub SQL2CSharp()
Dim selection As EnvDTE.TextSelection
Dim startPoint As EnvDTE.EditPoint
Dim endPoint As TextPoint
Dim quoteStart As String
selection = DTE.ActiveDocument.Selection()
startPoint = selection.TopPoint.CreateEditPoint()
endPoint = selection.BottomPoint
quoteStart = """"
DTE.UndoContext.Open("SQL 2 CSharp")
Try
startPoint.Insert("string sql = """)
Do While (True)
Dim line As Integer
line = startPoint.Line
startPoint.Insert(quoteStart)
startPoint.EndOfLine()
If (line = endPoint.Line) Then
startPoint.Insert(quoteStart & ";")
Else
startPoint.Insert(quoteStart & "+")
End If
startPoint.LineDown()
startPoint.StartOfLine()
If (line = endPoint.Line) Then
Exit Do
End If
Loop
Finally
' If an error occurred, then make sure that the undo context is cleaned up.
' Otherwise, the editor can be left in a perpetual undo context.
DTE.UndoContext.Close()
End Try
End Sub
You can never try. You either do it or you don't.
|
|
|
|
|
Why would anyone need or want this? You SHOULD NOT be using inline SQL at all.
only two letters away from being an asset
|
|
|
|
|
Are you suggesting I should use the alternatives for SQL like SPs, functions, views etc?
You can never try. You either do it or you don't.
|
|
|
|
|
Only if you want your applications to perform better, be more maintainable and more secure
only two letters away from being an asset
|
|
|
|
|
And why the hell would we want that?!
|
|
|
|
|
Why would this be insecure if there is no WHERE clause associated with the SQL statement?
Depending on if it is a reusable/non-reusable SQL statement, I think its a personal preference to keep the SQL in code or move it to database.
You can never try. You either do it or you don't.
|
|
|
|
|
If you have to ask it shows you really have no idea what your doing. Help make this profession better, drop out now.
only two letters away from being an asset
|
|
|
|
|
I never would've thought this kind of comment would come from a CodeProject supporter. I'm just thinking about this kind of response in general is what makes a good or bad forum.
Now, if you can please answer the question (which was a genuine one), everyone who reads this thread now and a few years from now will learn something.
You can never try. You either do it or you don't.
|
|
|
|
|
|
Mark,
We are going on a tangent here.
I understand the questions in the threads you posted are coming from novice programmers, they posted in an entirely irrelevent forum, and that they didn't do their homework. People do that all the time, every minute, every hour, every day.
I see no relation to the question on hand.
You can never try. You either do it or you don't.
|
|
|
|
|
No, you should be using LINQ, but short of that, there is nothing wrong with parametrised inline SQL.
|
|
|
|
|
A hearty debate but the general consensus is to use stored procs for a number of benefits.
only two letters away from being an asset
|
|
|
|
|
|
string sql = @"
select *
From Status
From ActivityStatus ActStsId=StsId";
Putting an @ in front of a string makes it a multi-line string. Whitespace is preserved (including newlines). If you had a quote character in your SQL, you'd have to escape it by typing a two of them (you could do this with a quick find-replace in Notepad before pasting into C#).
But like others have said, typically a bad idea to put SQL in C# code. Some environments don't have stored procedures (like SQL Server CE), in which case I'd recommend storing the SQL in your settings as a string setting. You can then reference it by the setting name in your code.
Visual Studio is an excellent GUIIDE.
|
|
|
|
|
aspdotnetdev wrote: Putting an @ in front of a string makes it a multi-line string.
Thank you, I rarely use sql strings and when I do it is a PITA adding in the white space formatting that makes it easier to debug. Learn something every day.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I'm trying to compile my project but its giving me this error:
1. The namespace 'PenDriveBackupProgram' already contains a definition for 'PenDriveBackup'
Prior to this error I was trying to get this class working in my project:
http://downloads.cfdan.com/csharp/us...lnumber.cs.txt
I was calling the class like so:
USBDriveSerialNumber.USBSerialNumber usb = new USBDriveSerialNumber.USBSerialNumber();
string serial = usb.getSerialNumberFromDriveLetter(path);
MessageBox.Show(serial);
But now there is an error in the class which is appearing at runtime?
The error is with this bit of code;
ManagementObjectSearcher searcher1 = new ManagementObjectSearcher("SELECT * FROM Win32_LogicalDiskToPartition");
foreach (ManagementObject dm in searcher1.Get())
And the error is:
Context 0x33c3ff0 is disconnected. No proxy will be used to service the request on the COM component. This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the application is completely done with the RuntimeCallableWrappers that represent COM components that live inside them.
I read some information at MSDN about the project needing full trust, so I went into the project settings -> security -> Enabled ClickOnce Security Settings -> Selected This is a full trust application.
Although I set the global settings to Full Trust the class has the modifer Partial so I figured this could be a problem (First project in C#)
So I removed the partial modifier with everything to do with the PenDriveBackup class which now results in the error you see above.
All help is appreciated greatly, cheers, George.
|
|
|
|
|
Hi,
there seem to be a couple of issues.
the PenDriveBackup error means you have more than one type of that name; that could be classes, structs, enums, etc. Or even two copies of the same code (did you recently copy/paste PenDriveBackup code?).
the Context disconnected error is a run-time error (I don't get how you could get it as you are getting the previous error at compile time, so your app can't run). Are you using threads, and/or doing things regarding AppartmentState? You're not the first today having this problem, I personally never have seen it happen. It could be a COM component referring to a managed object that is no longer around.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
Firstly thanks for your reply Luc, I appreciate it. Now, there must of been some confusion, I guess I didnt state it correctly.
The COM error that I showed here is an error I got when my code was (apparently working, besides that run-time error) and I seeked online for suggestions on possible solutions and I came across that the program needs Full Trust for it to work. So I changed the project to full trust in project settings and yet the error still persisted. I was able to run the program at this point.
I was then stumped, had no idea what to do until I saw that my main class 'PenDriveBackup' (which resides in Main.cs) had the modifiers 'public partial'.
So the class looked like 'public partial PenDriveBackup : Form'. I removed the partial from the class and went into my Main.Designer.cs where I removed the partial from that class too which then went from 'public partial class PenDriveBackup' to 'public class PenDriveBackup'.
Upon doing this the error, "Error1 The namespace 'PenDriveBackupProgram' already contains a definition for 'PenDriveBackup'" occurs unabling me to compile and that is the current state im in.
My project consists of five items;
Main.cs -> Where most of my code sits.
MessageBoxForm.cs -> Custom message form, used to determine either incremental or full backup.
InputBoxForm.cs -> Custom input box, used to determine where the user wants to backup.
USBSerialNumber.cs -> A class file downloaded from the internet.
And ofcourse, Program.cs
Using the Find function I managed to see which items included the class PenDriveBackup.
Main.cs -> One class, being PenDriveBackup
-> Main.Designer.cs -> A class in here is called PenDriveBackup but as it belongs to Main.cs it can't be the problem?
MessageBoxForm.cs -> Found no class called PenDriveBackup
InputBoxForm.cs ->; Found no class called PenDriveBackup
USBSerialNumber.cs ->; Found no class called PenDriveBackup
I hope I have provided suffecient information to help you, I have been tearing my hair out for the last few days.
Oh and something that may help, if I were to build the USBSerialNumber.cs form in its own project it works. Thats why I thought the modifier partial was causing the problem.
Thanks again Luc!
|
|
|
|
|
OK
1. initially a C# class had to reside in a single file; which also meant Visual Designer, which actually generates code, was modifying your form's C# source file. Then they introduced the notion of partial classes, which means you add the partial keyword to a class, and now can split the class over several files. Since then, Visual Designer has its myForm.Designer.cs file which you shouldn't touch, and you have your myForm.cs file which VD won't touch; they both contain a "public partial class myForm" statement, and the compiler understands the two or more parts have to be joined together to create one class. When you remove the "partial" keywords, that no longer works; the compiler now sees two different classes with the same name, and rightfully complains. You have to reinstate the partials.
2. I don't know about your original problem; I'm not inclined to go with the "full trust" issue.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
FWIW: Here[^] is the thread mentioning a context problem I read earlier.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? 59.24% waren verstandig genoeg om NEEN te stemmen; bye bye viaduct.
|
|
|
|
|
Thanks once again for your Reply Luc.
I have now fixed the code in regards to the partial modifier and I understand how that works now. I can compile my project yet the runtime error still persists ofcourse.
Looking at the link you gave me it looks as though the problem is something related to a thread however I do not explicity state any threads in my project thus im confident when I say no threads are being used.
In regards to the AppartmentState I also am not using this.
Thanks George.
|
|
|
|
|
i hear that there 's classes for replication in vs 2008
i need that classes and simple example for it
becuase iam in case which can't replicate direct from sql server
i need help
thanks
md_refay
|
|
|
|