我试图通过此方法从数据库访问中获取信息
public List<gerant> getinfogerant()
{
List<gerant> gerer = new List<gerant>();
string sql_gerant = "select CIN,NOM,PRENOM,ADRESS_PERSONNEL,NUM_TEL,MAIL,MOBILE,CP_GERANT,VILLE_GERANT,DATE_CIN from GERANT";
connexion connect = new connexion();
OleDbConnection connection = connect.getconnexion();
connection.Open();
OleDbCommand cmd = new OleDbCommand(sql_gerant, connection);
OleDbDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
gerer.Add(new gerant(reader.GetInt64(0),
reader.GetString(1),
reader.GetString(2),
reader.GetString(3),
reader.GetDouble(4),
reader.GetString(5),
reader.GetDouble(6),
reader.GetInt32(7),
reader.GetString(8),
reader.GetDateTime(9))
);
}
connection.Close();
return gerer;
}
在我的数据库访问中,我将字段cin
定义为长整数并格式化为&#34; 00000000&#34;
但我在reader.GetInt64(0)
中收到错误:
指定的演员表无效
我该如何解决?
答案 0 :(得分:1)
试试此代码
public List<gerant> getinfogerant()
{
List<gerant> gerer = new List<gerant>();
try
{
connexion connect = new connexion();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = new OleDbConnection(connect.getconnexion());
cmd.CommandType = CommandType.Text;
comd.CommandText = "select CIN,NOM,PRENOM,ADRESS_PERSONNEL,NUM_TEL,MAIL,MOBILE,CP_GERANT,VILLE_GERANT,DATE_CIN from GERANT";
connection.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
gerant g = new gerant();
if (!reader.IsDBNull(0)) g.CIN = int.Parse(reader.GetValue(0).ToString());
if (!reader.IsDBNull(1)) g.NOM = reader.GetValue(1).ToString();
if (!reader.IsDBNull(2)) g.PRENOM = reader.GetValue(2).ToString();
if (!reader.IsDBNull(3)) g.ADRESS_PERSONNEL = reader.GetValue(3).ToString();
if (!reader.IsDBNull(4)) g.NUM_TEL = Convert.ToDouble(reader.GetValue(4).ToString());
if (!reader.IsDBNull(5)) g.MAIL = reader.GetValue(5).ToString();
if (!reader.IsDBNull(6)) g.MOBILE =Convert.ToDouble(reader.GetValue(6).ToString());
if (!reader.IsDBNull(7)) g.CP_GERANT = int.Parse(reader.GetValue(7).ToString());
if (!reader.IsDBNull(8)) g.VILLE_GERANT = reader.GetValue(8).ToString();
if (!reader.IsDBNull(9)) g.DATE_CIN = Convert.ToDateTime(reader.GetValue(9).ToString());
gerer.add(g);
}
return gerer;
}
catch(Exception ex)
{
throw ex;
}
finally
{
reader.Close();
connection.Close();
}
}
请调整您的课程和方法的名称: - public List getinfogerant() - &gt; public List getInfoGerant()
答案 1 :(得分:0)
Access中的整数有1,2和4字节变量。单字节编号命名为Byte(范围0-255),双字节编号命名为Integer(-32768到32767),然后是Long Integer(20亿到20亿)。
https://eggerapps.at/mdbviewer/docs/en/field-types.html 使用getint32
答案 2 :(得分:0)
请检查DBNULL值,可能是您获得空值。
此致 Jasbeer Singh
答案 3 :(得分:0)
我想,你不是reader.GetInt64(0)
而是gerant
构造函数中的异常,所以它可以是任何列。
要检查此修改代码,请按以下步骤
while (reader.Read())
{
var cin = reader.GetInt64(0);
var nom = reader.GetString(1);
var prenom = reader.GetString(2);
var addressPersonel = reader.GetString(3);
var numTel = reader.GetDouble(4);
var mail = reader.GetString(5);
var mobile = reader.GetDouble(6);
var cpGerant = reader.GetInt32(7);
var villeGerant = reader.GetString(8);
var dateCin = reader.GetDateTime(9);
gerer.Add(new gerant(cin,
nom,
prenom,
addressPersonel,
numTel,
mail,
mobile,
cpGerant,
villeGerant,
dateCin)
);
}
你会在实际行中得到例外。
我建议查看NUM_TEL
列和MOBILE
double
号码。
另外,请在阅读之前检查非string
列的DbNull
值。 long
,int
,double
和DateTime
不能为空。