存储过程我可以写在CS文件里吗?只要执行这个CS文件。存储过程就可以加载到数据库。有这样的写发吗?

解决方案 »

  1.   

    可以,运行时通过执行SQL语句创建存储过程
      

  2.   

    string[] spstring=
    {
       "CREATE PROCUDERE spname AS SELECT...",
        "CREATE PROCUDERE spname AS UPDATE..."
        //注意,SqlCommand中的CommandText中不能有GO,这句是C#中是不执行的.
    }
    ...
    SqlCommand cmd=new SqlCommand;
    foreach(string sp in spstring)
    {
    cmd.CommandText=sp;
    con.open();
    cmd.ExecuteNonQuery();
    con.close();
    ...
    }
      

  3.   

    当然可以,CLR 存储过程。
    如果你的存储过程逻辑比较复杂,用 CLR 存储过程就幸福了。
    我在新项目里全部使用了 CLR Store Procedure
    逻辑处理相当灵活 贴一段代码给你看
    详细用法参考 SQL 文档    public class Groups
        {
            /// <summary>
            /// by vengair
            /// </summary>
            /// <param name="groupsInfo"></param>      
            [SqlProcedure]
            public static void sp_Account_GetRelativeGroupsByGroupsInfo(string groupsInfo)
            {
                StringBuilder myBuilder = new StringBuilder();            List<string> groupInfos = new List<string>(groupsInfo.Split(';'));
                foreach (string groupInfo in groupInfos)
                {
                    if (groupInfos.IndexOf(groupInfo) > 0)
                    {
                        myBuilder.AppendLine("INTERSECT");
                    }                myBuilder.AppendLine("SELECT DISTINCT gB.*");
                    myBuilder.AppendLine("FROM dbo.[Account.Users] u");
                    myBuilder.AppendLine("\tINNER JOIN dbo.[Account.UserGroupRelations] ugA");
                    myBuilder.Append("\tON u.[UserId] = ugA.[UserId]");                List<string> groupInfoItems = new List<string>(groupInfo.Split('|'));                int leftValue = 0;
                    int rightValue = 0;                using (SqlConnection myConnection = new SqlConnection("context connection=true"))
                    {
                        myConnection.Open();                    SqlCommand myCommand = new SqlCommand("SELECT [LeftValue], [RightValue] FROM dbo.[Account.Groups] WHERE [GroupId] = @groupId", myConnection);
                        myCommand.Parameters.AddWithValue("@groupId", groupInfoItems[0]);                    SqlDataReader myReader = myCommand.ExecuteReader();
                        using (myReader)
                        {
                            while (myReader.Read())
                            {
                                leftValue = myReader.GetInt32(0);
                                rightValue = myReader.GetInt32(1);
                            }
                        }
                    }                if (groupInfoItems[1] == "1")
                    {
                        myBuilder.AppendLine(" AND ugA.[GroupId] = '" + groupInfoItems[0] + "'");
                    }
                    else
                    {
                        myBuilder.AppendLine();
                        myBuilder.AppendLine("\tINNER JOIN dbo.[Account.Groups] g");
                        myBuilder.AppendLine("\tON ugA.[GroupId] = g.[GroupId] AND g.[LeftValue] >= " + leftValue + " AND g.[RightValue] <= " + rightValue);
                    }                myBuilder.AppendLine("\tINNER JOIN dbo.[Account.UserGroupRelations] ugB");
                    myBuilder.AppendLine("\tON u.[UserId] = ugB.[UserId]");
                    myBuilder.AppendLine("\tINNER JOIN dbo.[Account.Groups] gA");
                    myBuilder.Append("\tON ugB.[GroupId] = gA.[GroupId]");
                    myBuilder.AppendLine(" AND gA.[GroupId] NOT IN (SELECT [GroupId] FROM dbo.[Account.Groups] g WHERE (g.[LeftValue] <= " + leftValue + " AND g.[RightValue] >= " + rightValue + ") OR (g.[LeftValue] > " + leftValue + " AND g.[RightValue] < " + rightValue + "))");
                    myBuilder.AppendLine("\tINNER JOIN dbo.[Account.Groups] gB");
                    myBuilder.AppendLine("\tON gA.[LeftValue] >= gB.[LeftValue] AND gA.[RightValue] <= gB.[RightValue]");
                }            myBuilder.AppendLine("ORDER BY [GroupName]");            using (SqlConnection myConnection = new SqlConnection("context connection=true"))
                {
                    myConnection.Open();                SqlCommand myCommand = new SqlCommand(myBuilder.ToString(), myConnection);                SqlContext.Pipe.ExecuteAndSend(myCommand);
                }
            }
        }