上传文件并将其存储在Sql Server 2008数据库中。 [英] Upload Files and Store it in Sql Server 2008 Database.

查看:99
本文介绍了上传文件并将其存储在Sql Server 2008数据库中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我正在开展一个名为Job portal网站的项目,其中已经发布了注册表单。在那个注册表单上传文件控件按钮就在那里。我想为上传文件编写一些代码,它应该接受所有类型的文件,如Doc,xls,pdf等,然后它应该在sql database 2008中存储。



那么如何在sql server 2008中保存文件?请帮助\



这里是Sql Code ...





 创建  proc  [dbo]。[proc_RegistrationMaster] 

@ UserName varchar 100 ),
@ CreatePassword varchar 100 ),
@ FirstName varchar 100 ),
@ LastName varchar 100 ),
@ Gender varchar 10 ),
@ Addresss varchar (max),
@EmailID varchar 50 ),
@ MobileNo bigint
@LandLineNo bigint
@ UG_Graduation varchar 50 ),
@ PG_Graduation varchar 50 ),
@ Doctorate varchar 50 ),
@ ResumeHeadLine varchar 100 ),
@TotalExperince varchar 50 ),
@ CurrentCTC varchar 50 ),
@ ExpectedCTC varchar 50 ),
@ FunctionalArea varchar 100 ),
@ IndustryDomain Varchar 100 ),
@ UploadResume varbinary (max)

as
begin
插入 进入 RegistrationMasters


@ UserName
@ CreatePasswo rd
@ FirstName
@ LastName
@ Gender
@ Addresss
@EmailID
@ MobileNo
@LandLineNo
@ UG_Graduation
@ PG_Graduation
< span class =code-sdkkeyword> @ Doctorate ,
@ ResumeHeadLine
@TotalExperince
@ CurrentCTC
@ ExpectedCTC
@ FunctionalArea
@ IndustryDomain
@ UploadResume

end

这是c#代码...



  public   void  btn_Register_Click( object  sender,EventArgs e)
{
string Connstrr = ConfigurationManager.ConnectionStrings [ MyConnectionString ]的ConnectionString。
SqlConnection Conn = new SqlConnection(Connstrr);
Conn.Open();

// Sql Insert Command

< span class =code-keyword> string
filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
Stream str = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(str);
字节 [] size = br.ReadBytes(( int )str.Length);

SqlCommand insertCommand = new SqlCommand( proc_RegistrationMaster,康涅狄格州);
insertCommand.CommandType = System.Data.CommandType.StoredProcedure;

insertCommand.Parameters.Add( @ UserName,txtUserName.Text .ToUpper());
insertCommand.Parameters.Add( @ CreatePassword,txtCreatePassword.Text.ToUpper() );
insertCommand.Parameters.Add( @ FirstName,txtFirstName.Text.ToUpper() );
insertCommand.Parameters.Add( @ LastName,txtLastName.Text.ToUpper() );
insertCommand.Parameters.Add( @ Gender,RBGender.SelectedItem.Text.ToUpper ());
insertCommand.Parameters.Add( @ Addresss,txtAddress.Text.ToUpper() );
insertCommand.Parameters.Add( @ EmailID,txtemailID.Text.ToUpper() );
insertCommand.Parameters.Add( @ MobileNo,txtMobileNo.Text);
insertCommand.Parameters.Add( @LandLineNo,txtLandlineNo.Text);
insertCommand.Parameters.Add( @ UG_Graduation,ddUGGraduation.SelectedItem.Text.ToUpper ());
insertCommand.Parameters.Add( @ PG_Graduation,ddPGGraduation.SelectedItem.Text.ToUpper ());
insertCommand.Parameters.Add( @ Doctorate,ddDoctorate.Text.ToUpper() );
insertCommand.Parameters.Add( @ ResumeHeadLine,txtResumeHeadline.Text);
insertCommand.Parameters.Add( @ TotalExperince,ddTotalExperienceFrom.SelectedItem.Text + < span class =code-string>
+ ddTotalExperienceTo.SelectedItem.Text + );
insertCommand.Parameters.Add( @ CurrentCTC,ddCurrentCTCLakh.SelectedItem.Text + < span class =code-string> Lakh + ddCurrentCTCThousand.SelectedItem.Text + );
insertCommand.Parameters.Add( @ ExpectedCTC,ddExpectedCTCLakh.SelectedItem.Text + < span class =code-string> Lakh + ddexpectedCTCThousand.SelectedItem.Text + );
insertCommand.Parameters.Add( @ FunctionalArea,ddFunctionalArea.SelectedItem.Text);
insertCommand.Parameters.Add( @ IndustryDomain,ddIndustriesDomain.SelectedItem.Text);
insertCommand.Parameters.AddWithValue( @ UploadResume Application / Word);
insertCommand.ExecuteNonQuery();





现在当我运行此代码时出现错误。错误是

不允许从数据类型nvarchar到varbinary(max)的隐式转换。使用CONVERT函数运行此查询。



伙计们请帮助

解决方案

你在做什么是完全错误的。



和你一样提到@UploadResume参数是varbinary所以你需要传递二进制数据。



看你必须这样做。



1.如果你保存在一个页面上,还有很多其他字段,包括文件上传。

2.填写所有字段后还选择文件(doc / pdf / xls /等等。)当你点击代码后面的提交按钮后,你必须这样做。



3.查看所有文本框,你可以很容易得到的下拉值对于上传文件部分,你必须检查

< asp:fileupload id =   FileUpload runat =   server xmlns:asp =  #unknown  /> 



  if (FileUpload.HasFile)
{
HttpPostedFile myFile = FileUpload.PostedFile;
int nFileLen = myFile.ContentLength;
byte [] myData = new byte < /跨度> [nFileLen];
myFile.InputStream.Read(myData, 0 ,nFileLen);
}





4.您必须将mydata作为@UploadResume的值参数传递。



5.完成。


检查此链接,看看它是否对您有所帮助:

uploading-and-storing-files- in-sql-using-asp-net [ ^ ]

aspnet-download-and-upload-files [ ^ ]

保存档案到使用SQL-Server的数据库 - - 文件上传,控制 [ ^

Hi Guys, I am working on a project called Job portal website, where Registration form has taken place. And in that Registration form "Upload File" control Button is there. I wants to write some code for Upload files and its should accept all types of files like Doc, xls, pdf, etc then it shoud get store in sql database 2008.

So how to save files in sql server 2008?? Please Help\

here is the Sql Code...


Create proc [dbo].[proc_RegistrationMaster]
(
@UserName varchar(100),
@CreatePassword varchar(100),
@FirstName varchar(100),
@LastName varchar(100),
@Gender varchar(10),
@Addresss varchar(max),
@EmailID varchar(50),
@MobileNo bigint,
@LandLineNo bigint,
@UG_Graduation varchar(50),
@PG_Graduation varchar(50),
@Doctorate varchar (50),
@ResumeHeadLine varchar(100),
@TotalExperince varchar(50),
@CurrentCTC varchar(50),
@ExpectedCTC varchar(50),
@FunctionalArea varchar(100),
@IndustryDomain Varchar(100),
@UploadResume varbinary(max)
)
as
begin
insert into RegistrationMasters
values
(
@UserName,
@CreatePassword,
@FirstName, 
@LastName,
@Gender, 
@Addresss,
@EmailID,
@MobileNo,
@LandLineNo,
@UG_Graduation,
@PG_Graduation,
@Doctorate,
@ResumeHeadLine,
@TotalExperince,
@CurrentCTC,
@ExpectedCTC,
@FunctionalArea,
@IndustryDomain,
@UploadResume
)
end

here is the c# code...

 public void btn_Register_Click(object sender, EventArgs e)
        {
            string Connstrr = ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString;
            SqlConnection Conn = new SqlConnection(Connstrr);
            Conn.Open();
            
            //Sql Insert Command 

string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            Stream str = FileUpload1.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(str);
            Byte[] size = br.ReadBytes((int)str.Length);

            SqlCommand insertCommand = new SqlCommand("proc_RegistrationMaster",Conn);
            insertCommand.CommandType = System.Data.CommandType.StoredProcedure;

            insertCommand.Parameters.Add("@UserName",txtUserName.Text.ToUpper());
            insertCommand.Parameters.Add("@CreatePassword",txtCreatePassword.Text.ToUpper());
            insertCommand.Parameters.Add("@FirstName",txtFirstName.Text.ToUpper());
            insertCommand.Parameters.Add("@LastName",txtLastName.Text.ToUpper());
            insertCommand.Parameters.Add("@Gender",RBGender.SelectedItem.Text.ToUpper());
            insertCommand.Parameters.Add("@Addresss", txtAddress.Text.ToUpper());
            insertCommand.Parameters.Add("@EmailID",txtemailID.Text.ToUpper());
            insertCommand.Parameters.Add("@MobileNo",txtMobileNo.Text);
            insertCommand.Parameters.Add("@LandLineNo",txtLandlineNo.Text);
            insertCommand.Parameters.Add("@UG_Graduation",ddUGGraduation.SelectedItem.Text.ToUpper());
            insertCommand.Parameters.Add("@PG_Graduation",ddPGGraduation.SelectedItem.Text.ToUpper());
            insertCommand.Parameters.Add("@Doctorate",ddDoctorate.Text.ToUpper());
            insertCommand.Parameters.Add("@ResumeHeadLine",txtResumeHeadline.Text);
            insertCommand.Parameters.Add("@TotalExperince",ddTotalExperienceFrom.SelectedItem.Text + "Year" + ddTotalExperienceTo.SelectedItem.Text + "Month");
            insertCommand.Parameters.Add("@CurrentCTC",ddCurrentCTCLakh.SelectedItem.Text + "Lakh" + ddCurrentCTCThousand.SelectedItem.Text + "Thousand");
            insertCommand.Parameters.Add("@ExpectedCTC",ddExpectedCTCLakh.SelectedItem.Text + "Lakh" + ddexpectedCTCThousand.SelectedItem.Text + "Thousand");
            insertCommand.Parameters.Add("@FunctionalArea",ddFunctionalArea.SelectedItem.Text);
            insertCommand.Parameters.Add("@IndustryDomain",ddIndustriesDomain.SelectedItem.Text);
            insertCommand.Parameters.AddWithValue("@UploadResume", "Application/Word");
insertCommand.ExecuteNonQuery();



Now When i Run this Code I am Getting Error. And the Error is
"Implicit conversion from data type nvarchar to varbinary(max) is not allowed. Use the CONVERT function to run this query."

Guys Please Help

解决方案

what you are doing is completely wrong.

as you mention that "@UploadResume" parameter is varbinary so you need to pass binary data.

See you have to do like this.

1.as you save on a page there are lots of other fields including file upload.
2.After filling all fields and also select file (doc/pdf/xls/etc..) for upload when you click on submit button in code behind you have to do this.

3. see all textbox, dropdowns value you can easily get but for upload file portion you have to check

<asp:fileupload id="FileUpload" runat="server" xmlns:asp="#unknown" />


if (FileUpload.HasFile)
            {
                HttpPostedFile myFile = FileUpload.PostedFile;
                int nFileLen = myFile.ContentLength;
                byte[] myData = new byte[nFileLen];
                myFile.InputStream.Read(myData, 0, nFileLen);
            }



4. you have to pass "mydata" as a value parameter for "@UploadResume".

5. Done.


Check this link, see if it helps you:
uploading-and-storing-files-in-sql-using-asp-net[^]
aspnet-download-and-upload-files[^]
Save-Files-to-SQL-Server-Database-using-FileUpload-Control[^]


这篇关于上传文件并将其存储在Sql Server 2008数据库中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆