无效令牌'='在类、结构或接口成员声明中,无效令牌'('在类、结构或接口成员声明中
本文关键字:接口 成员 声明 令牌 结构 在类 无效 | 更新日期: 2023-09-27 17:54:50
我想返回存储在数据库中的最后一条记录,并想实例化该返回值到类ReligionCaste的成员pID,但是我有标题错误。
我的数据库功能是
public int LastEnteredRecord()
{
int lastId=0;
dbConnection = new SqlConnection(connectionString);
try //If error in connection opening, throws it.
{
dbConnection.Open();
command.Connection = dbConnection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.LastRecordEntered";
try //if error in query execution, throws it.
{
lastId= Convert.ToInt32 ( command.ExecuteScalar());
// MessageBox.Show("Record Entered with ID:"+lastId .ToString ());
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
dbConnection.Close();
dbConnection.Dispose();
}
catch (SqlException ex)
{
MessageBox.Show(ex.Message);
}
return lastId;
}
我想在代码
时将返回值赋给一个类class ReligionCaste
{
//public int religion_ID, caste_ID;
public String religion, sect, caste, subCaste;
public int pID;
DatabaseHandler.DBCntrlr dbObj = new DatabaseHandler.DBCntrlr();
pID = dbObj.LastEnteredRecord();
}
您需要一个构造函数来为pId分配值,目前您的语法无效。例子:
class ReligionCaste
{
public ReligionCaste()
{
pID = dbObj.LastEnteredRecord();
}
//public int religion_ID, caste_ID;
public String religion, sect, caste, subCaste;
public int pID;
DatabaseHandler.DBCntrlr dbObj = new DatabaseHandler.DBCntrlr();
}
class ReligionCaste
{
//public int religion_ID, caste_ID;
public String religion, sect, caste, subCaste;
public int pID;
private DatabaseHandler.DBCntrlr dbObj;
public ReligionCaste()
{
dbObj = new DatabaseHandler.DBCntrlr();
pID = dbObj.LastEnteredRecord();
}
}