我对 ASP.NET 很陌生,我正在为我的学校项目制作我的第一个应用程序,但我遇到了一个问题。
我有用于添加文件和图片的数据库。我使用了SqlDataSource
and DataList
,所以我会自动生成插入、更新和删除。在代码隐藏 C# 中,我编写了代码,用于将图像直接添加到数据库和带有进入数据库的路径的文件,但我不知道如何将值(路径)添加到参数,以便可以将它们全部保存在一起。我应该使用什么命令?
也许这是个愚蠢的问题,但我没有人要问。抱歉,如果我的代码在此窗口中看起来有点有趣enter code here
这是我的 C#
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
FileUpload kontrola = (FileUpload)FormView1.FindControl("FileUpload2");
if (kontrola.PostedFile != null && kontrola.PostedFile.FileName != "")
{
BinaryReader reader = new BinaryReader(kontrola.PostedFile.InputStream);
myImage = reader.ReadBytes(kontrola.PostedFile.ContentLength);
}
System.Data.SqlClient.SqlParameter uploadData =
new System.Data.SqlClient.SqlParameter("@image",System.Data.SqlDbType.Image);
uploadData.Value = myImage;
e.Command.Parameters.Add(uploadData);
FileUpload kontrola2 = (FileUpload)FormView1.FindControl("FileUpload1");
//Get Filename from fileupload control
string filename = Path.GetFileName(kontrola2.PostedFile.FileName);
//Save images into Images folder
kontrola2.SaveAs(Server.MapPath("~/files/" + kontrola2.FileName));
string valp = "files/" + kontrola2.FileName; // I am not sure if this is right
//e.Command.Parameters.Add["@data_file"].Value = valp; // this is not right, but this part I don't know
}
这是我在aspx
.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:repository_dbConnectionString %>"
SelectCommand="SELECT * FROM [data]"
DeleteCommand="DELETE FROM [data] WHERE [id] = @id"
InsertCommand="INSERT INTO [data] ([data_set_name], [abstract], [source], [multivariate], [unvariate], [sequentional], [time_series], [text], [domain_theory], [clasification], [regression], [clustering], [casual_discovery], [categorical], [integer], [real], [life_sciences], [physical_sciences], [cs_engineering], [social_sciences], [business], [game], [other], [matrix], [non_matrix], [yes], [no], [num_istances], [num_attributes], [relevant_info], [attribute_info], [relevant_papers], [citation_request], [data_file], [image], [date], [approval]) VALUES (@data_set_name, @abstract, @source, @multivariate, @unvariate, @sequentional, @time_series, @text, @domain_theory, @clasification, @regression, @clustering, @casual_discovery, @categorical, @integer, @real, @life_sciences, @physical_sciences, @cs_engineering, @social_sciences, @business, @game, @other, @matrix, @non_matrix, @yes, @no, @num_istances, @num_attributes, @relevant_info, @attribute_info, @relevant_papers, @citation_request, @data_file, @image, GetDate(), @approval)"
UpdateCommand...
oninserting="SqlDataSource1_Inserting">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="data_set_name" Type="String" />
<asp:Parameter Name="abstract" Type="String" />
<asp:Parameter Name="source" Type="String" />
<asp:Parameter Name="multivariate" Type="Boolean" />
<asp:Parameter Name="unvariate" Type="Boolean" />
<asp:Parameter Name="sequentional" Type="Boolean" />
<asp:Parameter Name="time_series" Type="Boolean" />
<asp:Parameter Name="text" Type="Boolean" />
<asp:Parameter Name="domain_theory" Type="Boolean" />
<asp:Parameter Name="clasification" Type="Boolean" />
<asp:Parameter Name="regression" Type="Boolean" />
<asp:Parameter Name="clustering" Type="Boolean" />
<asp:Parameter Name="casual_discovery" Type="Boolean" />
<asp:Parameter Name="categorical" Type="Boolean" />
<asp:Parameter Name="integer" Type="Boolean" />
<asp:Parameter Name="real" Type="Boolean" />
<asp:Parameter Name="life_sciences" Type="Boolean" />
<asp:Parameter Name="physical_sciences" Type="Boolean" />
<asp:Parameter Name="cs_engineering" Type="Boolean" />
<asp:Parameter Name="social_sciences" Type="Boolean" />
<asp:Parameter Name="business" Type="Boolean" />
<asp:Parameter Name="game" Type="Boolean" />
<asp:Parameter Name="other" Type="Boolean" />
<asp:Parameter Name="matrix" Type="Boolean" />
<asp:Parameter Name="non_matrix" Type="Boolean" />
<asp:Parameter Name="yes" Type="Boolean" />
<asp:Parameter Name="no" Type="Boolean" />
<asp:Parameter Name="num_istances" Type="Int32" />
<asp:Parameter Name="num_attributes" Type="Int32" />
<asp:Parameter Name="relevant_info" Type="String" />
<asp:Parameter Name="attribute_info" Type="String" />
<asp:Parameter Name="relevant_papers" Type="String" />
<asp:Parameter Name="citation_request" Type="String" />
<asp:Parameter Name="data_file" Type="String" />
<asp:Parameter DbType="Date" Name="date" />
<asp:Parameter Name="approval" Type="Boolean" />
</InsertParameters>