【发布时间】:2015-04-22 11:09:38
【问题描述】:
我有一个 openquery SQL 脚本:
Select * from openquery([oak],'
SELECT LicenseKey, SUM(PaymentAmount)as Payments
FROM vw_ODBC_actv_Payments pt
WHERE MONTH(pt.EntryDate) = 2 and
YEAR(pt.EntryDate) = 2015
GROUP BY LicenseKey
')
当我从 SSMS 运行它时,我可以看到它返回了预期的 n 行。
但是,当我使用相同的连接属性来获取 C# 控制台应用程序的 DataSet 中的数据时:
SqlDataAdapter da = new SqlDataAdapter();
SqlCommand pcmd= new SqlCommand();
DataSet ds= new DataSet();
OpenConnection();
pcmd.Connection = new SqlConnection("Data source=IP adress of the server;Initial Catalog=master; user ID=***; password=***");
cmd.CommandText = "Select * from openquery([oak],'" +
"SELECT LicenseKey, SUM(PaymentAmount)as Payments" +
"FROM vw_ODBC_actv_Payments pt " +
"WHERE MONTH(pt.EntryDate) = 2 and" +
"YEAR(pt.EntryDate) = 2015" +
"GROUP BY LicenseKey')";
try
{
da.SelectCommand = pcmd;
da.Fill(ds); //here comes the error
}
catch (Exception ex)
{
throw new Exception("DBUtils.ExecuteReader():" + ex.Message);
}
我收到这样的错误:
提供者表示用户没有权限 执行操作。现在我需要解决这个问题
我只是在学习 openquery。有人可以指导吗?
【问题讨论】:
-
你没有从上面的代码中打开连接..你也没有声明
ds -
@Izzy - 这是我项目中的代码摘录。连接和数据集由自定义函数提供。当我打开连接时,我的问题就来了。
标签: c# database-connection openquery