使用 SQL 命令从 C# (VS 2005) 中的表中检索整数
string sql="", sql2 = "", rank="";
int basic_wage, rent_allowance, seniority_allowance, overtime_rate;
sql += "select rank, Basic_Wage,Rent_Allowance,Seniority_Allowance,Overtime_rate from Designation_Details where Designation_ID=(select Designation_ID from Officer where Member_ID=" + id + ")";
sql2 += "select Overtime_Hours from Officer where Member_ID=" + id;
DataTable dtable1 = DataAccess.GetDataTable(sql);
DataTable dtable2 = DataAccess.GetDataTable(sql2);
rank+=dtable1.Rows[0].ItemArray[0];
basic_wage = dtable1.Rows[0].ItemArray[1];
rent_allowance = dtable1.Rows[0].ItemArray[2];
seniority_allowance = dtable1.Rows[0].ItemArray[3];
overtime_rate = dtable1.Rows[0].ItemArray[4];
表中的bBasic_Wage
、Rent_Allowance
、Seniority_Allowance
和Overtime_rate
均为int数据类型。
错误消息显示:
...无法将对象隐式转换为 int。存在显式转换...
string sql="", sql2 = "", rank="";
int basic_wage, rent_allowance, seniority_allowance, overtime_rate;
sql += "select rank, Basic_Wage,Rent_Allowance,Seniority_Allowance,Overtime_rate from Designation_Details where Designation_ID=(select Designation_ID from Officer where Member_ID=" + id + ")";
sql2 += "select Overtime_Hours from Officer where Member_ID=" + id;
DataTable dtable1 = DataAccess.GetDataTable(sql);
DataTable dtable2 = DataAccess.GetDataTable(sql2);
rank+=dtable1.Rows[0].ItemArray[0];
basic_wage = dtable1.Rows[0].ItemArray[1];
rent_allowance = dtable1.Rows[0].ItemArray[2];
seniority_allowance = dtable1.Rows[0].ItemArray[3];
overtime_rate = dtable1.Rows[0].ItemArray[4];
bBasic_Wage
,Rent_Allowance
,Seniority_Allowance
, and Overtime_rate
are all int data types in the table.
The error message says:
....cannot implicitly convert object to int. An explicit conversion exists...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 Convert.ToInt32,它们有时会以十进制形式返回,但仍然具有正确的值。
另外,如果可能的话,至少使用列名,而不是索引。其他开发人员发现这个习惯真的很烦人。
Use the Convert.ToInt32, they sometimes come back as decimal, but still have the correct value.
Also if possible, use at least column names, not indexes. Other developers find this habit really annoying.
您只需将值转换为整数:
或
You just need to cast the values to integers:
or
[我会发表评论,但我没有得到 15 个随机点...]
1 - 根据 Yuriy,你应该使用列名称。它不仅使它更易于阅读,而且如果(以及当)有人编辑您的 SQL 查询时,您的代码也会变得更加健壮。两年后,当您不再参与该项目时,这种情况就会发生,但保持友善是件好事
2 - 您必须习惯使用绑定变量。您当前的代码最容易受到 SQL 注入攻击,并且也可能效率低下(取决于您的数据库的设置方式)。相信我(尽管我有 1 分),这会在以后的某个时候拯救你 - 也许不是在这个项目上 - 但它会在某个时候拯救你
[I would leave a comment but I haven't got 15 random points...]
1 - As per Yuriy you should use column names. It not only makes it easier to read, but makes your code more robust if (and when) someone edits you SQL query. This will happen 2 years from now when you are no longer on the project, but its nice to be nice
2 - You MUST get used to using bind variables. Your current code is prime for SQL injection attacks and is also potentially inefficient (depending on how your DB is setup). Trust me (even though I have 1 point) that this will save you at some point later on - maybe not on this project - but it will at some point