提高你的数据库编程效率:Microsoft CLR Via Sql Server

时间:2023-11-23 14:37:20

你还在为数据库编程而抓狂吗?那些恶心的脚本拼接,低效的脚本调试的日子将会与我们越来越远啦。现在我们能用支持.NET的语言来开发数据库中的对象,如:存储过程,函数,触发器,集合函数已及复杂的类型。看到这些你还能淡定吗?哈哈,不仅仅是这些。那些能被.NET支持的第三方扩展通过该技术统统都能应用在数据库编程上,如:正则表达式,.NET庞大的加密解密库,以及各种.NET集成的排序和搜索算法。

下面我就来一一介绍怎么使用该技术来解放我们的双手!

实现存储过程

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SqlServer.Server;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Data.SqlTypes;
  9. using System.Collections;
  10. public class SampleStoreProcedure
  11. {
  12. [SqlProcedure]
  13. public static void PrintStudentDetail()
  14. {
  15. SqlConnection conn = new SqlConnection("Context connection=true");
  16. conn.Open();
  17. SqlCommand cmd = new SqlCommand("select * from student", conn);
  18. SqlCommand cmd2 = new SqlCommand("insert into studentdetail values(@detail)");
  19. SqlDataReader reader;
  20. string tmpData=string.Empty;
  21. ArrayList tmpDataArray=new ArrayList();
  22. reader = cmd.ExecuteReader();
  23. while (reader.Read())
  24. {
  25. for (int i = 0; i < reader.FieldCount; i++)
  26. {
  27. tmpData += reader[i].ToString();
  28. }
  29. tmpDataArray.Add(tmpData);
  30. }
  31. reader.Close();
  32. cmd2.Connection = conn;
  33. foreach (string tmp in tmpDataArray)
  34. {
  35. cmd2.Parameters.Clear();
  36. cmd2.Parameters.AddWithValue("@detail", tmp);
  37. cmd2.ExecuteNonQuery();
  38. }
  39. conn.Close();
  40. //conn2.Close();
  41. }
  42. [SqlProcedure]
  43. public static void GetStudentDetail(int id)
  44. {
  45. SqlConnection conn = new SqlConnection("Context connection=true");
  46. SqlCommand cmd = new SqlCommand("select * from student where id=@id", conn);
  47. SqlDataReader reader;
  48. cmd.Parameters.AddWithValue("@id", id);
  49. try
  50. {
  51. conn.Open();
  52. reader = cmd.ExecuteReader();
  53. SqlPipe pipe = SqlContext.Pipe;
  54. pipe.Send(reader);
  55. reader.Close();
  56. }
  57. catch
  58. {
  59. conn.Close();
  60. }
  61. finally
  62. {
  63. }
  64. }
  65. };

部署步骤

  1. 1.编译项目,获取生成的DLL文件。
  2. 2.在数据库中输入命令sp_configure [clr enabled],1 reconfigure --开启对程序集的信任权限。
  3. 3.输入命令:create assembly chapter34_UDT from 'c:\chapter34_UDT.dll' 注册程序集
  4. 4.输入命令:
  5. <p>--注册存储过程
  6. create procedure PrintStudentDetail
  7. as
  8. external name chapter34_UDT.SampleStoreProcedure.PrintStudentDetail</p><p>--注册带参数的存储过程
  9. create procedure GetStudentDetail
  10. (
  11. @Id int
  12. )
  13. as
  14. external name chapter34_UDT.SampleStoreProcedure.GetStudentDetail</p>

执行结果

  1. exec PrintStudentDetail
  2. exec GetStudentDetail 1

提高你的数据库编程效率:Microsoft CLR Via Sql Server

存储过程PrintStudentDetail执行结果

提高你的数据库编程效率:Microsoft CLR Via Sql Server

存储过程GetStudentDetail执行的结果

实现函数

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Data.SqlTypes;
  8. using Microsoft.SqlServer.Server;
  9. using System.Security;
  10. using System.Security.Cryptography;
  11. public class SampleFunction
  12. {
  13. public SampleFunction()
  14. {
  15. }
  16. [SqlFunction]
  17. public static SqlString Hash(SqlString data)
  18. {
  19. SHA1 sha1 = SHA1.Create();
  20. byte[] tmp = Encoding.ASCII.GetBytes(data.Value);
  21. string result= Convert.ToBase64String(sha1.ComputeHash(tmp));
  22. return new SqlString(result);
  23. }
  24. }

部署步骤

  1. 1.编译项目,获取生成的DLL文件。
  2. 2.在数据库中输入命令:
  3. sp_configure [clr enabled],1 reconfigure --开启对程序集的信任权限。
  4. 3.输入命令:
  5. create assembly chapter34_UDT from 'c:\chapter34_UDT.dll' 注册程序集
  6. --如果上述步骤已经做了就忽略
  7. <p>4.输入命令:</p>
  8. --注册函数
  9. create function HashSomeThing(@data nvarchar) returns nvarchar
  10. as
  11. external name chapter34_UDT.SampleFunction.[Hash]

执行结果

  1. <p>输入调用命令:</p>select dbo.HashSomeThing(name) from Student

提高你的数据库编程效率:Microsoft CLR Via Sql Server

实现表值函数

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Data.SqlClient;
  7. using System.Data.SqlTypes;
  8. using Microsoft.SqlServer.Server;
  9. using System.Text.RegularExpressions;
  10. using System.Xml.Linq;
  11. using System.Xml;
  12. using System.IO;
  13. using System.Collections;
  14. public class SampleTableValueFunction
  15. {
  16. ///
  17. /// 表值函数的主体,该函数需要结合“内容填充函数”才能发挥功能。这里的“内容填充函数”是通过
  18. /// 属性“FillRowMethodName”属性来指定的。属性“TableDefinition”用来定义返回表格的格式。
  19. ///
  20. [SqlFunction(TableDefinition = "tmp_value nvarchar(max)", FillRowMethodName = "FillRow")]
  21. public static IEnumerable PrintOneToTen()
  22. {
  23. IList<string> result2 = new List<string>();
  24. var matches = new string[]{
  25. "one",
  26. "two",
  27. "three",
  28. "four",
  29. "five",
  30. "six",
  31. "seven",
  32. "eight",
  33. "nine",
  34. "ten"
  35. };
  36. return matches.AsEnumerable();
  37. }
  38. public static void FillRow(object obj, out SqlString tmp)
  39. {
  40. tmp = new SqlString(obj.ToString());
  41. }
  42. }

部署步骤

  1. 1.编译项目,获取生成的DLL文件。
  2. 2.在数据库中输入命令:
  3. sp_configure [clr enabled],1 reconfigure --开启对程序集的信任权限。
  4. 3.输入命令:
  5. create assembly chapter34_UDT from 'c:\chapter34_UDT.dll' 注册程序集
  6. --如果上述步骤已经做了就忽略
  7. 4.输入命令:
  8. create function SampleTableValueFunction() returns table(tmp_value nvarchar(max) null)
  9. as
  10. external name chapter34_UDT.SampleTableValueFunction.PrintOneToTen

执行结果

提高你的数据库编程效率:Microsoft CLR Via Sql Server

实现触发器

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SqlServer.Server;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Data.SqlTypes;
  9. public class SampleTrigger
  10. {
  11. public SampleTrigger()
  12. {
  13. }
  14. [SqlTrigger(Event = "For Insert,Update,Delete", Name = "PrintInfo", Target = "Student")]
  15. public static void PrintInfo()
  16. {
  17. SqlTriggerContext triggerContext = SqlContext.TriggerContext;
  18. SqlConnection conn = new SqlConnection("Context connection=true");
  19. SqlCommand cmd = new SqlCommand();
  20. cmd.Connection = conn;
  21. switch (triggerContext.TriggerAction)
  22. {
  23. case TriggerAction.Insert:
  24. cmd.CommandText = "insert into StudentDetail values('insert operation!')";
  25. break;
  26. case TriggerAction.Delete:
  27. cmd.CommandText = "insert into StudentDetail values('delete operation!')";
  28. break;
  29. case TriggerAction.Update:
  30. cmd.CommandText = "insert into StudentDetail values('update operation!')";
  31. break;
  32. default:
  33. break;
  34. }
  35. try
  36. {
  37. conn.Open();
  38. cmd.ExecuteNonQuery();
  39. }
  40. catch
  41. {
  42. }
  43. finally
  44. {
  45. conn.Close();
  46. }
  47. }
  48. [SqlTrigger(Name="InsertSomething",Target="chapter30.dbo.Student",Event="FOR INSERT")]
  49. public static void InsertSomething()
  50. {
  51. SqlTriggerContext triggerContext = SqlContext.TriggerContext;
  52. if (triggerContext.TriggerAction == TriggerAction.Insert)
  53. {
  54. var conn = new SqlConnection("Context connection=true");
  55. var cmd = new SqlCommand();
  56. cmd.Connection = conn;
  57. cmd.CommandText = "Insert into StudentDetail values('insert event')";
  58. conn.Open();
  59. cmd.ExecuteNonQuery();
  60. conn.Close();
  61. }
  62. }
  63. }

部署步骤

  1. 1.编译项目,获取生成的DLL文件。
  2. 2.在数据库中输入命令:
  3. sp_configure [clr enabled],1 reconfigure --开启对程序集的信任权限。
  4. 3.输入命令:
  5. create assembly chapter34_UDT from 'c:\chapter34_UDT.dll' 注册程序集
  6. --如果上述步骤已经做了就忽略
  7. 4.输入命令:
  8. --注册触发器
  9. create trigger PrintSomething on Student
  10. for insert,update,delete
  11. as
  12. external name  chapter34_UDT.SampleTrigger.PrintInfo

执行结果

  1. 输入命令:
  2. insert into Student values(12345,'tmp','11','11')
  3. update Student set Name='new'+Name where Id=12345
  4. delete from Student where Id=12345
  5. select * from StudentDetail

提高你的数据库编程效率:Microsoft CLR Via Sql Server

实现聚合函数

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SqlServer.Server;
  6. using System.Data;
  7. using System.Data.SqlClient;
  8. using System.Data.SqlTypes;
  9. [Serializable]
  10. [SqlUserDefinedAggregate(Format.Native)]
  11. public struct SampleSum
  12. {
  13. private int sum;
  14. public void Init()
  15. {
  16. sum = 0;
  17. }
  18. public void Accumulate(SqlInt32 Value)
  19. {
  20. sum += Value.Value;
  21. }
  22. public void Merge(SampleSum Group)
  23. {
  24. sum += Group.sum;
  25. }
  26. public SqlInt32 Terminate()
  27. {
  28. return new SqlInt32(sum);
  29. }
  30. }

部署步骤

  1. 1.编译项目,获取生成的DLL文件。
  2. 2.在数据库中输入命令:
  3. sp_configure [clr enabled],1 reconfigure --开启对程序集的信任权限。
  4. 3.输入命令:
  5. create assembly chapter34_UDT from 'c:\chapter34_UDT.dll' 注册程序集
  6. --如果上述步骤已经做了就忽略
  7. 4.输入命令:
  8. --注册聚合函数
  9. create aggregate SampleSum(@value int) returns int
  10. external name [chapter34_UDT].SampleSum

执行结果

  1. 输入命令:
  2. select dbo.SampleSum(TAggregate) from TAggregate
  3. select Sum(TAggregate) from TAggregate

提高你的数据库编程效率:Microsoft CLR Via Sql Server

实现类型

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.SqlServer.Server;
  6. using System.Data.SqlTypes;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. [Serializable]
  10. [Microsoft.SqlServer.Server.SqlUserDefinedType(Format.Native)]
  11. public struct Facade : INullable
  12. {
  13. public bool isNull;
  14. int hairColor;
  15. int tall;
  16. int skin;
  17. int country;
  18. public Facade(int hairColor, int tall, int skin, int country)
  19. {
  20. isNull = false;
  21. this.hairColor = hairColor;
  22. this.tall = tall;
  23. this.skin = skin;
  24. this.country = country;
  25. }
  26. public static Facade Null
  27. {
  28. get
  29. {
  30. return new Facade { isNull = true };
  31. }
  32. }
  33. public override string ToString()
  34. {
  35. StringBuilder sb = new StringBuilder();
  36. sb.AppendFormat("{0};", hairColor);
  37. sb.AppendFormat("{0};", tall);
  38. sb.AppendFormat("{0};", skin);
  39. sb.AppendFormat("{0}", country);
  40. return sb.ToString();
  41. }
  42. public static Facade Parse(SqlString data)
  43. {
  44. if (data.IsNull)
  45. {
  46. return new Facade { isNull = true };
  47. }
  48. Facade result;
  49. string[] tmpData = data.Value.Split(';');
  50. result = new Facade(int.Parse(tmpData[0]), int.Parse(tmpData[1]), int.Parse(tmpData[2]), int.Parse(tmpData[3]));
  51. return result;
  52. }
  53. public bool IsNull
  54. {
  55. get { return isNull; }
  56. }
  57. }

部署步骤

  1. 1.编译项目,获取生成的DLL文件。
  2. 2.在数据库中输入命令:
  3. sp_configure [clr enabled],1 reconfigure --开启对程序集的信任权限。
  4. 3.输入命令:
  5. create assembly chapter34_UDT from 'c:\chapter34_UDT.dll' 注册程序集
  6. --如果上述步骤已经做了就忽略
  7. 4.输入命令:
  8. create type Facade external name
  9. [chapter34_UDT].Facade

执行结果

提高你的数据库编程效率:Microsoft CLR Via Sql Server

小结

CLR Sql Server 的推出大大的提高了Sql Server的脚本编程效率问题,并且这项技术给了我们很大的相信空间。现在我们就来用有限的手段实现无限的可能吧!

reference from : http://blog.csdn.net/ghostbear/article/details/7333189