EnVisageOnline/Main/Source/iniPHIClientControlAPI/iniPHIClientControlAPI.Tests/helper/sqlClass.cs

66 lines
2.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace iniPHIClientControlAPI.Tests.helper
{
public class sqlClass
{
private static string m_connectionString = "";
public static int getNextId(string table)
{
int idval = 0;
// Create Instance of Connection and Command Object
SqlConnection connection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["iniPhiClientControlADO"].ConnectionString);
SqlCommand command = new SqlCommand("select IDENT_CURRENT( '"+table+"' )", connection);
try
{
// Open the connection
connection.Open();
// Mark the Command as a StoredProcedure
command.CommandType = CommandType.Text;
// Execute the stored procedure
using (SqlDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow))
{
// Read the data, if returned
if (reader.Read())
{
// Use try catch where necessary to ensure valid values
try
{
idval = Convert.ToInt32(reader[0].ToString());
idval++;
}
catch { idval = -1; }
}
// Close the reader
reader.Close();
}
}
catch (System.Data.SqlClient.SqlException sqlexception)
{
idval = -1;
}
catch (System.Exception exception)
{
idval = -1;
}
finally
{
// Close the connection
if (connection.State == ConnectionState.Open)
connection.Close();
}
// Return the GrantProAidProgram Business object
return idval;
}
}
}