C# 在数据库中存储图像

C# 在数据库中存储图像,c#,asp.net,C#,Asp.net,我用类型文件在输入中显示图像,然后我想将此图像保存到数据库。我知道如何将varbinary或图像存储到数据库,但我不知道如何访问输入文件 SqlConnection con = new SqlConnection(stcon); SqlCommand command = new SqlCommand(); string path = ""; System.Drawing.Image img = System.Drawing.Im

我用类型文件在输入中显示图像,然后我想将此图像保存到数据库。我知道如何将varbinary或图像存储到数据库,但我不知道如何访问输入文件

SqlConnection con = new SqlConnection(stcon);
        SqlCommand command = new SqlCommand();


            string path = "";
            System.Drawing.Image img = System.Drawing.Image.FromFile(path);
            MemoryStream tmpStream = new MemoryStream();
            img.Save(tmpStream, ImageFormat.Png); // change to other format
            tmpStream.Seek(0, SeekOrigin.Begin);
            byte[] imgBytes = new byte[100000];
            tmpStream.Read(imgBytes, 0, 100000);

            command.CommandText = "INSERT INTO image(image) VALUES (:image)";
            IDataParameter par = command.CreateParameter();
            par.ParameterName = "image";
            par.DbType = DbType.Binary;
            par.Value = imgBytes;
            command.Parameters.Add(par);
            command.ExecuteNonQuery();

您只需使用
System.IO.File.ReadAllBytes
方法即可读取二进制文件:

byte[] imgBytes = System.IO.File.ReadAllBytes(@"c:\temp\capture.png");
因此,在您的情况下,您可以这样使用它(替换路径):

你在这里有榜样


顺便说一句,不要将图像保存在数据库中,而是将文件的路径保存在数据库中,并将图像保存到磁盘。

对于您的问题,我认为您已将图像插入表中,现在希望检索插入的图像。如果是这样的话,你可以试着

                string sql = "";
                string address = "";
                SqlConnection con = new SqlConnection(ConnectionString);
                sql = "SELECT Image from ImageTable where imageId=1";

                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter adp = new SqlDataAdapter();
                adp.SelectCommand = cmd;
                adp.Fill(ds,"Data");


                address = "C:\\Users\\CARDIT\\Desktop\\Imag1.jpeg";
                byte[] bytes = (byte[])ds.Tables["Data"].Rows[i][0];
               MemoryStream ms = new MemoryStream(bytes);


               System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

                    Bitmap bmSave = new Bitmap(returnImage);
                    Bitmap bmTemp = new Bitmap(bmSave);

                    Graphics grSave = Graphics.FromImage(bmTemp);
                    grSave.DrawImage(returnImage, 0, 0, returnImage.Width, returnImage.Height);

                    bmTemp.Save(address); //If You want to save in Specific location

                    pictureBox1.Image = bmSave; //if you want to use Image in Picturebox Control;

你的确切问题是什么??你不能存储你想从数据库中检索图像的图像???你有什么问题?您想从sql中检索图像,还是?'ur','U'?注释上的语法错误:-)坏主意,在数据库中保存UNC路径,并将物理映像保存到某个文件系统中,而不是数据库中。
                string sql = "";
                string address = "";
                SqlConnection con = new SqlConnection(ConnectionString);
                sql = "SELECT Image from ImageTable where imageId=1";

                DataSet ds = new DataSet();
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataAdapter adp = new SqlDataAdapter();
                adp.SelectCommand = cmd;
                adp.Fill(ds,"Data");


                address = "C:\\Users\\CARDIT\\Desktop\\Imag1.jpeg";
                byte[] bytes = (byte[])ds.Tables["Data"].Rows[i][0];
               MemoryStream ms = new MemoryStream(bytes);


               System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);

                    Bitmap bmSave = new Bitmap(returnImage);
                    Bitmap bmTemp = new Bitmap(bmSave);

                    Graphics grSave = Graphics.FromImage(bmTemp);
                    grSave.DrawImage(returnImage, 0, 0, returnImage.Width, returnImage.Height);

                    bmTemp.Save(address); //If You want to save in Specific location

                    pictureBox1.Image = bmSave; //if you want to use Image in Picturebox Control;