简体   繁体   中英

C# Getting an Error inserting data and image into SQL Server 2008 database

I'm trying to insert a column and image into a SQL Server 2008 database but i am getting 2 errors

Error 1
The best overloaded method match for 'System.IO.BinaryReader.BinaryReader(System.IO.Stream)' has some invalid arguments (line 62 column 35)

Error 2 Argument 1: cannot convert from 'datagrid.FileStream' to 'System.IO.Stream' (line 62 column 52)

I'm confused What to do ...

        try
        {
            byte[] img = null;
            FileStream fs = new FileStream(picLoc, FileMode.Open, FileAccess.Read);
            BinaryReader br = new BinaryReader(fs);
            img = br.ReadBytes((int)fs.Length);
            string sql = "INSERT INTO PicTable(Name,Image) VALUES(" + textBox1.Text + ",@IMG)";
            if (conn.State != ConnectionState.Open)
                conn.Open();
            command = new SqlCommand(sql, conn);
            command.Parameters.Add(new SqlParameter("@IMG", img));
            int x = command.ExecuteNonQuery();
            MessageBox.Show(x.ToString() + " records saved.");
            conn.Close();

            pictEmp.Image = null;
        }
        catch (Exception ex)
        {
            conn.Close();
            MessageBox.Show(ex.Message);
        }

It looks like this

在此处输入图片说明

Based on the errors, FileStream is not coming from the System.IO namespace, it is coming from the dataGrid namespace.

Fully qualifying the FileStream should resolve your issue:

System.IO.FileStream fs = new System.IO.FileStream(picLoc, FileMode.Open, FileAccess.Read);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM