sql

时间:2019-11-30 04:55:41

标签: c# sql-server ssms

我无法从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");
        }

dbh是一种DBHandler类型,这当然是该函数的来源

在sql数据库中,与此有关的@infoReq的数据类型有点(在sql中:[firstTime] BIT NOT NULL

我的代码有什么问题?

1 个答案:

答案 0 :(得分:2)

尝试一下:

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