反正是有转换的C#功能SQL Server存储过程的?

我这是写在C#中的存储过程:

SqlConnection scn = new SqlConnection("context connection=true;"); SqlCommand scm = new SqlCommand("Select * from EPMS_Filters",scn); 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();

反正是有或某些工具,可以将此代码转换为SQL存储过程的代码? 还是必须用手工改写呢? 我问的原因是,比如我不知道如何处理转弯的数据集变量SQL类型..

谢谢!

格雷格

--------------解决方案-------------

你必须手工改写它。 这不是太难,但。

对于通过您的数据表的循环,你会替换成光标,像这样的东西:

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;

答案是否定的,它不太可能有自动化的过程将能够把这一C#为SQL存储过程。

在Visual Studio中使用SQL Server的项目,你可以改变你现有的C#代码存储过程。

这些功能都装饰有[Microsoft.SqlServer.Server.SqlProcedure]

而你的函数只返回sqlboolean类型,如果你想其他的返回类型,您使用的输出参数。

核实 http://blog.sqlauthority.com/2008/10/19/sql-server-introduction-to-clr-simple-example-of-clr-stored-procedure/

分类:C# 时间:2015-03-15 人气:0
本文关键词: C#,SQL存储过程
分享到:

相关文章

Copyright (C) 55228885.com, All Rights Reserved.

55228885 版权所有 京ICP备15002868号

processed in 0.860 (s). 10 q(s)