使用 SQL 命令从 C# (VS 2005) 中的表中检索整数

发布于 2024-08-02 11:20:15 字数 934 浏览 3 评论 0原文

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_WageRent_AllowanceSeniority_AllowanceOvertime_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妄断弥空 2024-08-09 11:20:15

使用 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.

熟人话多 2024-08-09 11:20:15

您只需将值转换为整数:

rank += Convert.ToInt32(dtable1.Rows[0].ItemArray[0]);

rank += (int)(dtable1.Rows[0].ItemArray[0]);

You just need to cast the values to integers:

rank += Convert.ToInt32(dtable1.Rows[0].ItemArray[0]);

or

rank += (int)(dtable1.Rows[0].ItemArray[0]);
牵强ㄟ 2024-08-09 11:20:15

[我会发表评论,但我没有得到 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文