本文介绍了上传文件不保存在数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
尝试上传文件并保存在目录和数据库中,当我上传文件时,它保存在目录中但不在数据库中这里是上传文件代码
try to upload document and save in directory and in database also when i upload document it saves in directory but not in database here is upload document code
if (FileUploadControl.PostedFile != null && FileUploadControl.PostedFile.ContentLength
< 102400)
{
string filename = Path.GetFileName(FileUploadControl.PostedFile.FileName);
string folder = Server.MapPath("~/Docfiles/");
Directory.CreateDirectory(folder);
FileUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));
try
{
up.fileupladdd(Txt_docde.Value, txt_dname.Value,
FileUploadControl.FileName, Convert.ToInt32(DropDownList1.SelectedValue),
Convert.ToInt32(DropDownList2.SelectedValue),
Convert.ToInt32(Session["UserID"]),Convert.ToString(Session["UserID"]));
StatusLabel.Text = "Success";
}
catch
{
StatusLabel.Text = "Failed";
}
}
Txt_docde.Value = "";
txt_dname.Value = "";
和sp上传文件是
and sp of upload file is
ALTER procedure [dbo].[fileuplaod]
@DocDesciption nvarchar(50),
@DocName nvarchar(50),
@Uploadfile nvarchar(50),
@DocTypeID int,
@DepID int,
@UserID int
as
insert into DocumentInfo(DocDesciption ,DocName,Uploadfile,DocTypeID,DepID ,UserID)
values(@DocDesciption,@DocName,@Uploadfile,@DocTypeID,@DepID ,@UserID)
where出现问题?
where is the problem occur?
推荐答案
SqlConnection PubsConn = new SqlConnection
("your connection string here");
SqlCommand testCMD = new SqlCommand
("fileuplaod", PubsConn);
testCMD.CommandType = CommandType.StoredProcedure;
SqlParameter DocDesc = testCMD.Parameters.Add
("@DocDesciption", SqlDbType.NVarChar, 50);
DocDesc.Direction = ParameterDirection.Input;
SqlParameter DocName= testCMD.Parameters.Add
("@DocName ", SqlDbType.NVarChar, 50);
DocName.Direction = ParameterDirection.Input;
// Add other parameters as well
DocDesc.Value = "set document description here";
DocName.Value = "set document name here";
//set other parameter values here
PubsConn.Open();
testCMD.ExecuteNonQuery ().ToString() ;
Convert.ToInt32(Session["UserID"]),Convert.ToString(Session["UserID"]));
这里存在错误,因为你发送了两次相同的参数而你的方法只有6个参数
the error exist here because you send the same argument twice and your method has 6 paramater only
public void fileupladdd(string DocDesc, string Docname, string file, int doctypeid, int depid, int UserID, string UploadedBy)
{
//db.ExecuteNonQuery("fileuplaod", new object[] { DocDesc, Docname, file, doctypeid, depid, UserID, UploadedBy });
Hashtable hash = new Hashtable();
hash.Add("@DocDesc", DocDesc);
hash.Add("@Docname", Docname);
hash.Add("@file", file);
hash.Add("@doctypeid", doctypeid);
hash.Add("@depid", depid);
hash.Add("@UserID", UserID);
hash.Add("@UploadedBy", UploadedBy);
ExecuteStoreProcedure("youspname here", hash);
}
public int ExecuteStoreProcedure(string spName, Hashtable param)
{
try
{
using (_DbConn = new SqlConnection(_sConString))
{
_DbConn.Open();
SqlCommand _DbCommand = new SqlCommand();
_DbCommand.CommandText = spName;
_DbCommand.CommandType = CommandType.StoredProcedure;
_DbCommand.Connection = _DbConn;
foreach (string para in param.Keys)
{
_DbCommand.Parameters.AddWithValue(para, param[para]);
}
return _DbCommand.ExecuteNonQuery();
}
}
catch (Exception)
{
throw;
}
}
这篇关于上传文件不保存在数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!