如何使用c# [英] How to use openquery in c#

查看:210
本文介绍了如何使用c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个openquery SQL脚本:

 从openquery中选择*([oak],'
SELECT LicenseKey ,SUM(PaymentAmount)作为付款
FROM vw_ODBC_actv_Payments pt
WHERE MONTH(pt.EntryDate)= 2和
YEAR(pt.EntryDate)= 2015
GROUP BY LicenseKey
')

当我从SSMS运行它时,我可以看到它返回预期的n行。 p>

但是当我使用相同的连接属性来触发这个方法来获取C#控制台应用程序的DataSet中的数据时:

  SqlDataAdapter da = new SqlDataAdapter(); 
SqlCommand pcmd = new SqlCommand();
DataSet ds = new DataSet();
OpenConnection();
pcmd.Connection = new SqlConnection(Data source = IP地址的服务器;初始目录= master;用户ID = ***;密码= ***);
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); //这里的错误
}
catch(Exception ex)
{
throw new Exception(DBUtils.ExecuteReader():+ ex.Message);
}

我遇到这样的错误:


提供程序指示用户没有
的权限来执行操作。现在我需要对这个问题做一些事情。


我只是在学习openquery。任何人都可以指导?

解决方案

首先,你不是在代码的任何地方打开连接,第二次使用使用块清理你的代码。因此,假设查询按要求工作,你可以这样做。

  using(SqlConnection con = new SqlConnection ))
{
string myQuery =Your Query;
using(SqlCommand cmd = new SqlCommand(myQuery,con))
{
using(SqlDataAdapter sda = new SqlDataAdapter())
{
con.Open ;
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds);
}
}
}

注意:如果您在 config 文件中存储 connectionString ,并在代码中读取它,则会更好。


I have an openquery SQL script:

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
')

When I run this from SSMS I can see that it returns expected n rows.

However when I'm firing this with the same connection properties to get the data in a DataSet for a C# console application:

    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);
}

I'm getting an error like this:

The provider indicates that the user did not have the permission to perform the operation. Now I need to do something with this issue

I'm just learning about openquery. Can anybody guide?

解决方案

Firstly you're not opening the connection anywhere in your code hence the error. Second clean up your code with the using block. So assuming the query works as required you can do something like.

using(SqlConnection con = new SqlConnection("Connection String Here"))
    {
        string myQuery = "Your Query";
        using(SqlCommand cmd = new SqlCommand(myQuery, con))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                con.Open();
                sda.SelectCommand = cmd;
                DataSet ds = new DataSet();
                sda.Fill(ds);
            }
        }
    }

Note: It would be a better if you stored the connectionString in your config file and read it in your code.

这篇关于如何使用c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆