从数据库中读取图像并通过Web服务发送

时间:2013-11-17 08:07:22

标签: sql-server database c#-4.0

我的数据库中有一个包含系统用户数据的表,其中一个数据是个人资料图片, 我正在尝试阅读这些数据并使用网络服务将其发送到智能手机,

我尝试以下代码,但它不起作用

        public User select(string username, string password)
    {

        SqlCommand myCommand = new SqlCommand("select * from users where userName = @uname and password = @pwd or email = @uname and password = @pwd");


        myCommand.Parameters.AddWithValue("@uname", username);
        myCommand.Parameters.AddWithValue("@pwd", password);

        DBAccess db = new DBAccess();
        DataSet ds = db.select(myCommand);

        User user = new User();
        DataRow dr;

        try
        {                
            dr = ds.Tables[0].Rows[0];

            user.Id = Convert.ToInt32(dr["userID"]);                
            user.FirstName = (string)dr["firstName"]+" ";
            user.LastName = (string)dr["lastName"] + " ";
            user.Email = (string)dr["email"] + " ";
            user.Username = (string)dr["userName"];
            user.Password = (string)dr["password"];
            user.type = (string)dr["type"];
            user.followingID = Convert.ToInt32(dr["followingID"]);
            user.theme = Convert.ToInt32(dr["theme"]);
            user.tel = (string)dr["tel"] + " ";
            user.mobile = (string)dr["mobile"] + " ";
            user.fax = (string)dr["fax"] + " ";
            user.lastLoggedIn = Convert.ToDateTime(dr["lastLoggedIn"]);
            user.image = Convert.ToByte[](dr["Image"]);

            return user;
        }
        catch (Exception ex)
        {
            DAO.exDao myEx = new DAO.exDao();

            return null;
        }
    }//end of select method

我不知道如何从数据库中读取图像并将其保存为字节数组以便发送抛出Web服务,因此下一行的错误

user.image = Convert.ToByte[](dr["Image"]);

1 个答案:

答案 0 :(得分:0)

您可以将图像的路径存储在数据库中。然后在检索之后你可以显示图像。