How to upload image to SQL Server using C# -



How to upload image to SQL Server using C# -

i had info type image column in database set image, have since changed varbinary(max). however, when effort run code , upload image store in column, every record showing null. believe database fine , problem must how passing image info sql query. question this: need modify image uploaded database in right format? image retrieval planned implemented.

here database looks like. included column name, info type, , results when selecting rows. no errors returned when attempted upload image. while column allow null values, each of records attempts upload image.

below code "add" button handles communicating database. have made effort cutting out unrelated problem, such closing form.

private void addbutton_click(object sender, eventargs e) { string strconnect = @"server=mainserver\sqlexpress; database=inventory; integrated security=sspi;"; sqlconnection con = new sqlconnection(strconnect); databaseutility db = new databaseutility(); string sqlstart = @"insert vehicles(manufacturer, model, date_acquired, vin, year, cost"; string sqlend = @" values('" + makebox.text + "', '" + modelbox.text + "', '" + datepicker.text + "', '" + vinbox.text + "', '" + yearbox.text + "', '" + costbox.text; string sql; if (vinbox.textlength != 17) { messagebox.show("the vin entered invalid", "invalid vin"); } else if (string.isnullorwhitespace(makebox.text) || string.isnullorwhitespace(modelbox.text) || string.isnullorwhitespace(yearbox.text) || string.isnullorwhitespace(vinbox.text) || string.isnullorwhitespace(costbox.text)) { messagebox.show("not required fields filled", "missing information"); } else { if (!string.isnullorwhitespace(askingbox.text)) { sqlstart += ", asking_price"; sqlend += "', '" + askingbox.text; } if (!string.isnullorwhitespace(categorycombobox.text)) { sqlstart += ", category"; sqlend += "', '" + categorycombobox.text; } if (!string.isnullorwhitespace(additionalnotesbox.text)) { sqlstart += ", additional_notes"; sqlend += "', '" + additionalnotesbox.text; } if (!openfiledialog1.checkfileexists) { sqlstart += @", image"; sqlend += "', '@image"; } sqlstart += ")"; sqlend += "')"; sql = sqlstart + sqlend; sqlcommand insertcommand = new sqlcommand(sql, con); if (!openfiledialog1.checkfileexists) { sqlparameter sqlparam = insertcommand.parameters.addwithvalue("@image", (object)getimage(openfiledialog1.filename)); sqlparam.dbtype = dbtype.binary; } seek { messagebox.show(insertcommand.tostring(), "invalid input"); con.open(); insertcommand.executenonquery(); con.close(); } grab (exception ex) { messagebox.show("error in insertcommand" + ex, "invalid input"); } { insertcommand.connection.close(); } } }

you seek utilize filedialog.checkfileexists check whether selected file exists, property not work way - gets value indicating whether dialog box displays warning if user specifies file name not exist.

so if property set true - , that's default value openfiledialog - , code adds parameter @image never executed.

if want check whether file exists, utilize file.exists

c# sql sql-server

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -