1

i have design an access query which seem like this:

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

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#.

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

I am using access 2007.

Thanks for your help :)

Fionnuala
  • 90,370
  • 7
  • 114
  • 152
safi
  • 3,636
  • 8
  • 24
  • 37
  • If you are expecting complete C# code, then probably you need to show us what you have written so far? – Sachin Shanbhag Mar 02 '11 at 09:59
  • @Sachin Shanbhag: i have made the connection and everything, i just need to know about the method how to do it. i have written my own code to make the connection and other stuff to update insert but this is something i need to do... `OleDbConnection connection1 = (OleDbConnection)DatabaseConnection.Instance.GetConnection(); connection1.Open(); OleDbCommand sqlcmd2 = new OleDbCommand("update [t] set [a]=@name where [a]=@f", ren_connection1); sqlcmd2.Parameters.AddWithValue("b", name); sqlcmd2.Parameters.AddWithValue("a", path);` – safi Mar 02 '11 at 10:01
  • @safi - good. Can you update your question with this code, it will help in answering. – Sachin Shanbhag Mar 02 '11 at 10:04
  • @Sachin Shanbhag: yes sure. i will do it. – safi Mar 02 '11 at 10:05
  • @Anuraj: c#, ACCESS (the query is designed in access and i want to call it in c#, pass it two values, i am using oledb. – safi Mar 02 '11 at 10:17
  • possible duplicate of [error in stored procedure Access](http://stackoverflow.com/questions/5170117/error-in-stored-procedure-access) – Fionnuala Mar 02 '11 at 21:59
  • @Remou. no i check it and it was not one of the reason. – safi Mar 03 '11 at 10:14

3 Answers3

3

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

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.

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

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...

Vincent Vancalbergh
  • 3,267
  • 2
  • 22
  • 25
  • i just need to know how to pass value to access query? – safi Mar 02 '11 at 10:18
  • @safi: So you need to call an existing access query that has some parameters? Or is it ok that your c# code contains the query (like in my answer)? – Vincent Vancalbergh Mar 02 '11 at 10:30
  • i want to call an existing query which i desgin in access,and pass it two values. thanks – safi Mar 02 '11 at 10:33
  • @Vincent Vacanlbergh: Yes I know, i will switch to it, but these are some bugs which i have to fix :) – safi Mar 02 '11 at 10:38
  • I adjusted my answer to call an existing Access query. – Vincent Vancalbergh Mar 02 '11 at 12:04
  • @Vincent VancalBergh:i have an access query defined as ` SELECT replace(filepath,?,?) as expr1 FROM Thumbnail;` in access DB. so how can i use it here, i specify the query name as updatequery in access, but it doesnt workÆ) – safi Mar 02 '11 at 13:50
  • As far as I can see, this is one of three questions, I have replied to #1: http://stackoverflow.com/questions/5156695/update-table-access/5174280#5174280 – Fionnuala Mar 02 '11 at 22:25
  • @Vincent Vancalbergh: What in the original question suggests to you that the capabilities of the Jet/ACE database are being exceeded here, or that a server database is an appropriate solution? Looks to me like you are just an Access bigot, rather than having any real justification for making the suggestion. Jet/ACE is certainly not appropriate in all situations, but you imply that it is never appropriate. While that may be your opinion, it's not factually-based. – David-W-Fenton Mar 04 '11 at 04:26
  • @David: It's the kind of inconsistencies the OP is running into that drove me away from Access. Sure, it's a nice "all in one system". It has tables, forms, reporting, ideal at first glance. But more often than not I found myself fighting "little annoyances" than actually getting things done. You use the term "bigot" a bit to lightly, almost as if my dislike is unjust :). – Vincent Vancalbergh Mar 04 '11 at 14:12
  • I don't see anything here that is an "inconsistency" in Access. It might reflect a lack of knowledge of the distinction between Access and Jet/ACE and how expressions are resolved in Access/Jet/ACE SQL. There's nothing inconsistent here at all. The rules are well established and have been that way for as long as Access has been around. If you don't know about them, it might appear inconsistent, but the issue in that case is your lack of knowledge, not the design of Access/Jet/ACE. – David-W-Fenton Mar 05 '11 at 21:18
  • 1
    @David It's not because you are aware of how a certain quirk operates and you know how to circumvent it's behavior that it ceases to be inconsistent. In Access you can freely use REPLACE, in Jet you can't. SQL Server has no such shenanigans (or at least to a degree that it can ben considered non-existant). – Vincent Vancalbergh Mar 07 '11 at 10:23
0

You execute it like this:

sqlcmd2.ExecuteNonQuery();

Cosmin
  • 2,365
  • 2
  • 23
  • 29
  • i am sorry but i put the code there because people might think i am looking for a bunch of code and i am not, i am looking for help, and what you wrote was just part of the example..if you had read the answer carefully :) i vote up now. – safi Mar 02 '11 at 10:35
-1

I think you need to modify code like this

    string q = "SELECT Replace(names,'{0}','{1}') AS Expr1 FROM table1";
//You can provide any values instead of LION and KISS
    string query = string.format(q,"LION","KISS") 

    using(OleDbConnection cnn = (OleDbConnection)DatabaseConnection.Instance.GetConnection())
    {
        cnn.Open();  
        using(OleDbCommand cmd = new OleDbCommand(query, cnn))
            {
            OleDbDataReader reader = cmd.ExecuteReader();
            while(reader.Read())
            {
                Console.WriteLine(reader["Expr1"].toString());
            }
            }
    }
Anuraj
  • 18,859
  • 7
  • 53
  • 79
  • thanks, and in this case, string.format(q,"lion","kiss") so lion and kiss should be passed as parameter? and oledbcommand("query",cnn) in this case query is the name which i design on the database side access? – safi Mar 02 '11 at 10:28
  • 3
    -1 You should use command parameters and don't format query as string, this is bad practice for security reasons. – Darius Kucinskas Mar 02 '11 at 10:34
  • @safi: I don't think you can pass those two as parameters because we are replacing it in C# not in SQL. – Anuraj Mar 02 '11 at 10:38
  • @Darius Kucinskas: I agree. Please look into the query. How can I pass parameters to it? in the reply to my comment in the question he mentioned like he want to use replace in C# not sql. – Anuraj Mar 02 '11 at 10:42
  • @Anuraj Yes I am sorry I was too quick to comment, taking back my -1 :) – Darius Kucinskas Mar 02 '11 at 10:46
  • @Anuraj:i get this exception : OLEDBEXCEPTION: UNDEFINED FUNCTION REPLACE IN EXPRESSION. – safi Mar 02 '11 at 10:47
  • @Safi: Oops. Sorry my mistake. I modified the answer. Please try now. – Anuraj Mar 02 '11 at 11:30
  • @Anuraj: thanks i will try it now, and i was on lunch :)come back now – safi Mar 02 '11 at 12:01
  • @Anuraj: I tried it and it is still giving me the same error:( – safi Mar 02 '11 at 12:23
  • Try this string q = "SELECT Replace([names],'{0}','{1}') AS Expr1 FROM table1" – Anuraj Mar 02 '11 at 12:39
  • @Anuraj: still the same error, It do not recognize the Replace. I am using Oledb, access and C#. Do you know anyother way? – safi Mar 02 '11 at 13:16
  • Sorry :( I am not able to test it because I don't have a Database to test – Anuraj Mar 02 '11 at 13:47
  • @Anuraj: Anyway thanks, but can you tell me how to call an access select sql query in c#? – safi Mar 02 '11 at 14:04
  • @Anuraj: I have now write oledbcommand and now i am doing by that, but i got an error, there is an error in stored procedure? this is how i wrote the stored procedure: `SELECT Replace(name,?,?) AS expr1 FROM Table ; ` I am passing two string to the procedure. – safi Mar 02 '11 at 15:33