SELECT max(cast( xh as int)) as xh FROM kk.kkcltj
用cast( xh as int)把varchar(2)的类型转成int后再查出最大值
注意:xh的字段里只能存储char型或number型数据,否则会报 无效符号 错误
有时候不主意会写成
SELECT max(cast( xh as int)) FROM kk.kkcltj
这样select出来的值就没有字段名,所以在取xh值的时候会报错
如:
OracleConnection connn = dbc.getConnection();//获得conn连接
try
{
connn.Open();
OracleCommand cmdd = connn.CreateCommand();
cmdd.CommandText = "SELECT max(cast( xh as int)) as xh FROM kk.kkcltj ";//插入之前检查有有无重复数据
OracleDataReader odrRepeat = cmdd.ExecuteReader();//创建一个OracleDateReader对象
if (odrRepeat.Read())//读取数据,如果odr.Read()返回为true的话,就说明到登陆成功了
{
xhMax = Convert.ToInt32(odrRepeat["xh"].ToString());
}
}
catch (Exception ee)
{
MessageBox.Show(ee.Message.ToString());
}
这里的odrRepeat["xh"]就会出错,因为没有xh这个字段,却在取这个字段的值
给字段个名字即可,as xh
黑色头发:http://heisetoufa.iteye.com/admin/blogs/new