错误“输入字符串的格式不正确”,使用sql

问题描述 投票:1回答:1

我在从sql数据库中获取int值时遇到麻烦:

if(Convert.ToDouble(dbh.getInfo("firstTime", username))==1)

而且我也尝试过:

if((int)dbh.getInfo("firstTime", username)==1)

这是getInfo函数:

public object getInfo(string infoReq, string username)
        {
            string query = "select (@infoReq) from AccountDB where username like @username";
            try
            {
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    SqlCommand cmd = new SqlCommand(query, con);
                    cmd.Parameters.AddWithValue("@infoReq", infoReq);
                    cmd.Parameters.AddWithValue("@username", username);
                    con.Open();
                    return cmd.ExecuteScalar();
                }
            }
            catch (Exception e){

            }
            return MessageBox.Show("Please check your fields");
        }

[dbhDBHandler类型,当然是该函数的来源

在sql数据库中,此问题的@infoReq的数据类型为一点(在sql中为[firstTime] BIT NOT NULL

我的代码有什么问题?

c# sql-server ssms
1个回答
0
投票

尝试一下:

public object getInfo(string infoReq, string username)
{
    string query = "select @infoReq from AccountDB where username like '%@username%'";
    try
    {
        using (SqlConnection con = new SqlConnection(connectionString))
        {
            SqlCommand cmd = new SqlCommand(query, con);
            cmd.Parameters.AddWithValue("@infoReq", infoReq);
            cmd.Parameters.AddWithValue("@username", username);
            con.Open();
            return cmd.ExecuteScalar();
        }
    }
    catch (Exception e){

    }
    return MessageBox.Show("Please check your fields");
}
© www.soinside.com 2019 - 2024. All rights reserved.