传递参数以从C#访问查询 [英] passing parameter to access query from c#

查看:64
本文介绍了传递参数以从C#访问查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设计了一个访问查询,如下所示:

i have design an access query which seem like this:

SELECT Replace(names,'lion','kiss') AS Expr1
FROM table1;

lion kiss 这两个值是临时的,现在我希望它们是两个变量,以便可以从c#向其传递值.

the two values that is lion and kiss, which are temporary, now i want these to be two variables, so that i can pass value to it from c#.

如何从c#调用此查询,并向其传递两个值.

how to call this query from c#, and pass it two values.

我正在使用Access 2007.

I am using access 2007.

感谢您的帮助:)

推荐答案

尝试类似的方法(我发现

Try something like this (I found this on the subject):

public void ReplaceColumnA(string oldvalue, string newvalue)
{
    using(OleDbConnection connection1 = (OleDbConnection)DatabaseConnection.Instance.GetConnection())
    {
        connection1.Open();  
        using(OleDbCommand sqlcmd2 = new OleDbCommand("queryname", connection1))
        {
            sqlcmd2.Parameters.AddWithValue("param1", newvalue); 
            sqlcmd2.Parameters.AddWithValue("param2", oldvalue);
            sqlcmd2.ExecuteNonQuery();
        }
    }
}

Access查询如下所示:

The Access query would look like this:

UPDATE [t]
SET [a] = ?
WHERE [a] = ?

您传递的参数名称无关紧要,这是您传递参数的顺序.

The names of the parameters you pass on don't matter, it's the order you pass them as.

通过使用"using"语句,您可以确保.NET正确释放了连接和资源.

By using the "using" statement you are ensure .NET is properly releasing the connections and resources.

此外,我强烈建议切换到SQL Server Express Edition.它是免费的,而且功能比Access中的功能强大得多.真的,您只是在Access中继续前进而已.

Additionally I STRONGLY recommend switching to SQL Server Express Edition. It's free and a LOT more potent than what you can cook up in Access. Really, you're just shooting yourself in the foot continuing in Access...

这篇关于传递参数以从C#访问查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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