2016-04-09 15 views
0

我想从我的网站上的SQLSEVER数据库显示图片。我的用户有一个带有图片数据类型的图片字段。如果图片列为空,那么我希望显示的图片是egg.jpg,但现在对于每个人来说,他们的图片都是egg.jpg,即使他们在数据库中有图片。这是我的方法。SQL读取器返回每一行的DBnull

public string getImageUrl() 
     { 
    System.Data.SqlClient.SqlConnection sc = new    System.Data.SqlClient.SqlConnection(); 

    sc.ConnectionString = "Server =MRCOMPUTER2\\SQLEXPRESS; Database = WBL;Trusted_Connection=Yes;"; 
    sc.Open(); 
    System.Data.SqlClient.SqlCommand insert = new System.Data.SqlClient.SqlCommand(); 
    insert.Connection = sc; 
    insert.CommandText = "SELECT profilePicture from SystemUser"; 

    insert.ExecuteNonQuery(); 
    SqlDataReader reader = insert.ExecuteReader(); 
    string url = ""; 
    while (reader.Read()) 
    { 

     if (!DBNull.Value.Equals(reader[0])) 
     { 
      url = "data:Image/png; base64," + Convert.ToBase64String((byte[])reader[0]); 

     } 

     else { 

      url = "images/egg.jpg"; 
     } 

    } 
    return url; 

} 
+1

为什么你选择的命令被称为 '插入'?它是否必须在db中插入一些东西?你不应该在insert.ExecuteReader();'之前调用'insert.ExecuteNonQuery();'。 –

回答

0

你可以尝试使用列这样的名称

var Val = (String)reader["column name"]; 

另外,尝试这样的测试:

while (reader.Read()) 
      { 

       var testVal = reader.GetString(0); 
       Var testVal2 = reader.GetString(1); 
+0

我只是试过,但同样的问题 – hamstrdethwagon

+0

dbnull检查之前读者的价值是什么?在Visual Studio中添加一个快速监视器,以便在调试时进行钻取 – ironman

1

你的代码返回图像的最后用户在您的表中。

这里:

insert.CommandText = "SELECT profilePicture from SystemUser"; 

您从表中选择所有用户(只是你目前所显示的一个没有)。然后:

while (reader.Read()) 
{ 
    ... 
    url = ... 
    ... 
} 

重新分配url迭代while循环。这是语义上等价于:

url = ... /* The value determined from the last record of the reader. */ 

因此,所有用户都显示相同的画面 - 在最后用户在表的一个。

0

您将SELECT语句附加到ExecuteNonQuery中?

把它关掉。

只是执行读写声明...

0

试试这个:

 static void Main(string[] args) 
     { 
      string connectionString = "Server=.;Database=AA;Trusted_Connection=True;"; 

      /* 
      CREATE TABLE [dbo].[SystemUser] 
      (
       [ProfilePicture] [varbinary](max) NULL 
      ) 
      */ 

      using (SqlConnection connection = new SqlConnection(connectionString)) 
      { 
       string sql = @" 
INSERT [AA].[dbo].[SystemUser] ([ProfilePicture]) VALUES (@ProfilePicture); 
INSERT [AA].[dbo].[SystemUser] ([ProfilePicture]) VALUES (NULL); 
"; 

       SqlCommand command = new SqlCommand(); 
       command.Connection = connection; 
       command.CommandText = sql; 
       command.CommandType = CommandType.Text; 

       byte[] bytes = File.ReadAllBytes(@"1.jpg"); 
       command.Parameters.AddWithValue("@ProfilePicture", bytes); 

       connection.Open(); 
       command.ExecuteNonQuery(); 
      } 


      DataSet ds = new DataSet(); 
      using (SqlConnection connection = new SqlConnection(connectionString)) 
      { 
       string sql = @" 
SELECT TOP 1000 [ProfilePicture] FROM [AA].[dbo].[SystemUser]; 
"; 

       SqlCommand command = new SqlCommand(); 
       command.Connection = connection; 
       command.CommandText = sql; 
       command.CommandType = CommandType.Text; 

       connection.Open(); 
       SqlDataAdapter da = new SqlDataAdapter(command); 
       da.Fill(ds); 
      } 

      var rows = ds.Tables[0].Rows.Cast<DataRow>(); 
      foreach (DataRow row in rows) 
      { 
       byte[] bytes = row.Field<byte[]>(0); 
       if (bytes != null) 
       { 
        string fileName = Guid.NewGuid().ToString("N") + ".jpg"; 
        File.WriteAllBytes(fileName, bytes); 
       } 
      } 
     }