2014-04-26 71 views
0

我想从Windows应用程序插入图像到mysql数据库,它已被成功执行但在我的表中没有数据。插入图像到mysql数据库时发生的问题

这里我使用了下面的代码。执行成功,根据我的表电子邮件ID和图像必须存储在表中,但这两个字段保存为空。

public void LoadImages() 
     { 
      MySqlConnection cn = new MySqlConnection(connstring); 
      cn.Open(); 
      string image = txtLogo.Text; 
      byte[] ImageData; 
      FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read); 
      BinaryReader br = new BinaryReader(fs); 

      ImageData = BitConverter.GetBytes(fs.Length); 
      br.Close(); 
      fs.Close(); 
      MySqlParameter parImage = new MySqlParameter(); 
      parImage.ParameterName = "?Images"; 
      MySqlCommand cmd = new MySqlCommand("insert into Fn_Pictures(Images,Email)values(?Images,'" + txtEmailIdText + "')", cn); 
      parImage.MySqlDbType = MySqlDbType.MediumBlob; 
      parImage.Size = 3000000; 
      parImage.Value = ImageData;//here you should put your byte [] 

      cmd.Parameters.Add(parImage); 
      cmd.ExecuteNonQuery(); 
      cn.Close(); 

     } 
+0

尝试增加大小 –

+0

是维涅什库马尔我已经调试代码和txtEmailIdText .Text输入为空 – user3531533

回答

0
BitConverter.GetBytes(fs.Length); 

这将返回长度为一个字节[4],而不是读出的图像数据。 看看这里读文件: http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes(v=vs.110).aspx

你可能也想parImage.Size设置为ImageData.length,而不是300万。

+0

我已经尝试过你的建议,但没有用...... @ Hunter Guy – user3531533

+0

@ user3531533放一个断点并检查你是否在'txtEmailIdText.Text'和'ImageData'中获得值或不是# –

+0

是Vignesh库马尔我有调试的代码和txtEmailIdText.Text输入为空 – user3531533