{
string connectionString = “Data Source=(local);Initial Catalog=CompanyDb;Persist Security Info=True;User ID=sa;Password=123”;
public int SqlExecuteQuery(string strQuery, int timeOut)
{
int returnCount = 0;
SqlConnection sqlConnection = new SqlConnection();
SqlCommand sqlCommand = new SqlCommand();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
sqlCommand.CommandText = strQuery;
sqlCommand.CommandTimeout = timeOut;
sqlCommand.Connection = sqlConnection;
returnCount = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
return returnCount;
}
public DataSet SqlSelectQuery(string strQuery, int timeOut)
{
DataSet ds = new DataSet();
SqlConnection sqlConnection = new SqlConnection();
SqlCommand sqlCommand = new SqlCommand();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
sqlCommand.CommandText = strQuery;
sqlCommand.CommandTimeout = timeOut;
sqlCommand.Connection = sqlConnection;
sqlDataAdapter.Fill(ds);
sqlConnection.Close();
return ds;
}
public object SqlScalarQuery(string strQuery, int timeOut)
{
object returnString = new object();
SqlConnection sqlConnection = new SqlConnection();
SqlCommand sqlCommand = new SqlCommand();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlCommand);
sqlConnection.ConnectionString = connectionString;
sqlConnection.Open();
sqlCommand.CommandText = strQuery;
sqlCommand.CommandTimeout = timeOut;
sqlCommand.Connection = sqlConnection;
returnString = sqlCommand.ExecuteScalar();
sqlConnection.Close();
return returnString;
}
}
———————————————————————————————————-
To generate Max Id
public void GetStudentId()
{
string str = “select isnull(max(id),0)+1 from student”;
TextBox6.Text = Convert.ToString(dm.SqlScalarQuery(str, 1000));
}
———————————————————————————————————–