이 코드가 있습니다.
private void obtengoUltimoComienzoColocado()
{
ManejoUsuarios lAdm = new ManejoUsuarios();
lblTodoOK.Text = lAdm.LevantoUltimoIDBarco().ToString();
}
public int LevantoUltimoIDBarco()
{
ConexionBD lP = new ConexionBD();
try
{
string lQuery = "Select Max(idBarco) as UltimoID From Comienzos;";
Convert.ToInt32(lP.ObtenerRegistro(lQuery));
return 1;
}
catch (Exception ex)
{
return 0;
}
}
public DataSet ObtenerRegistro(string SqlQuery)
{
DataSet lResult;
SqlConnection lSqlConnection = new SqlConnection("Data Source=SEBA-PC\\sqlexpress;Initial Catalog=Batalla_Naval;Integrated Security=True");
SqlCommand lSqlCommand = null;
try
{
lSqlCommand = new SqlCommand();
lSqlCommand.CommandText = SqlQuery;
lSqlCommand.CommandType = CommandType.Text;
SqlDataAdapter lAdapter = new SqlDataAdapter(lSqlCommand);
lResult = new DataSet();
lSqlConnection.Open();
lSqlCommand.Connection = lSqlConnection;
lAdapter.Fill(lResult);
}
catch (Exception)
{
throw;
}
finally
{
lSqlConnection.Close();
}
return lResult;
}
보시다시피 세 가지 함수를 사용하여 데이터베이스로 이동하여 Comienzos 테이블에서 최대 ID를 얻지 만 데이터 세트를 int32로 변환하려면 LevantoUltimoIDBarco 함수가 0을 반환합니다.
데이터 세트에서 첫 번째 값을 선택해야하며 전체 데이터 세트를 정수로 변환 할 수 없습니다.
Convert.ToInt32(lP.ObtenerRegistro(lQuery).Tables[0].Rows[0][0]);
또는 더 간단합니다 (쿼리가 데이터 세트에서 정수를 반환하기 때문에) :
(Int32)(lP.ObtenerRegistro(lQuery).Tables[0].Rows[0][0]);
그런 다음 1을 반환하는 대신 결과를 반환하거나 저장해야합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다