private ReturnClass ExecSqlProcVoidMethods(string strQuery, bool isProc, SP_Parameters p) { ReturnClass outcome = new ReturnClass(true); int results = 0; try { using (var conn = new SqlConnection(connectionstring)) { conn.Open(); using (var command = new SqlCommand(strQuery, conn)) { command.CommandTimeout = commandtimeout; if (isProc) { command.CommandType = CommandType.StoredProcedure; } else { command.CommandType = CommandType.Text; } if (p != null) { foreach (SqlParameter objparam1 in p) { command.Parameters.Add(objparam1); } } results = command.ExecuteNonQuery(); } } } catch (Exception ex) { outcome.SetFailureMessage("An update query Failed. Please see logs for exact error", "ExecSql error. Query is[" + strQuery + "] Error:[" + ex.Message + "]"); results = -1; } outcome.Intvar = results; return(outcome); }
private ReturnClass SyncScalarMethods(string strQuery, SP_Parameters p, bool isProc, ScalarType sctype) { ReturnClass outcome = new ReturnClass(true); try { using (var conn = new SqlConnection(connectionstring)) { conn.Open(); using (var command = new SqlCommand(strQuery, conn)) { command.CommandTimeout = commandtimeout; if (isProc) { command.CommandType = CommandType.StoredProcedure; } else { command.CommandType = CommandType.Text; } if (p != null) { foreach (SqlParameter objparam1 in p) { command.Parameters.Add(objparam1); } } try { using (var reader = command.ExecuteReader()) { if (reader.Read()) { switch (sctype) { case ScalarType.String: outcome.Message = reader[0].ToString(); break; case ScalarType.Int: outcome.Intvar = (int)reader[0]; break; case ScalarType.Long: outcome.Longvar = (long)reader[0]; break; case ScalarType.Double: outcome.Doublevar = (double)reader[0]; break; } } } } catch (Exception ex) { outcome.SetFailureMessage("A query Failed. Please see logs for exact error", "SyncScalarMethods Error Query is[" + strQuery + "] Error:[" + ex.ToString() + "]"); } } } } catch (SqlException ex) { outcome.SetFailureMessage("A query Failed. Please see logs for exact error", "SyncScalarMethods Error Query is[" + strQuery + "] Sql Error:[" + ex.ToString() + "]"); } catch (Exception ex) { outcome.SetFailureMessage("A query Failed. Please see logs for exact error", "SyncScalarMethods Error Query is[" + strQuery + "] Error:[" + ex.ToString() + "]"); } return(outcome); }
public ReturnClass UploadImageField(string filepath, string imagefieldparametername, string Sql) { ReturnClass outcome = new ReturnClass(true); SP_Parameters p = new SP_Parameters(); SqlCommand cmd = null; Stream imgStream = null; FileInfo file = null; byte[] imgBinaryData = null; int RowsAffected = 0; int filesize = 0; int n = 0; if (!imagefieldparametername.StartsWith("@")) { imagefieldparametername = "@" + imagefieldparametername; } if (!File.Exists(filepath)) { outcome.SetFailureMessage("The file does not exist or is not accessible."); } if (outcome.Success) { try { file = new FileInfo(filepath); filesize = Convert.ToInt32(file.Length); } catch (Exception ex) { outcome.Success = false; outcome.Message = ex.Message; } } if (outcome.Success) { try { imgStream = File.OpenRead(filepath); imgBinaryData = new byte[filesize]; n = imgStream.Read(imgBinaryData, 0, filesize); } catch (Exception ex) { outcome.Success = false; outcome.Message = ex.Message; } } if (outcome.Success) { try { using (var conn = new SqlConnection(connectionstring)) { conn.Open(); cmd = new SqlCommand(Sql, conn); if (commandtimeout > 0) { cmd.CommandTimeout = commandtimeout; } p.Add(imagefieldparametername, SqlDbType.Image, filesize, ParameterDirection.Input, imgBinaryData); foreach (SqlParameter objparam1 in p) { cmd.Parameters.Add(objparam1); } RowsAffected = cmd.ExecuteNonQuery(); } } catch (Exception ex) { outcome.Success = false; outcome.Message = ex.Message; } } try { imgStream.Close(); } catch (Exception ex) { } return(outcome); }