当前位置:编程学习 > C#/ASP.NET >>

asp.net C#获取数据库int字段值方法

private static void 使用原始sql命令打印字段值() { sqlconnection conn = new sqlconnection(@"data source=.sqlexpress;initial catalog=图库;integrated security=true"); try { conn.open(); console.writeline("sucess"); } catch (exception) { console.writeline("打开失败"); } sqlcommand comm = conn.createcommand(); comm.commandtext = "select * from 会员数据"; sqldatareader reader = comm.executereader(); while (reader.read()) { console.writeline("你要打印的数据:{0}", reader.getsqlint16(reader.getordinal("年龄"))); } }
image
想使用上面数据库教程用程序来得到所有的年龄,结果老是出错,说是指定的转换无效,自己研究了下,原来数据库中的int字段在转换时需要使用int32,而不是int16
image

改成int32程序就正确运行了

console.writeline("你要打印的数据:{0}", reader.getsqlint32(reader.getordinal("年龄")));
image
补充:asp.net教程,基础入门 
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,