Code to Insert Image into SQL Server - c#

I want to insert an image into the SQL Server database from my Windows Forms application.
This question looks like it was trying to ask what I wanted to find out, but it was closed:
Insert image into SQL Server

Here is the code I used to do that.
Modify this code as needed for the table that you are going to use by viewing the design of your database in Microsoft Management Studio:
public static void InsertImage(int inventoryID, int businessID, FileInfo file, string sqlConnection)
{
var list = new List<byte>();
using (var stream = file.Open(FileMode.Open))
{
var data = new byte[stream.Length];
stream.Read(data, 0, data.Length);
list.AddRange(data);
}
var bmp = System.Drawing.Image.FromFile(file.FullName, true);
using (var conn = new SqlConnection(sqlConnection))
{
conn.Open();
var imageId = -1;
var sqlSelect = "SELECT [ImageId] FROM [dbo].[ImageTable] WHERE [InventoryId]=#InventoryId;";
using (var cmd = new SqlCommand(sqlSelect, conn))
{
cmd.Parameters.Add("#InventoryId", System.Data.SqlDbType.Int).Value = inventoryID;
using (var r = cmd.ExecuteReader())
{
if (r.Read())
{
var o = r["ImageId"];
if ((o != null) && (o != DBNull.Value))
{
imageId = (int)o;
}
}
}
}
if (imageId == -1)
{
var sqlCmd = "INSERT INTO [dbo].[ImageTable] " +
"([InventoryId], [ImageFileName], [ImageSize], [ImageWidth], [ImageHeight], [ImageBytes]) " +
"VALUES " +
"(#InventoryId, #ImageFileName, #ImageSize, #ImageWidth, #ImageHeight, #ImageBytes); ";
using (var cmd = new SqlCommand(sqlCmd, conn))
{
cmd.Parameters.Add("#InventoryId", System.Data.SqlDbType.Int).Value = inventoryID;
cmd.Parameters.Add("#ImageFileName", System.Data.SqlDbType.VarChar, 255).Value = file.Name;
cmd.Parameters.Add("#ImageSize", System.Data.SqlDbType.Int).Value = list.Count;
cmd.Parameters.Add("#ImageWidth", System.Data.SqlDbType.SmallInt).Value = bmp.Width;
cmd.Parameters.Add("#ImageHeight", System.Data.SqlDbType.SmallInt).Value = bmp.Height;
cmd.Parameters.Add("#ImageBytes", System.Data.SqlDbType.VarBinary, -1).Value = list.ToArray();
cmd.ExecuteNonQuery();
}
}
}
}
To run/test the code, I created this helper method:
public static string[] GetImages(string fullFolderPath, string searchPattern)
{
var list = new List<String>();
if (Directory.Exists(fullFolderPath))
{
if (String.IsNullOrEmpty(searchPattern))
{
searchPattern = "*.jpg";
}
var dir = new DirectoryInfo(fullFolderPath);
var files = dir.GetFiles(searchPattern);
for (int i = 0; i < files.Length; i++)
{
InsertImage(i + 1, 1, files[i], _sqlConnection);
list.Add(files[i].FullName);
}
}
return list.ToArray();
}
Now, running it from my Console Application is a simple, single call:
static void Main(string[] args)
{
var list = GetImages(#"C:\inetpub\wwwroot\Ads", "*.jpg");
}

Related

How to programmatically upload DataTable and DataGrid at same time

I'm trying to create a table in my application that will list new objects that I will work with later. The problem is that I don't get the data in the DataGrid, but if the LoadAlertGrip() method is separate and under the ButtonClick event, it works as it should, but it doesn't change automatically when a new object is created. Do you know what to do?
public AdminWindow()
{
InitializeComponent();
Task.Run(() => Test(new EmailParser(true, "username" , "password")) );
}
public void Test(EmailParser emailParser)
{
bool help = true;
do
{
using (var client = new ImapClient())
{
using (var cancel = new System.Threading.CancellationTokenSource())
{
client.Connect(emailParser.ServerName, emailParser.Port, emailParser.IsSSLuse, cancel.Token);
client.Authenticate(emailParser.Username, emailParser.Password, cancel.Token);
var inbox = client.Inbox;
inbox.Open(FolderAccess.ReadOnly, cancel.Token);
for (int i = 0; i < inbox.Count; i++)
{
var message = inbox.GetMessage(i, cancel.Token);
GetBodyText = message.TextBody;
Problem problem = new Problem(message.MessageId);
if (!dAOProblem.GetAll().Any(x => x.Message_Id.Equals(problem.Message_Id)))
{
dAOProblem.Save(problem);
Alert alert = new Alert(message.MessageId, message.Date.DateTime, message.From.ToString(), 1, problem.Id);
if (!dAOAlert.GetAll().Any(x => x.Id_MimeMessage.Equals(alert.Id_MimeMessage)))
{
dAOAlert.Save(alert);
LoadAlertGrid();
}
}
}
//client.Disconnect(true, cancel.Token);
}
}
} while (help != false);
}
public void LoadAlertGrid()
{
SqlConnection con = DatabaseSingleton.GetInstance();
using (SqlCommand comm = new SqlCommand("SELECT Alert.email, Alert.date, AMUser.name AS 'admin', Problem.nameOfAlert, Result.result " +
"FROM ((Alert INNER JOIN AMUser ON Alert.AMUser_ID = AMUser.id)" +
" INNER JOIN Problem ON Alert.Problem_ID = Problem.id " +
"INNER JOIN Result ON Problem.Result_ID = Result.id); ", con))
{
DataTable table = new DataTable();
SqlDataReader sdr = comm.ExecuteReader();
table.Load(sdr);
AlertTable.ItemsSource = table.DefaultView;
sdr.Close();
}
}

HangFire running each job separately

I am using HangFire to run the job. I have a method that retrieve data from more than 50 sql servers.
I want to use HangFire to run each location separately by LocationID, and if 1 location fails I want that job get running again for that location after x minutes.
Also I want to see the log of the job on the HangFire Dashboard.
My job method :
public void SyncCompInfo()
{
List<Location> locations;
using (var db = new CompInfoDemoEntities())
{
locations = db.Locations.Where(x => x.IsActive).ToList();
}
foreach (var location in locations)
{
try
{
using (var _db = new CompInfoDemoEntities())
{
_log.Info("Getting CompoInfo data from location: " + location.StoreName);
_syncLogService.Add("Getting CompoInfo data from location: " + location.StoreName);
var responses = new List<CompInfoResponse>();
var compInfos = _db.IMS_CompInfo.Where(x => x.LocationId == location.Id).ToList();
using (var cnn = new SqlConnection(location.ConnectionString))
{
var sql = "select * from IMS_CompInfo;";
var sqlCmd = new SqlCommand(sql, cnn);
cnn.Open();
using (SqlDataReader rdr = sqlCmd.ExecuteReader())
{
while (rdr.Read())
{
var item = new CompInfoResponse();
item.Id = int.Parse(rdr["Id"].ToString());
item.ClientID = rdr["ClientID"].ToString();
item.LicenceID = rdr["LicenceID"].ToString();
item.POSCode = rdr["POSCode"].ToString();
item.logiPOS_Version = rdr["logiPOS_Version"].ToString();
if (rdr["LastLoginDate"] != null)
{
item.LastLoginDate = DateTime.Parse(rdr["LastLoginDate"].ToString());
}
item.ComputerName = rdr["ComputerName"].ToString();
if (rdr["BootTime"] != null)
{
item.BootTime = DateTime.Parse(rdr["BootTime"].ToString());
}
item.Domain = rdr["Domain"].ToString();
item.Manufacturer = rdr["Manufacturer"].ToString();
item.Model = rdr["Model"].ToString();
item.Memory = rdr["Memory"].ToString();
item.OS = rdr["OS"].ToString();
item.Build = rdr["Build"].ToString();
item.CPU = rdr["CPU"].ToString();
item.ProcArchitecture = rdr["ProcArchitecture"].ToString();
item.IP1 = rdr["IP1"].ToString();
item.MAC1 = rdr["MAC1"].ToString();
if (rdr["LastModifiedDate"] != null)
{
item.LastModifiedDate = DateTime.Parse(rdr["LastModifiedDate"].ToString());
}
if (rdr["Tag"] != null)
{
item.Tag = int.Parse(rdr["Tag"].ToString());
}
item.Application = rdr["Application"].ToString();
responses.Add(item);
}
}
}
What you seem to need is something like this, with a method you call upon startup, which loops other the locations and enqueues a job for each location.
I over simplified the thing (for example making the methods static),
but I guess most of the idea is there.
Have a look at Hangfire recurring tasks I guess it may be better suited to your needs than tasks fired upon application startups.
public void CalledUponStartup()
{
List<Location> locations;
using (var db = new CompInfoDemoEntities())
{
locations = db.Locations.Where(x => x.IsActive).ToList();
}
foreach (var location in locations)
{
BackgroundJob.Enqueue(() => SyncCompInfo(location.Id));
}
}
public static void SyncCompInfo(int locationId)
{
try
{
using (var _db = new CompInfoDemoEntities())
{
var location = db.Locations.FirstOrDefault(x => x.Id == locationId);
_log.Info("Getting CompoInfo data from location: " + location.StoreName);
_syncLogService.Add("Getting CompoInfo data from location: " + location.StoreName);
var responses = new List<CompInfoResponse>();
var compInfos = _db.IMS_CompInfo.Where(x => x.LocationId == location.Id).ToList();
using (var cnn = new SqlConnection(location.ConnectionString))
{
var sql = "select * from IMS_CompInfo;";
var sqlCmd = new SqlCommand(sql, cnn);
cnn.Open();
using (SqlDataReader rdr = sqlCmd.ExecuteReader())
{
while (rdr.Read())
{
var item = new CompInfoResponse();
item.Id = int.Parse(rdr["Id"].ToString());
item.ClientID = rdr["ClientID"].ToString();
item.LicenceID = rdr["LicenceID"].ToString();
item.POSCode = rdr["POSCode"].ToString();
item.logiPOS_Version = rdr["logiPOS_Version"].ToString();
if (rdr["LastLoginDate"] != null)
{
item.LastLoginDate = DateTime.Parse(rdr["LastLoginDate"].ToString());
}
item.ComputerName = rdr["ComputerName"].ToString();
if (rdr["BootTime"] != null)
{
item.BootTime = DateTime.Parse(rdr["BootTime"].ToString());
}
item.Domain = rdr["Domain"].ToString();
item.Manufacturer = rdr["Manufacturer"].ToString();
item.Model = rdr["Model"].ToString();
item.Memory = rdr["Memory"].ToString();
item.OS = rdr["OS"].ToString();
item.Build = rdr["Build"].ToString();
item.CPU = rdr["CPU"].ToString();
item.ProcArchitecture = rdr["ProcArchitecture"].ToString();
item.IP1 = rdr["IP1"].ToString();
item.MAC1 = rdr["MAC1"].ToString();
if (rdr["LastModifiedDate"] != null)
{
item.LastModifiedDate = DateTime.Parse(rdr["LastModifiedDate"].ToString());
}
if (rdr["Tag"] != null)
{
item.Tag = int.Parse(rdr["Tag"].ToString());
}
item.Application = rdr["Application"].ToString();
responses.Add(item);
}
}

How to change decimal separator in ExecuteReader c#

How to change decimal separator in string, e.g. in mnoz_obj item the returned value is 24,000 and I need to have 24.000. The values are from database to JSON.
I tried ToString(new CultureInfo etc.) but this doesn't work. I expect that myString.Replace(",",".") is not correct way to do it.
public static string getDoklad()
{
var dbCon = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString;
string[] fileArguments = Environment.GetCommandLineArgs();
List<ZebraPolozky> zebraPolozky = new List<ZebraPolozky>();
using (var cn = new OdbcConnection(dbCon))
{
OdbcCommand cmd = cn.CreateCommand();
cmd.CommandText = "SELECT * FROM cis06zebrap";
cn.Open();
using (var reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
ZebraPolozky zebraPolozka = new ZebraPolozky
{
doklad = reader["doklad"].ToString(),
sklad = reader["sklad"].ToString(),
reg = reader["reg"].ToString(),
mnoz_obj = reader["mnoz_obj"].ToString(),
mnoz_vyd = reader["mnoz_vyd"].ToString(),
kc_pce = reader["kc_pce"].ToString(),
sarze = reader["sarze"].ToString(),
datspo = reader["datspo"].ToString(),
veb = reader["veb"].ToString(),
poc2 = reader["poc2"].ToString(),
pvp06pk = reader["pvp06pk"].ToString(),
znacky = reader["znacky"].ToString(),
stav = reader["stav"].ToString(),
//prac = reader["prac"].ToString(),
//exp = reader["exp"].ToString()
};
zebraPolozky.Add(zebraPolozka);
}
}
}
cn.Close();
}
//var collw = new { polozky = zebraPolozky };
var jsonString = JsonConvert.SerializeObject(zebraPolozky);
return jsonString;
}
{
"doklad": "568375",
"sklad": "901",
"reg": "185121",
"mnoz_obj": "24,000",
"mnoz_vyd": "0,000",
"kc_pce": "240,72",
"sarze": "",
"datspo": "",
"veb": "24,00",
"poc2": "1",
"pvp06pk": "116783437",
"znacky": "R1902",
"stav": "0"
}
OdbcDataReader gives the value in its native format as stated in the doc.
You should then be able to cast it and use the overload of .ToString() you need.
Try something like:
((decimal)reader["mnoz_obj"]).ToString("N2")

Windows service stop automatically after first successful run

I have created windows service scheduler which run after 24 hr, When i stared the service first time it enters data in database it run perfectly fine.
But in second run service get stops , however i want service run continually.
I have created scheduler for it as well to run it every minute , but data only inserts when time match .
public void StartService(object e)
{
try {
//ExtractorEventLog("0", null);
List<PracticeInformation> getServiceTime = GetpracticeInformationList();
var gettime = (from t in getServiceTime
select new PracticeInformation
{
extractor_start_time = t.extractor_start_time
}).OrderByDescending(x => x.id).ToList().FirstOrDefault();
DateTime SetdateTime = DateTime.Parse(ConfigurationManager.AppSettings["ScheduledTime"]);
if (gettime != null)
{
SetdateTime = DateTime.Parse(gettime.extractor_start_time.ToString());
}
try
{
if (SetdateTime.ToString("HH:mm") == DateTime.Now.ToString("HH:mm"))
{
ExtractorEventLog(SetdateTime.ToString("HH:mm") + " " + DateTime.Now.ToString("HH:mm"), null);
string strFile = #"C:\requiredFilesForDDP\Temp" + DateTime.Now.Ticks.ToString() + ".csv";
if (connm == "")
{
GetConnectionString();
}
using (OdbcConnection conn = new OdbcConnection(connm))
{
//ExtractorEventLog(connm, null);
conn.Open();
#region Get Hospital data
var id = 0;
try
{
List<PracticeInformation> practiceList = getServiceTime;
var practiceName = practiceList[0].name;
id = practiceList[0].id;
List<PracticeInformationModel> HospitalAll = new List<PracticeInformationModel>();
using (OdbcCommand com = new OdbcCommand(Common.cmdGetHospital, conn))
{
using (OdbcDataReader readerHospital = com.ExecuteReader())
{
HospitalAll = DataReaderMapToList<PracticeInformationModel>(readerHospital);
List<PracticeInformationModel> objPracticeInformationmodel = (from pi in HospitalAll
select new PracticeInformationModel
{
practice_name = pi.practice_name,
phone = pi.phone
}
).ToList();
var getPracticeName = practiceList.Select(x => new { x.name }).ToList();
List<PracticeInformationModel> objpracticelist = objPracticeInformationmodel.Where(x => !getPracticeName.Any(y => y.name == x.practice_name)).ToList();
DataTable PracticeInformationDT = ToDataTable(objpracticelist); // To save PracticeInformation table
using (MySqlConnection sqConn = new MySqlConnection(Common.connectionStringDB))
{
using (SqlBulkCopy blkcopyPi = new SqlBulkCopy(sqConn.ConnectionString, SqlBulkCopyOptions.KeepNulls))
{
CreateCSVfile(PracticeInformationDT, strFile);
if (!File.Exists(strFile))
{
FileStream fs = new FileStream(strFile, FileMode.Create, FileAccess.Write);
fs.Close();
fs.Dispose();
}
if (sqConn.State == ConnectionState.Open)
{
sqConn.Close();
}
sqConn.Open();
MySqlBulkLoader bcp1 = new MySqlBulkLoader(sqConn);
bcp1.TableName = "PracticeInformation"; //Create PracticeInformation table into MYSQL database...
bcp1.FieldTerminator = ",";
bcp1.LineTerminator = "\r\n";
bcp1.FileName = strFile;
bcp1.NumberOfLinesToSkip = 0;
bcp1.Load();
try
{
File.Delete(strFile);
}
catch (Exception ex)
{
ExceptionLog(ex.Message, ex.GetType().Name.ToString(), ex.StackTrace, id);
}
}
readerHospital.Close();
}
}
}
#endregion
#region Get Provider Data From Dentrix
List<Provider> ProviderAll = new List<Provider>();
using (OdbcCommand com = new OdbcCommand(Common.cmdGetProvider, conn))
{
using (OdbcDataReader readerProvider = com.ExecuteReader())
{
ProviderAll = DataReaderMapToList<Provider>(readerProvider);
List<Provider> objProvider = (from pro in ProviderAll
select new Provider
{
provider_id = pro.provider_id,
first_name = pro.first_name,
last_name = pro.last_name,
work_phone = pro.work_phone
}).ToList();
}
}
#endregion
#region Add Appointment
using (OdbcCommand com = new OdbcCommand(Common.cmdText, conn))
{
using (OdbcDataReader reader = com.ExecuteReader())
{
List<AllModalFromApplointment> AppointmentListAll = new List<AllModalFromApplointment>();
AppointmentListAll = DataReaderMapToList<AllModalFromApplointment>(reader);
List<Alert> patientalert_id = new List<Alert>();
#region Get alert data
MySqlConnection connection = null;
List<string> target = new List<string>();
EventLog(Common.Get_Pre_Med_Alerts);
ExtractorEventLog(Common.Get_Pre_Med_Alerts, id.ToString());
int add = 0;
int alert_count = 0;
//string connectionStringDB = "server=192.168.0.1;user=dentrix;database=db_dentrix;password=dentrix";
connection = new MySqlConnection(Common.connectionStringDB);
foreach (var i in AppointmentListAll.Select(x => x.patient_id).ToList())
{
if (i != 0)
{
OdbcCommand ODBCCommand = new OdbcCommand("{call admin.sp_getpatientmedalerts (?)}", conn);
ODBCCommand.CommandType = CommandType.StoredProcedure;
ODBCCommand.Parameters.AddWithValue("#patient_guid", i); //4898-11738
ODBCCommand.ExecuteNonQuery();
OdbcDataReader a = ODBCCommand.ExecuteReader();
DataTable getAlert = new DataTable();
if (a.HasRows)
{
target.Add(i.ToString());
alert_count = add + 1;
getAlert.Load(a);
a.Close();
}
}
}
ExtractorEventLog(target.Count() + " " + Common.No_Of_Pre_Med_Alert, null);
#endregion
#region
List<AppointmentDetail> objAppointment = (from app in AppointmentListAll
join pro in ProviderAll on app.provider_id equals pro.provider_id
select new AppointmentDetail
{
patient_id = Convert.ToInt64(app.patient_guid),
appointment_id = Convert.ToInt64(app.appointment_id),
appointment_timestamp = Convert.ToString(app.appointment_date.AddHours(app.start_hour).AddMinutes(app.start_minute)),
patient_first_name = app.patient_name.Split(',')[0],
patient_last_name = app.patient_name.Split(',')[1],
patient_phone_number = app.patient_phone,
red_cross_flag = target.Any(x => x.ToString().Contains(app.patient_id.ToString())) ? "Y" : "N",
dr_name = pro.first_name.Trim() + ' ' + pro.last_name.Trim(),
dr_phone = HospitalAll[0].phone,
hospital_name = HospitalAll[0].practice_name,
appointment_type = null,
fees = 0,
status = null
//createdDate = DateTime.UtcNow
}).ToList();
//Ristrict duplicate records
ExtractorEventLog(Common.Data_Inserting, id.ToString());
List<AppointmentDetail> usethe = GetAppointmentList();
var query = usethe.Select(x => new { x.appointment_id }).ToList();
List<AppointmentDetail> xyz = objAppointment.Where(x => !query.Any(y => y.appointment_id == x.appointment_id)).ToList();
if (xyz != null && xyz.Count != 0)
{
var from = xyz.First().appointment_timestamp;
var to = xyz.Last().appointment_timestamp;
ExtractorEventLog(Common.Get_Appointment_Date + from + "and" + to, id.ToString());
}
else
{
EventLog("No appointmet present");
}
int appointment_count = xyz.Count();
DataTable appointmentDT = ToDataTable(xyz); // To save appointment table
using (MySqlConnection sqConn = new MySqlConnection(Common.connectionStringDB))
{
using (SqlBulkCopy blkcopy = new SqlBulkCopy(sqConn.ConnectionString, SqlBulkCopyOptions.KeepNulls))
{
CreateCSVfile(appointmentDT, strFile);
if (!File.Exists(strFile))
{
FileStream fs = new FileStream(strFile, FileMode.Create, FileAccess.Write);
fs.Close();
fs.Dispose();
}
if (sqConn.State == ConnectionState.Open)
{
sqConn.Close();
}
sqConn.Open();
MySqlBulkLoader bcp1 = new MySqlBulkLoader(sqConn);
bcp1.TableName = "appointment";
//bcp1.TableName = "Appointment"; //Create Appointment table into MYSQL database.
bcp1.FieldTerminator = ",";
bcp1.LineTerminator = "\r\n";
bcp1.FileName = strFile;
bcp1.NumberOfLinesToSkip = 0;
bcp1.Load();
try
{
File.Delete(strFile);
}
catch (Exception ex)
{
ExtractorEventLog("appointment-ex", null);
ExceptionLog(ex.Message, ex.GetType().Name.ToString(), ex.StackTrace, id);
}
ExtractorEventLog(appointment_count + " " + Common.No_Of_Appointment_Retrive, id.ToString());
}
ExtractorEventLog(Common.Done, id.ToString());
this.ScheduleService();
}
#endregion
}
}
}
catch (Exception ex)
{
ExtractorEventLog("a-ex", null);
ExceptionLog(ex.Message, ex.GetType().Name.ToString(), ex.StackTrace, id);
}
}
}
else{
this.ScheduleService();
}
}
catch (Exception ex)
{
ExtractorEventLog(ex.Message +"b-ex", null);
//ExceptionLog(ex.Message, ex.GetType().Name.ToString(), ex.StackTrace, id);
}
//EventLog("this ScheduleService()");
// this.ScheduleService();m
}
catch(Exception ex)
{
ExtractorEventLog("zz-ex", null);
}
}

How can get result from OleDbDataReader with C# method

How can return this from method ????
this is important for me
thank
this is my method :
public static dynamic GetFactorProperties(int factornumber)
{
using (var db = new OleDbConnection(cs))
{
db.Open();
var cmd = new OleDbCommand("SELECT * FROM FactorList WHERE FactorNumber = #0", db);
cmd.Parameters.AddWithValue("#0", factornumber);
var result = cmd.ExecuteReader();
if(result.Read())
{
return result ;
}
else
{
return ProperiesObject();
}
}
}
and this is my ExpandoObject()
public static dynamic ProperiesObject()
{
dynamic obj = new ExpandoObject();
obj.FactorDate = string.Empty;
obj.FactorNumber = string.Empty;
obj.CustomerName = string.Empty;
obj.FactorPrice = string.Empty;
obj.Discount = string.Empty;
return obj;
}
and this is read from method source :
var result = Program.GetFactorProperties(factornumber);
TXTDate.Text = result.FactorDate;
TXTFactorNumber.Text = result.FactorNumber;
TXTCustomerName.Text = result.CustomerName;
TXTFactorParice.Text = result.FactorPrice;
TXTDiscount.Text = result.Discount;
Method should look like this:
public static dynamic GetFactorProperties(int factornumber)
{
using (var db = new OleDbConnection(cs))
{
db.Open();
var cmd = new OleDbCommand("SELECT * FROM FactorList WHERE FactorNumber = #0", db);
cmd.Parameters.AddWithValue("#0", factornumber);
var result = cmd.ExecuteReader();
if(result.Read())
{
dynamic obj = new ExpandoObject();
obj.FactorDate = result.GetString(0);
obj.FactorNumber = result.GetString(1);
// ...
return obj;
}
else
{
return ProperiesObject();
}
}
}
GetString accepts number of column, from which you want to get value

Categories