温习一下这些天学习的CLR编程,存储过程,函数。编写CLR的存储过程,运行起来的效率,果然比普通的SQL语句,存储过程或是函数均高。以后专案需求,或是执行效率较高的SQL,得写成CLR程序,再部署至SQL中去,这样可以解决问题。
可复制代码:
[Microsoft.SqlServer.Server.SqlPRocedure] public static void GetFruitByKind(SqlByte kind_nbr) { SqlConnection connection = new SqlConnection("context connection=true"); connection.Open(); string commandText = "SELECT [Fruit_nbr],[FruitKind_nbr],[FruitName] FROM [dbo].[Fruit] WHERE [FruitKind_nbr] = @Kind_nbr "; SqlCommand command = new SqlCommand(commandText, connection); SqlParameter param = new SqlParameter("@Kind_nbr", SqlDbType.TinyInt); param.Value = kind_nbr; command.Parameters.Add(param); SqlDataReader reader = command.ExecuteReader(); SqlDataRecord record = new SqlDataRecord( new SqlMetaData("Fruit_nbr", SqlDbType.TinyInt), new SqlMetaData("FruitKind_nbr", SqlDbType.TinyInt), new SqlMetaData("FruitName", SqlDbType.NVarChar, 100, 1033, SqlCompareOptions.None) ); SqlContext.Pipe.SendResultsStart(record); while (reader.Read()) { record.SetByte(0, Convert.ToByte(reader["Fruit_nbr"])); record.SetByte(1, Convert.ToByte(reader["FruitKind_nbr"])); record.SetString(2, reader["FruitName"].ToString()); SqlContext.Pipe.SendResultsRow(record); } SqlContext.Pipe.SendResultsEnd(); }View Code
编译与部署至SQL之后,可看到新创建的CLR存储过程:看看效率:
新闻热点
疑难解答