[英]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:
@username
instead of @user
and @username
because it refers exactly to the same thing. @username
而不是@user
和@username
因为它完全指的是同一件事。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.