简体   繁体   English

从SQL表获取数据到空变量

[英]Get data from SQL table to empty variable

I'm trying to search in table(called Accounts) for Username and in the line of this user in coulmn 4 that called PhotoId there is string that i need to get and add it to empty string value that i made before. 我正在尝试在表(称为Accounts)中搜索用户名,并在该行的库伦4的该行中调用PhotoId,我需要获取一个字符串并将其添加到之前创建的空字符串值中。 thats what i tryied i want to do it by Query like my code 多数民众赞成在我想通过查询做到这一点,就像我的代码

string ecid = e.CallbackQuery.Message.Chat.Id.ToString();
using (SqlCommand sqlCommand = new SqlCommand("SELECT * from Accounts where Username like @username", con))
{
    con.Open();
    sqlCommand.Parameters.AddWithValue("@username", ecid);
    string variable;
    string userfound = (string)sqlCommand.ExecuteScalar();
    con.Close();
    if (userfound == ecid)
    {
        using (SqlCommand GotPhoto = new SqlCommand("SELECT PhotoId from Accounts where Username like @user", con))
        {
            con.Open();
            GotPhoto.Parameters.AddWithValue("@user", ecid);

            DataPid = GotPhoto.ExecuteScalarAsync().ToString();
            con.Close();

            //  await bot.SendPhotoAsync("xxx", DataPid, capo, parseMode: ParseMode.Markdown, replyMarkup: zz);

            await bot.SendTextMessageAsync(cid, "its been Poster", parseMode: ParseMode.Html);
        }
    }
    else
    {
        MessageBox.Show("Not Working");
    }
}

You are combining a SELECT * ... with ExecuteScalar() , which "returns the value of the first column in the first row of the resultset. Additional rows and columns are ignored."( https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executescalar(v=vs.110).aspx ). 您正在将SELECT * ...ExecuteScalar()结合使用,它“返回结果集第一行中第一列的值。其他行和列将被忽略。”( https://msdn.microsoft.com/ zh-cn / library / system.data.sqlclient.sqlcommand.executescalar(v = vs.110).aspx )。 Probably username is not the first column in the accounts table, so the content of another column is retured and your comparison fails. 用户名可能不是accounts表中的第一列,因此将重新列出另一列的内容,您的比较将失败。

Use SELECT username from Accounts where Username like @username instead. 请使用“ SELECT username from Accounts where Username like @username而“ SELECT username from Accounts where Username like @username改为。

EDIT 编辑

Depending on your data-model you can also combine the two queries into one as you are querying the same row twice. 根据您的数据模型,您还可以将两个查询合并为一个查询,因为您要查询同一行两次。

con.Open();
using (var cmd = new SqlCommand("SELECT PhotoId from Accounts where Username like @user", con)) {
    cmd.Parameters.AddWithValue("@user", ecid);
    using (var reader = await cmd.ExecuteReaderAsync()) {
        if (!reader.HasRows || ! reader.Read()) {
            MessageBox.Show("User not found");
        } else {
            DataPid = cmd.GetYOURDATATYPE(0).ToString();
            //....
        }

    }
}
con.Close();

According to the documentation of ExecuteScalar => 根据ExecuteScalar的文档=>

Executes the query, and returns the first column of the first row in the result set returned by the query. 执行查询,并在查询返回的结果集返回第一行的第一列 Additional columns or rows are ignored. 其他列或行将被忽略。

When you perform string userfound = (string)sqlCommand.ExecuteScalar(); 当执行string userfound = (string)sqlCommand.ExecuteScalar();

You are comparing the username to a value ( but wich one ? the value of the first column in the create table instruction) 您正在将用户名与一个值进行比较(但其中一个?在create table指令中第一列的值)

I Suggest you change the first query to be sure to select username 我建议您更改第一个查询,以确保select username

PS: PS:

  • To be consistant, you can also rename both of your query parameter @username instead of @user and @username because it refers exactly to the same thing. 为了保持一致,您还可以重命名两个查询参数@username而不是@user@username因为它完全指的是同一件事。
  • You can perform only one query instead of two. 您只能执行一个查询,而不是两个。 ( select photoid ... where username = .. , and just check if the result contains one row ) (选择photoid ...其中username = ..,然后检查结果是否包含一行)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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