在将文件保存到SQL数据库中时出错 [英] getting error to save a file in a SQL databese

查看:69
本文介绍了在将文件保存到SQL数据库中时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在SQl中创建了一个表.
桌子是这样的..

I have created a table in SQl.
The table is like this..

Create table filee
(
id varchar(5),
filee nvarchar(max)
)


之后,我要在(filee)列中保存一个(.doc)文件.
我已经编写了一个代码,但是此代码给出了错误.
代码是这样的.....


After that I want to save a (.doc) file in filee column.
I have written a code but this code give error.
The code is like this.....

    FileStream fs = new FileStream(flname, FileMode.Open, FileAccess.Read);
            byte[] data = new byte[fs.Length];
            fs.Read(data, 0, System.Convert.ToInt32(fs.Length));


            SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Mith;Integrated Security=True");
            SqlDataAdapter da = new SqlDataAdapter("Select * from filee",con);
            con.Open();
            DataSet ds = new DataSet();
            da.Fill(ds, "filee");
            DataRow dr = ds.Tables["filee"].NewRow();

            dr[0] = textBox1.Text;

            dr[1] = data;

            try
            {
                ds.Tables["filee"].Rows.Add(dr);
                da.Update(ds, "filee");
                MessageBox.Show("Data Inserted");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
          finaly
{
con.close()
}

推荐答案

HI,
在上面的代码中,您无处创建任何文件,也无处读取任何文件.


解决方法如下:


In the above code nowhere ur creating any file, and from nowhere ur reading any file.


Here is the solution :

protected void btnCreate_Click(object sender, EventArgs e)
    {
        FileStream createFile = new FileStream(@"C:\Docs\file2.docx", FileMode.OpenOrCreate, FileAccess.ReadWrite);
        createFile.Close();
        StreamWriter sw = new StreamWriter(@"C:\Docs\file2.docx");
        sw.Write("Tajuddin............");
        sw.Close();
        StreamReader sr=new StreamReader (@"c:\docs\file2.docx");
       string fileData=sr.ReadToEnd ();
       sr.Close();
       //after this u can write insert query to store in file column
       string sqlquery = string.Format("insert into filee values('{0}','{1}')", "123", fileData);
       SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Mith;Integrated Security=True");
       con.Open();
       SqlCommand cmd = new SqlCommand(sqlquery, con);
       cmd.ExecuteNonQuery();
       

    }


使数据类型文本代替nvarchar(max)
make your datatype text in place of nvarchar(max)


这篇关于在将文件保存到SQL数据库中时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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