gpt4 book ai didi

c# - 使用数据类型为longblob的mysql将图像插入数据库

转载 作者:行者123 更新时间:2023-11-30 01:23:54 24 4
gpt4 key购买 nike

我计划将图像插入数据库。但是插入时出现一些问题,即(图像格式:输入字符串的格式不正确)。请帮我 。提前致谢。错误出现在这一行 -> 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");
}
}

最佳答案

使用名为 Player 的类以及 id、名称和照片值。

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;
}

关于c# - 使用数据类型为longblob的mysql将图像插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18225781/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com