CLR 存储过程无法连接 SqlConnection 常规连接
CLR Stored Procedure Unable to connect with SqlConnection Regular Connection
我尝试在 VS2017 中创建一个 CLR 存储过程,但在执行该存储过程时遇到错误 "NOT Connected."。
我需要连接到其他数据库服务器以获取一些数据。因此我不能在 SqlConnection
.
中使用 context=true
- 将在 serverA 中创建存储过程
- 此存储过程将从服务器 B 查询数据
- 数据会回存到serverA。
我需要做些什么才能在 CLR 存储过程中进行常规连接吗?
请指教。谢谢!
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void udp_CLR_GetData()
{
string ConnStr = "server=MyServer; database=MyDB; user id=accabc; password=abc123";
string sql = " select top 1 ID from [dbo].Table1 ";
SqlDataReader dr = null;
DataTable dt = new DataTable();
try
{
using (SqlConnection fcon = new SqlConnection(ConnStr))
{
if (fcon.State == ConnectionState.Open)
{
SqlContext.Pipe.Send("Connected.");
using (SqlCommand fcmd = new SqlCommand(sql, fcon))
{
SqlContext.Pipe.Send("Before executing reader...");
dr = fcmd.ExecuteReader();
SqlContext.Pipe.Send("After executing reader...");
SqlContext.Pipe.Send("Before send...");
SqlContext.Pipe.Send(dr);
SqlContext.Pipe.Send("After send...");
}
}
else
{
SqlContext.Pipe.Send("NOT Connected.");
}
}
}
catch(Exception ex)
{
SqlContext.Pipe.Send("Exception error (udp_CLR_GetData): " + ex.Message);
}
finally
{
if(dr != null && !dr.IsClosed)
{
dr.Close();
}
}
}
}
尝试在连接字符串中定义数据源而不是服务器
string ConnStr = "DataSource=MyServer;Initial Catalog=MyDB;User Id=accabc;Password=abc123";
除此之外,请确保在服务器上启用了 clr:
https://docs.microsoft.com/en-us/sql/relational-databases/clr-integration/clr-integration-enabling?view=sql-server-ver15
正在创建 SqlConnection
的新实例:
using (SqlConnection fcon = new SqlConnection(ConnStr))
不会在 "open" 状态下创建它。您需要实际打开它才能使它成为 "open"。因此,我将删除其中的 if (fcon.State == ConnectionState.Open)
和关联的 else
部分。我还会删除 SqlContext.Pipe.Send("Connected.");
行。
然后,就在 dr = fcmd.ExecuteReader();
行之前,添加一行:
fcon.Open();
这样你打开连接并立即执行命令。无需打开连接即可完成准备命令的其他工作。
有关一般使用 SQLCLR 的更多信息,请访问:SQLCLR Info
我尝试在 VS2017 中创建一个 CLR 存储过程,但在执行该存储过程时遇到错误 "NOT Connected."。
我需要连接到其他数据库服务器以获取一些数据。因此我不能在 SqlConnection
.
context=true
- 将在 serverA 中创建存储过程
- 此存储过程将从服务器 B 查询数据
- 数据会回存到serverA。
我需要做些什么才能在 CLR 存储过程中进行常规连接吗?
请指教。谢谢!
using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
public partial class StoredProcedures
{
[Microsoft.SqlServer.Server.SqlProcedure]
public static void udp_CLR_GetData()
{
string ConnStr = "server=MyServer; database=MyDB; user id=accabc; password=abc123";
string sql = " select top 1 ID from [dbo].Table1 ";
SqlDataReader dr = null;
DataTable dt = new DataTable();
try
{
using (SqlConnection fcon = new SqlConnection(ConnStr))
{
if (fcon.State == ConnectionState.Open)
{
SqlContext.Pipe.Send("Connected.");
using (SqlCommand fcmd = new SqlCommand(sql, fcon))
{
SqlContext.Pipe.Send("Before executing reader...");
dr = fcmd.ExecuteReader();
SqlContext.Pipe.Send("After executing reader...");
SqlContext.Pipe.Send("Before send...");
SqlContext.Pipe.Send(dr);
SqlContext.Pipe.Send("After send...");
}
}
else
{
SqlContext.Pipe.Send("NOT Connected.");
}
}
}
catch(Exception ex)
{
SqlContext.Pipe.Send("Exception error (udp_CLR_GetData): " + ex.Message);
}
finally
{
if(dr != null && !dr.IsClosed)
{
dr.Close();
}
}
}
}
尝试在连接字符串中定义数据源而不是服务器
string ConnStr = "DataSource=MyServer;Initial Catalog=MyDB;User Id=accabc;Password=abc123";
除此之外,请确保在服务器上启用了 clr: https://docs.microsoft.com/en-us/sql/relational-databases/clr-integration/clr-integration-enabling?view=sql-server-ver15
正在创建 SqlConnection
的新实例:
using (SqlConnection fcon = new SqlConnection(ConnStr))
不会在 "open" 状态下创建它。您需要实际打开它才能使它成为 "open"。因此,我将删除其中的 if (fcon.State == ConnectionState.Open)
和关联的 else
部分。我还会删除 SqlContext.Pipe.Send("Connected.");
行。
然后,就在 dr = fcmd.ExecuteReader();
行之前,添加一行:
fcon.Open();
这样你打开连接并立即执行命令。无需打开连接即可完成准备命令的其他工作。
有关一般使用 SQLCLR 的更多信息,请访问:SQLCLR Info