如何从表单向SQL数据库添加图像 [英] How to add images to SQL database from a form
本文介绍了如何从表单向SQL数据库添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个asp.net表单,其中包含要输入日志的多个字段。
我现在要求将图像插入某些日志条目以记录正在输入的事件但是我不知道如何以我目前的形式这样做。我想添加一个文件上传,并将内容与文本框和下拉条目一起插入。
这是我的命令背后的代码。
I have an asp.net form that contains multiple fields to be entered to a log.
I now have a requirement to insert images to certain log entries to record the event that is being entered but I have no idea how to do this in my current form. I wanted to add a file upload and have the contents inserted along with the textbox and dropdown entries.
Here is my code behind in vb with my command.
Protected Sub ButtonSubmitInc_Click(sender As Object, e As EventArgs) Handles ButtonSubmitInc.Click
Dim Conn As New SqlClient.SqlConnection
Conn.ConnectionString = "Data Source=VS15IIS01;Initial Catalog=SecurityLog;User ID=sa;Password=W@sp@crmc;MultipleActiveResultSets=True;Application Name=EntityFramework"
Dim command As SqlCommand
Dim sqlString, inc_date, officer_name, time_in, time_out, helicopter_name, heli_patient_count, helicopter_comments, helicopter_status, patrol, patrol_comments, patrol_last_name, patrol_first_name, patrol_pat_DOB, patrol_mr_number, inc_er, er_comments, er_DOB, er_last_name, er_first_name, er_mr_number, inc_person, person_comments, person_last_name, person_first_name, person_DOB, person_mr_number, inc_property, property_comments, property_last_name, property_first_name, property_DOB, property_mr_number, property_value, inc_alarms, alarm_comments, inc_phy_sec, phy_sec_comments, phy_last_name, phy_first_name, phy_DOB, phy_mr_number, inc_pub_assist, pub_assist_comments, inc_misc, misc_comments, inc_other, other_comments, bag_count As String
officer_name = DDIncOfficerName.SelectedValue.ToString
patrol = DDPatrol.SelectedValue.ToString
inc_date = TxtDateInc.Text
time_in = DDStart.SelectedValue.ToString
time_out = DDEnd.SelectedValue.ToString
helicopter_name = TextBoxHelicopterName.Text
heli_patient_count = TextBoxHeliPatCount.Text
helicopter_comments = TextBoxHelicopterComments.Text
helicopter_status = DropDownListStatus.SelectedValue.ToString
patrol_comments = TextBoxPatrolComments.Text
patrol_last_name = TextBoxPatrolPatLastName.Text
patrol_first_name = TextBoxPatrolPatFirstName.Text
patrol_pat_DOB = TextBoxPatrolPatDOB.Text
patrol_mr_number = TextBoxPatrolPatMrNumber.Text
inc_er = DropDownListERInc.SelectedValue.ToString
er_comments = TextBoxERIncComments.Text
er_DOB = TextBoxERDOB.Text
er_last_name = TextBoxERPatLastName.Text
er_first_name = TextBoxERPatFirstName.Text
er_mr_number = TextBoxERPatMRNumber.Text
inc_person = DDPersonIncident.SelectedValue.ToString
person_comments = TxtPersonComments.Text
person_last_name = TextBoxPerPatLastName.Text
person_first_name = TextBoxPerPatFirstName.Text
person_DOB = TextBoxPersonPatDOB.Text
person_mr_number = TextBoxPerPatMrNum.Text
inc_property = DDPropertyIncident.SelectedValue.ToString
property_comments = TxtPropertyComments.Text
property_last_name = TextBoxPropPatLastNamer.Text
property_first_name = TextBoxPropPatFirstName.Text
property_DOB = TextBoxPropertyPatDOB.Text
property_mr_number = TextBoxPropPatMrNumber.Text
property_value = TextBoxValueLoss.Text
inc_alarms = DDAlarmsInc.SelectedValue.ToString
alarm_comments = TxtAlarmsComments.Text
inc_phy_sec = DDPhysicalSecurity.SelectedValue.ToString
phy_sec_comments = TxtPhySecComments.Text
phy_last_name = TextBoxPhyPatLastName.Text
phy_first_name = TextBoxPhyPatFirstName.Text
phy_DOB = TextBoxPhySecPatDOB.Text
phy_mr_number = TextBoxPhyPatMrNumber.Text
inc_pub_assist = DDPubAssistIncident.SelectedValue.ToString
pub_assist_comments = TxtPubAssistComments.Text
inc_misc = DDMiscInc.SelectedValue.ToString
misc_comments = TxtMiscComments.Text
inc_other = DDOtherInc.SelectedValue.ToString
bag_count = TxtBagCount.Text
other_comments = TxtOtherComments.Text
Conn = New SqlConnection("Data Source=VS15IIS01;Initial Catalog=SecurityLog;User ID=sa;Password=W@sp@crmc;MultipleActiveResultSets=True;Application Name=EntityFramework")
Conn.Open()
sqlString = "INSERT INTO Security_Log (officer_name, inc_date, time_in, time_out, helicopter_name, heli_patient_count, helicopter_comments, helicopter_status, patrol, patrol_comments, patrol_last_name, patrol_first_name, patrol_pat_DOB, patrol_mr_number, inc_er, er_comments, er_DOB, er_last_name, er_first_name, er_mr_number, inc_person, person_comments, person_last_name, person_first_name, person_DOB, person_mr_number, inc_property, property_comments, property_last_name, property_first_name, property_DOB, property_mr_number, property_value, inc_alarms, alarm_comments, inc_phy_sec, phy_sec_comments, phy_last_name, phy_first_name, phy_DOB, phy_mr_number, inc_pub_assist, pub_assist_comments, inc_misc, misc_comments, inc_other, bag_count, other_comments)VALUES('" + officer_name + "','" + inc_date + "','" + time_in + "','" + time_out + "','" + helicopter_name + "','" + heli_patient_count + "','" + helicopter_comments + "','" + helicopter_status + "', '" + patrol + "','" + patrol_comments + "','" + patrol_last_name + "','" + patrol_first_name + "','" + patrol_pat_DOB + "','" + patrol_mr_number + "','" + inc_er + "','" + er_comments + "','" + er_DOB + "','" + er_last_name + "','" + er_first_name + "','" + er_mr_number + "','" + inc_person + "','" + person_comments + "','" + person_last_name + "','" + person_first_name + "','" + person_DOB + "','" + person_mr_number + "','" + inc_property + "','" + property_comments + "','" + property_last_name + "','" + property_first_name + "','" + property_DOB + "','" + property_mr_number + "','" + property_value + "','" + inc_alarms + "','" + alarm_comments + "','" + inc_phy_sec + "','" + phy_sec_comments + "','" + phy_last_name + "','" + phy_first_name + "','" + phy_DOB + "','" + phy_mr_number + "','" + inc_pub_assist + "','" + pub_assist_comments + "','" + inc_misc + "','" + misc_comments + "','" + inc_other + "','" + bag_count + "','" + other_comments + "')"
command = New SqlCommand(sqlString, Conn)
command.ExecuteNonQuery()
Conn.Close()
推荐答案
我最初的建议是不要。将图像添加到数据库只会让它膨胀。一个更好的选择是将图像保存到文件夹并在数据库中记录路径。
My initial advice is don't. Adding images to your database simply bloats it. A better alternative is to save the image to a folder and record the path in the database instead.
这不好回答已被问过很多次的问题,一次又一次。请参阅: http://www.codeproject.com/search.aspx?q=%28%22.NET%22+OR+%22C%23%22%29+database+%28image + OR +图像+ OR +位图+ OR +位图%29 [ ^ ]。
但是在很多情况下只存储图像文件名(存储在文件系统中的文件)将是一个不错的选择。
-SA
This is not good to answer the question which has been asked so many times, again and again. Please see: http://www.codeproject.com/search.aspx?q=%28%22.NET%22+OR+%22C%23%22%29+database+%28image+OR+images+OR+bitmap+OR+bitmaps%29[^].
But storing only the image file names (of the files stored in the file system) would be a good option in many cases.
—SA
我个人会考虑使用数据库作为图像的位置。使用数据库可以帮助您进行包含图像和备份的事务,如果操作正确,将包含图像。
但是,我不会存储图像直接在数据库内,但会使用文件流。例如,请查看如何存储和获取二进制数据到文件流列 [ ^ ]
Personally I would consider using the database as the location for the images. Using a database helps you to have transactions which include the images and also the backups , when done properly, would include images.
However, I wouldn't store the images directly inside the database but would use a filestream. For example, have a look at How to store and fetch binary data into a file stream column[^]
这篇关于如何从表单向SQL数据库添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文