在我的项目中指定的强制转换无效错误 [英] Specified cast is not valid error occure in my project

查看:51
本文介绍了在我的项目中指定的强制转换无效错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

cmdNameShow = new SqlCommand("select * from tblBday where Dob like '" + DtMain.Date.ToString().Substring(0, 5) + "%' or Dob like '" + DtAdv1.ToString().Substring(0, 5) + "%' or dob like '" + DtAdv2.ToString().Substring(0, 5) + "%' or Dob like '" + DtAdv3.ToString().Substring(0, 5) + "%' or dob like '" + DtAdv4.ToString().Substring(0, 5) + "%' or Dob like '" + DtAdv5.ToString().Substring(0, 5) + "%' or dob like '" + DtAdv6.ToString().Substring(0, 5) + "%' or Dob like '" + DtAdv7.ToString().Substring(0, 5) + "%' or dob like '" + DtAdv8.ToString().Substring(0, 5) + "%' or Dob like '" + DtAdv9.ToString().Substring(0, 5) + "%' or dob like '" + DtAdv10.ToString().Substring(0, 5) + "%' or Dob like '" + DtAdv11.ToString().Substring(0, 5) + "%' or dob like '" + DtAdv12.ToString().Substring(0, 5) + "%' or Dob like '" + DtAdv13.ToString().Substring(0, 5) + "%' or dob like '" + DtAdv14.ToString().Substring(0, 5) + "%' or Dob like '" + DtAdv15.ToString().Substring(0, 5) + "%'",con);
            SqlDataReader drNameShow = cmdNameShow.ExecuteReader();
            while (drNameShow.Read())
            {
                lblPname = new Label();
                lblPname.AutoSize = true;
                lblPname.BackColor = System.Drawing.Color.Transparent;
                lblPname.ForeColor = Color.White;
                this.lblPname.Font = new System.Drawing.Font("Microsoft Sans Serif", 7F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                lblPname.Name = drNameShow.GetInt32(0).ToString();
                lblPname.Text = drNameShow.GetString(1).ToUpper() + "  " + drNameShow.GetString(2).ToUpper() + "  " + drNameShow.GetString(4).ToUpper();

推荐答案

首先不使用 SELECT * FROM ,然后使用编号列访问。

开始使用它效率低,因为它会返回你不使用的列,如果数据库格式发生变化并且移动了一列,它也会导致问题。



如果列出了您希望在SELECT语句中返回的列

Start off by not using SELECT * FROM and then using numbered column accesses.
To start with it's inefficient, because it returns columns you don't use, and it also causes problems if the DB format changes and a column is moved.

If you list the columns you expect to return in the SELECT statement
SELECT Name, Text FROM tblBday WHERE ...

然后你确切知道你要返回的是哪些列,然后你是否使用字符串来访问它们而不是索引:

Then you know exactly what columns you are returning, and if you then use a string to access them instead of a index:

lblPname.Name = drNameShow.GetInt32("Name").ToString();

它让您更可靠并且更容易阅读。



另外,当你犯错误时它会变得明显:一个名字可能是一个整数吗?如果没有,为什么要尝试将其转换为一个?



检查您的数据库,使用命名列,它应该自行解决。

It makes your more reliable and easier to read.

Plus, it then become obvious when you make a mistake: Is a name likely to be an integer at all? And if not, why are you trying to convert it to one?

Check your DB, use named columns, and it should fix itself.


除了OG的答案之外,你真的需要停止使用字符串连接来构建SQL查询并使用参数化查询 [ ^ ]。这将使您的代码更容易阅读,更容易调试和支持。
On top of OG's answer, you really need to stop using string concatenation to build the SQL query and use parameterized queries[^]. It'll make you're code much easier to read and much easier to debug and support.


这篇关于在我的项目中指定的强制转换无效错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆