简体   繁体   中英

Parameterized query that returns TEXT column(s) always returns zero for INT columns

Problemt with C# mySQL ODBC My table

CREATE TABLE `account` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`fbid` varchar(30) NOT NULL,
`fbname` varchar(80) NOT NULL,
`datecreate` datetime NOT NULL,
`ipcreate` varchar(20) NOT NULL,
`datelogin` datetime NOT NULL,
`iplogin` varchar(20) NOT NULL,
`xstatus` int(2) NOT NULL,
`xverstion` int(5) NOT NULL,
`xdata` text NOT NULL,
`xitem` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8

My procedure:

CREATE PROCEDURE `VVVVV_getUserByFbId`(fbid2 varchar(30))
BEGIN
SELECT * from vvvvv_account where vvvvv_account.fbid=fbid2 LIMIT 1;
END

fbid2 is parameter (=408301576730032) in C# code

OdbcConnection connection = new OdbcConnection(constr);
            OdbcCommand cmd;           
            DataTable dt = new DataTable();
            try
            {

                OpenConnection(connection);

                cmd = new OdbcCommand("{call VVVVV_getUserByFbId(?)}", connection);
cmd.Parameters.AddWithValue("@fbid2", "408301576730032");
                cmd.CommandType = CommandType.StoredProcedure;

                OdbcDataAdapter da = new OdbcDataAdapter(cmd);
                da.Fill(dt);
                da.Dispose();
            }
            catch (Exception ex)
            {

            }
            finally
            {
                CloseConnection(connection);
            }
            return dt;

output in C# dt.Rows[0]["id"] always = 0 Not Ok dt.Rows[0]["fbname"] = "ABC" OK

means I can still get data from database normal. But int column alway = 0, varchar, datetime colume is ok;

But if I change the procedure to:

BEGIN
    select * from account where account.fbid='408301576730032' LIMIT 1;
END

In C# "{call VVVVV_getUserByFbId()}" -> id field = 3903

If no parameter (fbid2) or no text fied (xdata, xitem) -> id , xstatus (int fields) return normal. But if an parameter is passed or select xdata -> id (int fields) always = 0;

enter image description here

You have encountered a verified bug in MySQL Connector/ODBC, reported here:

https://bugs.mysql.com/bug.php?id=97191

Since you are using C# you may want to see if MySQL Connector/NET works better for your application.

You should declare a variable in your stored procedure before using it in your select statement. fbid2 does not work if you call it when it's not declared before and when there is no @ before its call in select statement.

CREATE PROCEDURE VVVVV_getUserByFbId
    @fbid2 varchar(30) = null
AS
    BEGIN
        select * from account where account.fbid=@fbid2 LIMIT 1;
    END

Note

Be careful to Alter previous procedure before creating new one with the same name.

UPDATE

If you're trying to pass integer instead of varchar you have to cast it using one of the following ways:

Using CAST function:

select * from account where account.fbid=CAST(@fbid2 as varchar(30)) LIMIT 1;

Using CONVERT function:

select * from account where account.fbid=CONVERT(varchar(30),@fbid2) LIMIT 1;

Using STR function:

select * from account where account.fbid=LTRIM(STR(@fbid2,30)) LIMIT 1;

Please try to convert the returning value to int as follows.

int x = System.Convert.ToInt32(dt.Rows[0]["id"]);

The issue might be that MySql int is not mapping to C# int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM