c# - 我的 MySql 'SELECT' 命令有问题
问题描述
我想从我的表中获取第一个 id 并将其写入label16.text
为我的程序创建页面。为什么以下代码返回 system.threading.tasks.unwrappromise'1[system.object]?
await sqlConnection.OpenAsync();
SqlCommand command = new SqlCommand("SELECT Id from Recipes order by id asc limit 1", sqlConnection);
label16.Text = command.ExecuteScalarAsync().Result.ToString();
解决方案
你应该改变
label16.Text = command.ExecuteScalarAsync().Result.ToString();
至
var t = await command.ExecuteScalarAsync();
label16.Text = t.Unwrap().ToString();
为了填写答案,我也会从评论中粘贴这个。
你必须做的另一件事是修复你的 SQL 查询,它应该是
SELECT TOP(1) Id from Recipes order by id asc
第三个问题
我会这样做
try
{
var id = int.Parse(label16.Text);
sqlDataAdapter = new SqlDataAdapter($"SELECT * FROM Recipes WHERE Id = {id}", sqlConnection);
} catch { // error handling }
推荐阅读
- google-bigquery - QlikSense 尝试访问 BigQuery - ODBC 错误
- java - 如何使用更好的策略将基于 if 的逻辑替换为取决于布尔值?
- mysql - 将字符附加到sql中的字符串
- flutter - 如何重新启动在完成前停止但参数不同的动画
- typescript - 你如何在 Typescript 中验证 IP 地址?
- python - 如何在 Python 上使用 Opencv 在内存中编码/解码 Webp 格式?
- amazon-web-services - 本地测试时出现 AWS 状态机错误:''SCHEMA_VALIDATION_FAILED:值不是有效的资源 ARN
- r - 我可以使用 data.tables 加速 R 和 Sqlite 之间的计算吗?
- ios - 无需用户输入的 iOS NFC 背景标签读取
- django-2.2 - 为 django 2.2 升级 django-synchro:OperationalError - 没有这样的表:django_content_type"