c# - 使用 sql 出现错误“输入字符串的格式不正确”
问题描述
我无法从我的 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
这件事的DataType有点(在sql中[firstTime] BIT NOT NULL
:)
我的代码有什么问题?
解决方案
尝试这个:
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");
}
推荐阅读
- java - 如何延迟 Spring JPA 自动配置直到 bean 运行?
- flutter - 一张幻灯片中有多个项目。颤振 - 飞镖
- ios - 使用 alamofire 上传 pdf 文件
- gradle - gradle buildSrc IntelliJ 中未解决的参考 JavaVersion
- c++ - 如何将大数组复制到内存并在 OpenCL 内核中使用?
- java - 如何为多类继承生成openapi规范?
- numpy - 从 rfft2 数组创建 fft2 结果
- image-processing - 使用自定义内核/图像过滤器在二维数组中查找特定模式
- ruby-on-rails-3 - 如何使用 Nokogiri 使用 onClick 事件侦听器抓取网站
- html - 如何删除进度条底部的这条白线