C# SQL SEVER中的datatime类型 无法转换

南瓜饼 2010-10-20 10:48:29
我想把数据库中datatime转换成string显示出来 但是系统提示异常
无法将类型为“System.DBNull”的对象强制转换为类型“System.String”。
在 System.InvalidCastException 中第一次偶然出现的“MYSchool.exe”类型的异常
我的代码如下:
//查询老师用户
private void selectTeacher()
{
int TeacherId, UserStateId = -1; //教师号 用户状态
string LoginId, LoginPwd, TeacherName, Sex, Birthday, phone, Email; //用户名,密码,姓名,性别,生日,电话,EMail
string a1 = textBox1.Text == "" ? null : textBox1.Text;
string a2 = textBox2.Text == "" ? null: textBox2.Text;
//模糊和多重查询
try
{
//连接查询
string sql = string.Format("select * from Teacher where TeacherId like '%{0}%' and LoginId like '%{1}%'", a1, a2);
//MessageBox.Show("1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
SqlCommand command = new SqlCommand(sql, SQL.connection);
//MessageBox.Show("11", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
SQL.connection.Open();
//MessageBox.Show("111", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
SqlDataReader datareader = command.ExecuteReader(); //执行查询语句
//MessageBox.Show("1111", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
listView2.Items.Clear(); //清空listView2列表
if (!datareader.HasRows)
{
MessageBox.Show("抱歉没有找到你想要的用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
//MessageBox.Show("1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
while (datareader.Read())
{
TeacherId = (int)datareader["TeacherId"]; //教师号
UserStateId = (int)datareader["UserStateId"]; //用户状态Id
string UserState = UserStateId == 1 ? "活动" : "非活动"; //用户状态
LoginId = (string)datareader["LoginId"]; //用户名
LoginPwd = (string)datareader["LoginPwd"]; //密码
TeacherName = (string)datareader["TeacherName"]; //姓名
//MessageBox.Show("1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
Sex = (string)datareader["Sex"]; //性别
//MessageBox.Show("1", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//这里出了问题 数据库中的类型为datatime
Birthday = Convert.ToString(datareader["Birthday"]); //生日
MessageBox.Show(Convert.ToString(datareader["Birthday"]), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
phone = (string)datareader["phone"]; //电话
Email = (string)datareader["Email"]; //Email
//创建一个listView项
ListViewItem Teacher = new ListViewItem(Convert.ToString(TeacherId));
//向listView中添加一个新项
listView1.Items.Add(Teacher);
//添加子项
Teacher.SubItems.AddRange(new string[] { LoginId, LoginPwd, UserState, TeacherName, Sex, Birthday, phone, Email });
//MessageBox.Show(Convert.ToString(StudentId), "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
datareader.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show("查询数据库出错", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
finally
{
SQL.connection.Close();
}
}
...全文
196 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
蔡袅 2010-10-21
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 wenwei19861106 的回复:]
引用 1 楼 wuyq11 的回复:
datareader["Birthday"]==DBnull.Value?"":datareader["Birthday"].ToString();
或设置可空类型nullable

这个方法试了 不行 说上下文中不存在DBnull
我自己乱搞搞出来了 就是 把后面的变量都用Convert.ToString转换 请问这是为什么啊用(string)为什……
[/Quote]

人家手写的,你当然要更改下啦,是DBNull
ycg_893 2010-10-21
  • 打赏
  • 举报
回复
因为读取的值为空值(在NET为DBNULL.Value),这个对象就是一个Null,任何一个Null是不可能转换为其他的值,所以就出错。你需城判断是不是DBNULL.Value,如果不是再转换成其他的值。
南瓜饼 2010-10-20
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]
datareader["Birthday"]==DBnull.Value?"":datareader["Birthday"].ToString();
或设置可空类型nullable
[/Quote]
这个方法试了 不行 说上下文中不存在DBnull
我自己乱搞搞出来了 就是 把后面的变量都用Convert.ToString转换 请问这是为什么啊用(string)为什么就会出异常啊
qgj1655 2010-10-20
  • 打赏
  • 举报
回复
楼上已经正解了,出错原因应该是那个字段值为空了。
wuyq11 2010-10-20
  • 打赏
  • 举报
回复
datareader["Birthday"]==DBnull.Value?"":datareader["Birthday"].ToString();
或设置可空类型nullable
内容概要:本文介绍了一种基于多目标粒子群算法(MOPSO)的微电网优化调度模型,综合考虑风能、光伏、储能系统、柴油发电机、燃气轮机以及与主电网之间的能量交互等多种分布式能源的协同运行。通过构建以运行成本最小化、碳排放最低化和系统可靠性最优化为目标的多目标优化模型,利用Matlab平台实现MOPSO算法求解,完成对微电网在不同运行场景下的能量管理与调度方案优化。该模型能够有效平衡经济性与环保性之间的关系,适用于含多类型分布式电源的复杂微电网系统,具有较强的工程应用价值和科研参考意义; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及工程技术人员,尤其适合从事微电网、智能电网、综合能源系统、可再生能源集成与优化调度等领域研究的专业人士; 使用场景及目标:①用于多能源耦合微电网系统的协同优化调度研究;②支持多目标智能优化算法在能源系统的建模与求解实践,帮助用户掌握MOPSO在实际工程问题的应用方法;③为学术论文复现、毕业设计、科研项目开发提供完整的代码实例与技术支撑; 阅读建议:建议读者结合Matlab代码与理论文档,深入理解目标函数构建、约束条件处理及Pareto最优解集生成机制,重点关注算法参数设置、多目标权衡分析与结果可视化,并可通过调整能源配置或引入新约束进行二次开发与创新研究。

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧