Query working in mysql but not from c# code

Aleksa Ristic

I am having c# code like this:

using (MySqlConnection con = new MySqlConnection(AR.ConnectionString))
{
    con.Open();
    using (MySqlCommand cmd = new MySqlCommand(@"SELECT PORUDZBINAID, USERID, BRDOKKOM, DATUM,
                STATUS, MAGACINID, PPID, INTERNIKOMENTAR, REFERENT_OBRADE, NACIN_PLACANJA, TAG FROM 
                PORUDZBINA WHERE TAG LIKE '%@MOB%'", con))
    {
        cmd.Parameters.AddWithValue("@MOB", Mobilni);

        MySqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
            list.Add(new Porudzbina()
            {
                PorudzbinaID = Convert.ToInt32(dr[0]),
                UserID = Convert.ToInt32(dr[1]),
                BrDokKom = Convert.ToInt32(dr[2]),
                Datum = Convert.ToDateTime(dr[3]),
                Status = (PorudzbinaStatus)Convert.ToInt32(dr[4]),
                MagacinID = Convert.ToInt32(dr[5]),
                PPID = (dr[6] is DBNull) ? (int?)null : Convert.ToInt32(dr[6]),
                InterniKomentar = (dr[7] is DBNull) ? null : dr[7].ToString(),
                ReferentObrade = (dr[8] is DBNull) ? (int?)null : Convert.ToInt32(dr[8]),
                NacinUplate = (PorudzbinaNacinUplate)Convert.ToInt32(dr[9]),
                Tag = JsonConvert.DeserializeObject<Properties>(dr["TAG"].ToString())
            });
    }
}

I put breakpoint and it passes good paramter to query but doesn't enter while() loop (so i is not reading) and it returns no rows.

When i enter same query in my mysql and replace @MOB with parameter passed there, it does return me one row.

I guess problem is something with passing LIKE through c# but not sure why it does that.

jason.kaisersmith

You need to change how you are adding parameters slightly:

In your SQL, no quotes and no % symbols.

using (MySqlCommand cmd = new MySqlCommand(@"SELECT PORUDZBINAID, USERID, BRDOKKOM, DATUM,
                STATUS, MAGACINID, PPID, INTERNIKOMENTAR, REFERENT_OBRADE, NACIN_PLACANJA, TAG FROM 
                PORUDZBINA WHERE TAG LIKE @MOB", con))
    {

Then the parameter like this, without quotes.

cmd.Parameters.AddWithValue("@MOB", "%" + Mobilni + "%");

BTW: Ideally you should not use AddWithValue, but rather Add(). See this blog: https://blogs.msmvps.com/jcoehoorn/blog/2014/05/12/can-we-stop-using-addwithvalue-already/

And this SO post: MySqlCommand Command.Parameters.Add is obsolete

Instead, it should be like this:

cmd.Parameters.Add("@MOB", SqlDbType.Varchar).Value = "%" + Mobilni + "%";
//you must update to use the correct DBType for your data

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

MySQL query in PHP is not working

分類Dev

mysql insert into query not working

分類Dev

How to make a mysql query from c# and save it in a variable?

分類Dev

Mysql query is not working but i could not guess why

分類Dev

Mysql join query help, not working as required

分類Dev

mysql query compare dates, DATEDIFF not working

分類Dev

Mysql select query working but giving error on update query

分類Dev

Sorting Array from a MySql Query

分類Dev

MYSQL INSERT INTO FROM SELECT QUERY

分類Dev

C#: OleDbCommand Update query not working?

分類Dev

Runs in Query Window but not from code what is the problem?

分類Dev

C FILE fgets function working in this code

分類Dev

Comparing mysql password hashes using query row returning 0 (not working)

分類Dev

Search query from multiple fields MySQL

分類Dev

Setting $_SESSION as result from MySQL Query

分類Dev

MYSQL query with array that comes from ajax

分類Dev

Display Upcoming Events with PHP from MySQL query

分類Dev

SQL Server Query not working when executed from PHP

分類Dev

Date range from reports , not working properly because of query

分類Dev

C loop code from assembly

分類Dev

migrating code from gatsby to nextjs and now aws-sdk not working

分類Dev

Code from codepen.io not working when downloaded: floorplan

分類Dev

MySQL Connector C/C API - Query with special characters

分類Dev

How to switch code from db class to Eloquent query? Laravel

分類Dev

run a query code to retrieve array from database without page refresh

分類Dev

mongo query which costs different time from java code and shell

分類Dev

MySQL query inside query

分類Dev

mysql select query with sum and group by from two columns

分類Dev

Capture mysql_real_query from FireDAC connection

Related 関連記事

  1. 1

    MySQL query in PHP is not working

  2. 2

    mysql insert into query not working

  3. 3

    How to make a mysql query from c# and save it in a variable?

  4. 4

    Mysql query is not working but i could not guess why

  5. 5

    Mysql join query help, not working as required

  6. 6

    mysql query compare dates, DATEDIFF not working

  7. 7

    Mysql select query working but giving error on update query

  8. 8

    Sorting Array from a MySql Query

  9. 9

    MYSQL INSERT INTO FROM SELECT QUERY

  10. 10

    C#: OleDbCommand Update query not working?

  11. 11

    Runs in Query Window but not from code what is the problem?

  12. 12

    C FILE fgets function working in this code

  13. 13

    Comparing mysql password hashes using query row returning 0 (not working)

  14. 14

    Search query from multiple fields MySQL

  15. 15

    Setting $_SESSION as result from MySQL Query

  16. 16

    MYSQL query with array that comes from ajax

  17. 17

    Display Upcoming Events with PHP from MySQL query

  18. 18

    SQL Server Query not working when executed from PHP

  19. 19

    Date range from reports , not working properly because of query

  20. 20

    C loop code from assembly

  21. 21

    migrating code from gatsby to nextjs and now aws-sdk not working

  22. 22

    Code from codepen.io not working when downloaded: floorplan

  23. 23

    MySQL Connector C/C API - Query with special characters

  24. 24

    How to switch code from db class to Eloquent query? Laravel

  25. 25

    run a query code to retrieve array from database without page refresh

  26. 26

    mongo query which costs different time from java code and shell

  27. 27

    MySQL query inside query

  28. 28

    mysql select query with sum and group by from two columns

  29. 29

    Capture mysql_real_query from FireDAC connection

ホットタグ

アーカイブ