Is there anyway of converting c# function to sql server stored procedure?
I have a stored procedure which is written in c#:
SqlConnection scn = new SqlConnection("context connection=true;");
SqlCommand scm = new SqlCommand("Select * from EPMS_Filters",scn);
开发者_JAVA技巧 SqlDataAdapter sda = new SqlDataAdapter(scm);
DataSet ds = new DataSet();
//template for select Query.
String SelectQry = "select convert(varchar,TimeStamp,101) as TimeStamp, avg({0} from EPMSDataTable where timestamp between '{1}' and '{2}' group by convert(varchar,timestamp,101)";
String filters = "";
//select clause part which contains simple columns select col1, col2...
sda.SelectCommand.CommandText = "Select column_name from information_schema.columns where table_name = 'EPMSDataTable' and column_name <> 'timestamp'";
sda.Fill(ds, "SimpleFilters");
foreach (DataRow dr in ds.Tables["SimpleFilters"].Rows)
{
String fName = dr[0].ToString();
filters += dr[0] + ")as " + dr[0] + ", avg(";
}
filters = filters.TrimEnd(", avg(".ToCharArray()); //remove last ','
scm.CommandText = String.Format(SelectQry,filters,start.ToString(),end.ToString());
scn.Open();
SqlContext.Pipe.Send(scm.ExecuteReader());
scn.Close();
Is there anyway or some tool that can convert this code to an sql stored procedure code? Or do I have to rewrite it by hand?
You'll have to rewrite it by hand. It's not too hard though.
For the loop through your datatable, you would replace that with a cursor, something like this:
SET NOCOUNT ON;
DECLARE ohno_a_cursor CURSOR FAST_FORWARD FOR
Select column_name from information_schema.columns where table_name = 'EPMSDataTable' and column_name <> 'timestamp'
DECLARE @colname nvarchar(max);
OPEN ohno_a_cursor;
FETCH NEXT FROM ohno_a_cursor INTO @colname
WHILE @@FETCH_STATUS = 0 BEGIN
SET @Filters = @Filters + '(whatever');
END
CLOSE ohno_a_cursor;
DEALLOCATE ohno_a_cursor;
The answer is no, its unlikely that any automated process would be able to turn that C# into a SQL Stored procedure.
By using sql server project in visual studio, you can change your existing c# code to stored procedure.
The functions are decorated with [Microsoft.SqlServer.Server.SqlProcedure]
And your function return only sqlboolean type, if you want other return type, you use output parameters.
Check it http://blog.sqlauthority.com/2008/10/19/sql-server-introduction-to-clr-simple-example-of-clr-stored-procedure/
check this link, it can be helpful for you. http://www.atomicsql.com
It convert from c# to Mssql stored procedure.
the input is your dll containing your c# code, and the output is a sql script file.
check the instruction of the help
-- for admins of this thread: please do not delete this comment. this tools is not a virus or something that you thin this can harm your computer.
精彩评论