0

我打算在数据库中插入图像。但是插入 ie 时出现了一些问题(图像格式:输入字符串的格式不正确)。请帮我 。提前致谢。此行出现错误-> int count = cmd.ExecuteNonQuery();

我用 img(列名)Blob(数据类型)创建了数据库。

public partial class check1 : System.Web.UI.Page
{
MySqlConnection con = new MySqlConnection("server=localhost; database=esample; uid=root;");
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGridData();
    }
}
protected void btnupload_Click(object sender, EventArgs e)
{
    if (fileupload.HasFile)
    {
        int length = fileupload.PostedFile.ContentLength;
        byte[] imgbyte = new byte[length];
        HttpPostedFile img = fileupload.PostedFile;
        img.InputStream.Read(imgbyte, 0, length);
        string imagename = imgname.Text;
        con.Open();
        MySqlCommand cmd = new MySqlCommand("INSERT INTO brand (imgname,img) VALUES (@imagename,@imagedata)", con);
        cmd.Parameters.Add("@imagename", SqlDbType.VarChar).Value = imagename;
        cmd.Parameters.Add("@imagedata", SqlDbType.Blob).Value = imgbyte;
        int count = cmd.ExecuteNonQuery();
        con.Close();
        if(count==1)
        {
            BindGridData();
            imgname.Text = string.Empty;
            ScriptManager.RegisterStartupScript(this, this.GetType(), "alertmessage", "javascript:alert('" + imagename + " image inserted successfully')", true);

        }

       }
}
private void BindGridData()
{
    MySqlConnection con = new MySqlConnection("server=localhost; database=esample; uid=root;");
    MySqlCommand command = new MySqlCommand("SELECT imgname,img,bid from brand", con);
    MySqlDataAdapter daimages = new MySqlDataAdapter(command);
    DataTable dt = new DataTable();
    daimages.Fill(dt);
    gvImages.DataSource = dt;
    gvImages.DataBind();
    gvImages.Attributes.Add("bordercolor", "black");
}
}
4

1 回答 1

0

使用带有 id、name 和 photo 值的 Player 类。

public static bool createUser(Player player)
{
   MySqlCommand cmd = new MySqlCommand("INSERT INTO tbuser (id,sName,lbFoto) VALUES (@id,@name,@foto)", dbConnection);
   cmd.Parameters.AddWithValue("@id", player.id).DbType = DbType.Int32;
   cmd.Parameters.AddWithValue("@name", player.Name).DbType = DbType.String;
   cmd.Parameters.AddWithValue("@foto", player.Image).DbType = DbType.Binary;

   try
   {
       if (dbConnection.State == ConnectionState.Closed)
            dbConnection.Open();
            cmd.ExecuteNonQuery();

            dbConnection.Close();
            return true;
        }            
    }
    catch { }
    finaly
    { 
       if (dbConnection.State == ConnectionState.Open)
            dbConnection.Close();
    }
    return false;
 }

然后检索数据:

public static Player loadUser(string ID)
    {
        var table = Select(dbConnection, "tbuser", "id = " + ID);
        Player player = new Player();
        foreach (DataRow row in table.Rows)
        {
            player.id = (int)row[0];
            player.Name = row[1].ToString();
            player.Image = (byte[])row[2];

            return player;
        }
        return null;
    }

功能选择。这是一个额外的;)

public static DataTable Select(MySqlConnection con, string tableName, string expressionWhere)
    {
        string text = string.Format("SELECT * FROM {0} WHERE {1};", tableName, expressionWhere);
        MySqlDataAdapter cmd = new MySqlDataAdapter(text, con);
        DataTable table = new DataTable();

        if (con.State == ConnectionState.Closed)
            con.Open();

        try
        {
            cmd.Fill(table);
        }
        catch (Exception){}
        finally
        {
            if (con.State == ConnectionState.Open)
                con.Close();
        }

        return table;
    }
于 2014-11-14T21:00:52.057 回答