 |
|
 |
How about a SQL stored procedure generator and c# DAL generator for almost nothing! Then check out my www.dashSQL.com. dashSQL generaters all the code for stored procedures and as well as a base data access layer and then another c# DAL class that gets created per your table. No need for parameter types, int, varchar, etc. All parameters in the dashSQL code generator are strings!! SQL automatically converts everything for you! Give it a try!
www.dashsql.com
|
|
|
|
 |
|
 |
My issue with SubSonic, nHibernate and CodeAuthor is tha fact that they use adhoc or parameterized queries.
Using Stored Procedures is more secure than using adhoc queries or even parameterized queries via the command builder.
The security problem that this creates is that you are forced to assign SELECT permissions for tables, to the account that is running the queries.
What you really want is to assign EXEC permissions against your stored procedures. That way, users only get access to the data you package for them, not access to the entire table.
Here's a c# generator that also uses stored procedures exclusively:
OxyGen Code
http://www.techinceptions.com/codegenerator.html
You get pure commented c# code, paging (data source paging), caching etc.
And no dlls (but you can choose an option to compile the output).
The data is presented as generic lists or class instances, and there's special handling for static tables as well.
|
|
|
|
 |
|
 |
It looks like a great product.
Thanks for the link.
"In the end it's a little boy expressing himself." Yanni
|
|
|
|
 |
|
 |
This is really sweet but has anyone used this with an output variable set by ScopeIdentity? If so could someone please post an example of how to do this.
Thanks.!
Chaci Mulch
|
|
|
|
 |
|
 |
Here's what would be ideal for me:
A Visual Studio add-in that would allow you to generate C# calling code for a sproc simply by right-clicking the sproc in the server explorer and selecting "generate C# method". It would then analyze the sproc and generate a corresponding C# static class with methods to execute the sproc, get the SqlParameter[] for the sproc, overloads to execute the sproc with a specified connection, transaction, etc...
For example, say we have
ALTER PROC AddNewSignature (@Description varchar(255), @UserID uniqueidentifier, @Signature text, @EmailAccountID uniqueidentifier = null)
as
DECLARE @GUID uniqueidentifier
SELECT @GUID = newid()
INSERT SignatureTable (ID,Description,UserID,Signature)
SELECT @GUID,@Description, @UserID, @Signature
exec AssociateSignatureToMailAccount @SignatureID = @GUID, @MailAccID = @EmailAccountID
END
I want it to generate a C# static class containing methods like
public static void AddNewSignature(string description, Guid userID, string signature, Guid emailAccountID)
{
}
The method would have overloads for supplying connection, transactions, etc. Additionally, there would be more methods in the class that would just generate the SqlCommand with parameters and connection, allowing the consumer code to actually perform the execution. This would be important as many data tiers have custom execution with custom error handling.
Tech, life, family, faith: Give me a visit.
I'm currently blogging about: Connor's Christmas Spectacular!
Judah Himango
|
|
|
|
 |
|
|
 |
|
 |
CREATE PROCEDURE tools_CS_SPROC_Builder_2005 ( @objName nvarchar(100), @Executetype nvarchar(100) -- ExecuteNonQuery -or- ExecuteReader -or- ExecuteScalar ) AS /* ___________________________________________________________________ Name: CS SPROC Builder Version: 1 Date: 10/09/2004 Author: Paul McKenzie Description: Call this stored procedue passing the name of your database object that you wish to insert/update from .NET (C#) and the code returns code to copy and paste into your application. This version is for use with "Microsoft Data Application Block". Version: 1.1 Date: 17/02/2006 Author: Paul McKenzie Description: a) Updated to include 'UniqueIdentifier' Data Type b) Support for 'ParameterDirection.Output' */ SET NOCOUNT ON DECLARE @parameterCount int DECLARE @errMsg varchar(100) DECLARE @parameterAt varchar(1) DECLARE @connName varchar(100) DECLARE @outputValues varchar(100) --Change the following variable to the name of your connection instance SET @connName='ConfigurationSettings.AppSettings("connectionString")' SET @parameterAt='' SET @outputValues='' SELECT dbo.sysobjects.name AS ObjName, dbo.sysobjects.xtype AS ObjType, REPLACE(dbo.syscolumns.name,'@','') AS ColName, dbo.syscolumns.colorder AS ColOrder, dbo.syscolumns.length AS ColLen, dbo.syscolumns.colstat AS ColKey, dbo.syscolumns.isoutparam AS ColIsOut, dbo.systypes.xtype INTO #t_obj FROM dbo.syscolumns INNER JOIN dbo.sysobjects ON dbo.syscolumns.id = dbo.sysobjects.id INNER JOIN dbo.systypes ON dbo.syscolumns.xtype = dbo.systypes.xtype WHERE (dbo.sysobjects.name = @objName) AND (dbo.systypes.status <> 1) ORDER BY dbo.sysobjects.name, dbo.syscolumns.colorder SET @parameterCount=(SELECT count(*) FROM #t_obj) IF(@parameterCount<1) SET @errMsg='No Parameters/Fields found for ' + @objName IF(@errMsg is null) BEGIN DECLARE @source_name nvarchar,@source_type varchar, @col_name nvarchar(100),@col_order int,@col_type varchar(20), @col_len int,@col_key int,@col_xtype int,@col_redef varchar(20), @col_isout tinyint IF @ExecuteType = 'ExecuteNonQuery' BEGIN PRINT 'SUB '+@objName+'( _' END IF @ExecuteType = 'ExecuteReader' BEGIN PRINT 'FUNCTION '+@objName+'( _' END IF @ExecuteType = 'ExecuteScalar' BEGIN PRINT 'FUNCTION '+@objName+'( _' END
declare @parameterOut nvarchar(1000) DECLARE cur CURSOR FOR SELECT * FROM #t_obj OPEN cur -- Perform the first fetch. FETCH NEXT FROM cur INTO @source_name,@source_type,@col_name,@col_order,@col_len,@col_key,@col_isout,@col_xtype --if(@source_type=N'U') SET @parameterAt='' -- Check @@FETCH_STATUS to see if there are any more rows to fetch. WHILE @@FETCH_STATUS = 0 BEGIN SET @col_redef=(SELECT CASE @col_xtype WHEN 34 THEN 'Binary' WHEN 35 THEN 'String' WHEN 36 THEN 'Guid' WHEN 48 THEN 'Byte' WHEN 52 THEN 'Int16' WHEN 56 THEN 'Int32' WHEN 58 THEN 'DateTime' WHEN 59 THEN 'Single' WHEN 60 THEN 'Currency' WHEN 61 THEN 'DateTime' WHEN 62 THEN 'Double' WHEN 99 THEN 'String' WHEN 104 THEN 'Boolean' WHEN 106 THEN 'Decimal' WHEN 122 THEN 'Currency' WHEN 127 THEN 'Int64' WHEN 165 THEN 'Binary' WHEN 167 THEN 'String' WHEN 173 THEN 'Binary' WHEN 175 THEN 'String' WHEN 231 THEN 'String' WHEN 239 THEN 'String' ELSE '!MISSING' END AS C) --capture parameter select @parameterOut = ' ByVal ' + @col_name+ ' AS '+@col_redef -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM cur INTO @source_name,@source_type,@col_name,@col_order, @col_len,@col_key,@col_isout,@col_xtype IF @@FETCH_STATUS<>0 BEGIN PRINT @parameterOut + ' _' END IF @@FETCH_STATUS=0 BEGIN PRINT @parameterOut+', _' END END CLOSE cur DEALLOCATE cur IF @ExecuteType = 'ExecuteNonQuery' BEGIN PRINT ')' END IF @ExecuteType = 'ExecuteReader' BEGIN PRINT ') AS SQLDATAREADER' END IF @ExecuteType = 'ExecuteScalar' BEGIN PRINT ') AS ?' END
PRINT '' PRINT ' Dim db As Database = DatabaseFactory.CreateDatabase() ' PRINT ' Dim sqlCommand As String = "'+@objName+'" ' PRINT ' Dim dbCommand As DbCommand = db.GetStoredProcCommand(sqlCommand) ' PRINT '' DECLARE cur CURSOR FOR SELECT * FROM #t_obj OPEN cur -- Perform the first fetch. FETCH NEXT FROM cur INTO @source_name,@source_type,@col_name,@col_order,@col_len,@col_key,@col_isout,@col_xtype --if(@source_type=N'U') SET @parameterAt='' -- Check @@FETCH_STATUS to see if there are any more rows to fetch. WHILE @@FETCH_STATUS = 0 BEGIN SET @col_redef=(SELECT CASE @col_xtype WHEN 34 THEN 'Binary' WHEN 35 THEN 'String' WHEN 36 THEN 'Guid' WHEN 48 THEN 'Byte' WHEN 52 THEN 'Int16' WHEN 56 THEN 'Int32' WHEN 58 THEN 'DateTime' WHEN 59 THEN 'Single' WHEN 60 THEN 'Currency' WHEN 61 THEN 'DateTime' WHEN 62 THEN 'Double' WHEN 99 THEN 'String' WHEN 104 THEN 'Boolean' WHEN 106 THEN 'Decimal' WHEN 122 THEN 'Currency' WHEN 127 THEN 'Int64' WHEN 165 THEN 'Binary' WHEN 167 THEN 'String' WHEN 173 THEN 'Binary' WHEN 175 THEN 'String' WHEN 231 THEN 'String' WHEN 239 THEN 'String' ELSE '!MISSING' END AS C) --Write out the parameter IF(@col_isout<>1) BEGIN PRINT ' db.AddInParameter(dbCommand, "' + @col_name+ '", DbType.'+@col_redef+', ' + @col_name+ ')' END --Write out the parameter direction it is output IF(@col_isout=1) BEGIN PRINT ' db.AddOutParameter(dbCommand, "' + @col_name+ '", DbType.'+@col_redef+', ' + @col_name+ ')' END -- This is executed as long as the previous fetch succeeds. FETCH NEXT FROM cur INTO @source_name,@source_type,@col_name,@col_order, @col_len,@col_key,@col_isout,@col_xtype END PRINT '' IF @ExecuteType = 'ExecuteNonQuery' BEGIN PRINT ' db.ExecuteNonQuery(dbCommand)' PRINT 'END SUB' END IF @ExecuteType = 'ExecuteReader' BEGIN PRINT ' Return db.ExecuteReader(dbCommand) ' PRINT 'END FUNCTION' END IF @ExecuteType = 'ExecuteScalar' BEGIN PRINT ' Return db.ExecuteScalar(dbCommand) ' PRINT 'END FUNCTION' END CLOSE cur DEALLOCATE cur END if(LEN(@errMsg)>0) PRINT @errMsg DROP TABLE #t_obj SET NOCOUNT ON GO
|
|
|
|
 |
|
 |
Anybody got a vesion of the code generator for Jan 2006 MS DAAB.
Great time saver.
Cheers.
Zac
|
|
|
|
 |
|
 |
Hello mister,
Where I get reference about SQL Types and codes associated ?
WHEN 52 THEN 'SmallInt'
WHEN 56 THEN 'Int'
For example, the column col_xtype has many values: 34, 35, 52, etc, but I don't know that 56 code is the INT SQL SERVER type, I need some reference, do you know ?
Thanks and greetings.
|
|
|
|
 |
|
 |
Do you have a version for Oracle?
Joseph
|
|
|
|
 |
|
|
 |
|
 |
Hi
I found your DAL generator on codeproject, but where is the code?
|
|
|
|
 |
|
|
 |
|
|
 |
|
 |
I found this today and it saved me alot of time.
Thank you.
|
|
|
|
 |
|
 |
Very helpfull...
theJazzyBrain
Excellence is not an act, but a habit! Aristotle
|
|
|
|
 |
|
 |
Thanks for the wonderful program. It's a really nice piece of code.
Besides, I've just discovered that you can call ExecuteDataSet/ExecuteNonQuery(ConnectionString, StoredProcedureName, object1, object2,...)
where object1 is value to pass for Parameter1, object2 us value to pass for Parameter2, etc...
(SqlHelper will automatically discover the parameter types & assign the values accordingly)
|
|
|
|
 |
|
 |
Cool idea, but I think you should use the SqlHelperParameterCache.
It uses DerivedParameters (i'm assuming) to determine all of the parameter information. So yes, there's overhead, but it's only overhead once. It then caches the parameters. Then, each time something else calls the same stored procedure, the parameters are pulled from the cache.
So, you get the power of Derived parameters, and the speed of cached SqlParameter objects.
To produce the code, you can use the SqlHelperParameterCache to retrieve the parameters, then loop through them and create
param(0).value = null;
param(2).value = null;
param(3).value = null;
etc.
Actually, the code is so simple anyway, when you reach that point, maybe you don't need the generator at all.
Here's sample code from (shameless plug) DVDFriend.us
SqlParameter[] p = SqlHelperParameterCache.GetSpParameterSet(ConnectString, STORED_PROCEDURE, false);
p[0].Value = SessionManager.User.ScreenName;
p[1].Value = productID;
p[2].Value = inLibrary;
SqlHelper.ExecuteNonQuery(ConnectString, STORED_PROCEDURE, p);
The first checks to see if the parameters are cached. If so, returns them (actually, returns a copy of them). If not, fetches them, then returns a copy of them. Then you set your values and execute.
|
|
|
|
 |
|
 |
Hi
Interesting idea but not so applicable to the "Microsoft Data Application Block".
Maybe I use a different version of the MDAB,
but isn't there a set of overloaded methods with params object array? These use
inbuilt parameter discovery functions which cache the parameters.
- This would make the example code something like the following (assuming
the values are in the variables specified):
try
{
SqlHelper.ExecuteNonQuery(conn.Connection,
CommandType.StoredProcedure,
"usp_Insert_Organisation",
organisationid,
DisplayName,
DefaultCurrencyFID,
TaxCode,
UserFID,
IsClient,
IsContractor,
IsSupplier,
IsDesigner);
catch(Exception excp)
{
}
|
|
|
|
 |
|
 |
I modified the code so that it includes the .direction parameter and the sqldatatye handles variant.
This works pretty slick. Good job.
|
|
|
|
 |
|
 |
Cool.. would you send me a copy of your version so that I can test it and update the article with your mod.;)
|
|
|
|
 |
|
 |
Why not use the DeriveParameters() function instead. For instance:
SqlConnection cn = new SqlConnection(_strConn);
cn.Open();
SqlCommand cmd = new SqlCommand(strProcName, cn);
cmd.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(cmd);
cmd.Parameters["@first"].Value = "First Name";
cmd.Parameters["@last"].Value = "Last Name";
cmd.ExecuteNonQuery();
cn.Close();
|
|
|
|
 |
|
 |
Sure... your example is simple. But has a performance overhead when using DeriveParameters()
|
|
|
|
 |
|
 |
True enough. On a 2Ghz class system, I can do about 500 DeriveParamaters() per second against a simple stored procedure. This would probably not be acceptable if performed within a loop. However, it is a good practice to cache the parameters. The Microsoft Data Application Block can help out with caching the SqlParameters.
Incidentally, the MS Data Block also has a considerable overhead in calls to functions such as SqlHelper.ExecuteNonQuery() as compared with the SqlClient's ExecuteNonQuery(). These calls do tend to be inside loops or other very frequently used code.
|
|
|
|
 |
|
 |
i've done some code sql server generating but never thought of doing it from a sproc (although my t-sql's not good enough!).
any future plans? like a tool that generates *.cs file for all sprocs in the db? I think codesmith does something like that.. haven't tried it tho'.
|
|
|
|
 |