SQL Server中挪用C#類中的辦法實例(應用.NET法式集)。本站提示廣大學習愛好者:(SQL Server中挪用C#類中的辦法實例(應用.NET法式集))文章只能為提供參考,不一定能成為您想要的結果。以下是SQL Server中挪用C#類中的辦法實例(應用.NET法式集)正文
需求是如許的,我在.net法式裡操作數據時將一些字段數據加密了,這些數據是許多體系共用的,個中一delphi法式也須要用到,而且須要將數據解密,因為我在.net裡加密的方法比擬特別,在delphi法式裡解密比擬繁瑣且要消費許多時光,所以不能不讓sqlserver挪用法式集的方法來處理成績。
上面只是一個例子,貼出來同享。
樹立一個dll,class,代碼以下:
namespace MyDll
{
public partial class MyClass
{
[SqlMethod]
public static SqlString UrlDecode(string value)
{
return new SqlString(HttpUtility.UrlDecode(value));
}
}
}
放到數據庫辦事器上。
以後運轉上面的T-SQL代碼:
EXEC sp_configure 'show advanced options','1';
GO
RECONFIGURE;
GO
EXEC sp_configure 'clr enabled','1'
RECONFIGURE;
GO
ALTER DATABASE DBName SET TRUSTWORTHY ON
GO
CREATE ASSEMBLY
[System.Web] FROM
'C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\System.Web.dll'
WITH permission_set = UNSAFE
GO
CREATE ASSEMBLY SQL_CLR_Url
FROM 'C:\MyDLL.dll'
WITH PERMISSION_SET = UNSAFE
GO
CREATE FUNCTION SqlUrlDecode(@urlstr NVARCHAR(4000))
RETURNS NVARCHAR(4000)
AS
EXTERNAL NAME SQL_CLR_Url.[SqlClr.MyClass].UrlDecode
GO
然後便可以在T-SQL裡挪用這個函數了。
select dbo.SqlUrlDecode(Name) from Table
try {
/* 查詢CLOB對象並鎖定 */
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
/* 獲得此CLOB對像 */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* 停止籠罩式修正 */
BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
BufferedReader in = new BufferedReader(new FileReader(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 失足回滾 */
conn.rollback();
throw ex;
}
/* 恢回復復興提交狀況 */
conn.setAutoCommit(defaultCommit);
}
3、調換CLOB對像(將原CLOB對像消除,換成一個全新的CLOB對像)
public static void clobWordStr(String infile) throws Exception
{
/* 設定不主動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
/* 清空原CLOB對像 */
stmt.executeUpdate("UPDATE TEST_CLOB SET CLOBCOL=EMPTY_CLOB() WHERE ID='111'");
/* 查詢CLOB對象並鎖定 */
ResultSet rs = stmt.executeQuery("SELECT CLOBCOL FROM TEST_CLOB WHERE ID='111' FOR UPDATE");
while (rs.next()) {
/* 獲得此CLOB對像 */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* 更新數據 */
BufferedWriter out = new BufferedWriter(clob.getCharacterOutputStream());
BufferedReader in = new BufferedReader(new FileReader(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 失足回滾 */
conn.rollback();
throw ex;
}
/* 恢回復復興提交狀況 */
conn.setAutoCommit(defaultCommit);
}
4、CLOB對像讀取
public static void clobRead(String outfile) throws Exception
{
/* 設定不主動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
/* 查詢CLOB對像 */
ResultSet rs = stmt.executeQuery("SELECT * FROM TEST_CLOB WHERE ID='111'");
while (rs.next()) {
/* 獲得CLOB對像 */
oracle.sql.CLOB clob = (oracle.sql.CLOB)rs.getClob("CLOBCOL");
/* 以字符情勢輸入 */
BufferedReader in = new BufferedReader(clob.getCharacterStream());
BufferedWriter out = new BufferedWriter(new FileWriter(outfile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
out.close();
in.close();
}
} catch (Exception ex) {
conn.rollback();
throw ex;
}
/* 恢回復復興提交狀況 */
conn.setAutoCommit(defaultCommit);
}
2、 BLOB對象的存取
1、 向數據庫中拔出一個新的BLOB對像
public static void blobInsert(String infile) throws Exception
{
/* 設定不主動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
/* 拔出一個空的BLOB對像 */
stmt.executeUpdate("INSERT INTO TEST_BLOB VALUES ('222', EMPTY_BLOB())");
/* 查詢此BLOB對象並鎖定 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* 掏出此BLOB對像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 向BLOB對像中寫入數據 */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 失足回滾 */
conn.rollback();
throw ex;
}
/* 恢回復復興提交狀況 */
conn.setAutoCommit(defaultCommit);
}
2、修正BLOB對像(是在原BLOB對像基本長進行籠罩式的修正)
public static void blobModify(String infile) throws Exception
{
/* 設定不主動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
/* 查詢BLOB對象並鎖定 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* 掏出此BLOB對像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 向BLOB對像中寫入數據 */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 失足回滾 */
conn.rollback();
throw ex;
}
/* 恢回復復興提交狀況 */
conn.setAutoCommit(defaultCommit);
}
3、調換BLOB對像(將原BLOB對像消除,換成一個全新的BLOB對像)
public static void blobWordStr(String infile) throws Exception
{
/* 設定不主動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
/* 清空原BLOB對像 */
stmt.executeUpdate("UPDATE TEST_BLOB SET BLOBCOL=EMPTY_BLOB() WHERE ID='222'");
/* 查詢此BLOB對象並鎖定 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222' FOR UPDATE");
while (rs.next()) {
/* 掏出此BLOB對像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 向BLOB對像中寫入數據 */
BufferedOutputStream out = new BufferedOutputStream(blob.getBinaryOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(infile));
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 失足回滾 */
conn.rollback();
throw ex;
}
/* 恢回復復興提交狀況 */
conn.setAutoCommit(defaultCommit);
}
4、BLOB對像讀取
public static void blobRead(String outfile) throws Exception
{
/* 設定不主動提交 */
boolean defaultCommit = conn.getAutoCommit();
conn.setAutoCommit(false);
try {
/* 查詢BLOB對像 */
ResultSet rs = stmt.executeQuery("SELECT BLOBCOL FROM TEST_BLOB WHERE ID='222'");
while (rs.next()) {
/* 掏出此BLOB對像 */
oracle.sql.BLOB blob = (oracle.sql.BLOB)rs.getBlob("BLOBCOL");
/* 以二進制情勢輸入 */
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile));
BufferedInputStream in = new BufferedInputStream(blob.getBinaryStream());
int c;
while ((c=in.read())!=-1) {
out.write(c);
}
in.close();
out.close();
}
/* 正式提交 */
conn.commit();
} catch (Exception ex) {
/* 失足回滾 */
conn.rollback();
throw ex;
}
/* 恢回復復興提交狀況 */
conn.setAutoCommit(defaultCommit);
}
不雅察上述法式對LOB類型字段的存取,我們可以看出,較之其它類型字段,有上面幾個明顯分歧的特色:
一是必需撤消主動提交。