为什么不显示来自SQL的图像? [英] Why not showing images from SQL?

查看:67
本文介绍了为什么不显示来自SQL的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个C#程序,它将图像存储在"H:\ oucu \ Student Photos"中,并且应该存储到SQL数据库中的八个表之一中.该文件保存得很好,但是我尝试使用Web应用程序读取图像,该行中的所有信息都会出现,除了图像.

I have a C# program that stores images in "H:\oucu\Student Photos", and supposedly to an SQL database, in one of eight tables. the file saves well, but I try to use a web application to read the images, every information in the row appears except the images.

public bool SaveDIBAs( string picname, IntPtr bminfo, IntPtr pixdat )
	{
		string  Partion=  @"h:\oucu\Student_Images\";
		string StName=St_num;
		string FullPath=Partion+StName;
		
 
		Guid clsid;
		if( ! GetCodecClsid( FullPath.ToString(), out clsid ) )
		{
			MessageBox.Show( "Unknown picture format for extension " + Path.GetExtension( FullPath.ToString() ),
				"Image Codec", MessageBoxButtons.OK, MessageBoxIcon.Information );
			return false;
		}
		
		IntPtr img = IntPtr.Zero;
		int st = GdipCreateBitmapFromGdiDib( bminfo, pixdat, ref img );
		if( (st != 0) || (img == IntPtr.Zero) )
			return false;
		
		textBox2.Text = FullPath.ToString() ;
		
		st = GdipSaveImageToFile( img,FullPath.ToString(), ref clsid, IntPtr.Zero );
		GdipDisposeImage( img );
		return st == 0;
		
	}
 
	public void ShowFrm()
	{
		this.Show();
	}

private void button1_Click(object sender, System.EventArgs e)
	{
        //St_num=stNTxt.Text ;
        St_num = StudentPhoto.photo.fileName;
		SaveDIBAs( this.Text, bmpptr, pixptr );
		byte[] byte_Img;
        
		//SqlConnection mySqlConn=new SqlConnection("Network Library=DBMSSOCN;"+"Data Source=82.201.143.224,2022;"+"Initial Catalog=oucu_card;"+"User ID=ousa;"+"Password=cO45_iu89");
        SqlConnection mySqlConn = new SqlConnection("server=192.168.1.25;workstation id=localhost;packet size=4096; persist security info=False;user id=araslan;Password=hR@oucu1;Initial catalog=mandoobeen;min pool size=1;max pool size=10000000;connection lifetime=6000");
		
		
		SqlCommand sqlcmd = new SqlCommand();
		sqlcmd.Connection=mySqlConn;
		sqlcmd.CommandText="insert into " + StudentPhoto.photo.tableName + " (nationalNumber,pictureName,Picture) values(@nN,@pN,@pic)";
		
		sqlcmd.Parameters.Add("@nN",System.Data.SqlDbType.NVarChar);				
		sqlcmd.Parameters.Add("@pic",System.Data.SqlDbType.Image);
		sqlcmd.Parameters.Add("@pN",System.Data.SqlDbType.NVarChar);
		
 
		FileStream fs=new FileStream(this.textBox2.Text.ToString(),FileMode.Open,FileAccess.Read,FileShare.Read);
		byte_Img=new byte[Convert.ToInt32(fs.Length)];
		int iBytesRead=fs.Read(byte_Img,0,Convert.ToInt32(fs.Length));
		fs.Close();
 
        sqlcmd.Parameters["@nN"].Value = StudentPhoto.photo.nationalNumber;
		sqlcmd.Parameters["@pic"].Value=byte_Img;
        TwainGui.Login l = new TwainGui.Login();
        sqlcmd.Parameters["@pN"].Value = StudentPhoto.photo.fileName;
 
		mySqlConn.Open();
		int iresult=sqlcmd.ExecuteNonQuery();
		mySqlConn.Close();
 
				
		System.Diagnostics.Process process1;
		process1= new System.Diagnostics.Process();
		process1.EnableRaisingEvents = false;
		Directory.SetCurrentDirectory(textBox3.Text);
        Application.Exit();
    }

推荐答案

首先,它要求不要放置太多代码,这将使我们很难解决您的问题.
其次,将图像存储在数据库中并不是一个好主意,即使很小的图像也会占用大量字节,这会影响性能.但是,可以选择将图像的相对路径存储到数据库中.
例如 c:\\ images \\ xyz.jpg

在找到解决问题的方法时,您可以尝试以下链接:

http://www.aspsnippets.com/Articles/SQL Server的显示图像-使用ASP.Net.aspx的数据库 [
firstly its a request not to put this much code, it will be hard for us to solve your problem.
Secondaly storing images in database is not an good idea, even small image occupies large amount of bytes which effects the performance. However, alternatively to do this store the relative path of image into your database.
for eg c:\\images\\xyz.jpg

While to find the solution to your problem you can try following link:

http://www.aspsnippets.com/Articles/Display-Images-from-SQL-Server-Database-using-ASP.Net.aspx[^]

Hope you''ll get solution of your answer.
thanks.


这篇关于为什么不显示来自SQL的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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