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();
}
}
...全文
194 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

111,129

社区成员

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

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

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