gpt4 book ai didi

c# - 如何保存图像数据库并将其显示在网页的面板上?

转载 作者:行者123 更新时间:2023-11-29 03:22:39 25 4
gpt4 key购买 nike

我在 visual studio 的 webform 上为图像创建了一个文件夹,标题为 (Invoices),我想将图像的根目录保存在 MySql 数据库中并显示网页面板上的图像。图像已保存在文件夹中,但未保存在数据库中。

我试过的代码

protected void Page_Load(object sender, EventArgs e) {

    string connection = "server=localhost; userid=   ; password=   ; database=admindb; allowuservariables=True";

MySqlConnection cn = new MySqlConnection(connection);

try
{

string sqlcmd = "SELECT PicAddress FROM InvoicesFP WHERE InvoicesFP_ID=@InvoicesFP_ID";

cn.Open();

MySqlCommand cmd = new MySqlCommand(sqlcmd, cn);
cmd.CommandType = CommandType.Text;
MySqlDataReader rdr = cmd.ExecuteReader();

while (rdr.Read())
{
string url = rdr["PicAddress"].ToString();
Image1.ImageUrl = url;
}
}
catch (Exception ex)
{
throw ex;
}
cn.Close();
}

protected void Button1_Click(object sender, EventArgs e)
{
#region fileupload
var FileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName).Substring(1);
string SaveLocation = Server.MapPath("Invoices\\") + Guid.NewGuid().ToString("N") + FileExtension;
MySqlConnection connection = new MySqlConnection("server=localhost; userid=root; password=admin1234; database=admindb; allowuservariables=True");
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "insert into invoicesfp (PicAddress), VALUES (@PicAddress)";
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("PicAddress", Server.MapPath("~/Invoices"));
cmd.Connection.Open();
cmd.BeginExecuteNonQuery();
cmd.Connection.Close();

try
{
FileUpload1.PostedFile.SaveAs(SaveLocation);
}
catch (Exception ex)
{
if (ex is ArgumentNullException || ex is NullReferenceException)
{
throw ex;
}
}
string PicAddress = "~/Invoices/" + SaveLocation;
}

#endregion

最佳答案

更新

先试试这个代码,使用这个示例你不需要使用 Rename 方法而且无论它的扩展是什么,它都是支持的:

#region fileupload 
var FileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName).Substring(1);
string SaveLocation = Server.MapPath("Invoices\\") + Guid.NewGuid().ToString("N") + "." + FileExtension;

try
{
FileUpload1.PostedFile.SaveAs(SaveLocation);
}
catch (Exception ex)
{
if (ex is ArgumentNullException || ex is NullReferenceException)
{
throw ex;
}
}
string PicAddress = "~/Invoices/" + SaveLocation;

#endregion

MySqlConnection connection = new MySqlConnection("server=localhost; userid=root; password=admin1234; database=admindb; allowuservariables=True");
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = "INSERT INTO InvoicesFP (PicAddress) VALUES (@PicAddress)";
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("PicAddress", "PicAddress");
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();

然后在您的 page_load 中,您可以调用 Select Query 以获取 Address out 数据库并将其传递给控制,例如:

   const string DB_CONN_STR = "Server=etc;Uid=etc;Pwd=etc;Database=etc;";

MySqlConnection cn = new MySqlConnection(DB_CONN_STR);

try {

string sqlCmd = "SELECT PicAddress FROM [YourTable] where Id == PicAddress id";

cn.Open(); // have to explicitly open connection (fetches from pool)

MySqlCommand cmd = new MySqlCommand(sqlCmd, cn);
cmd.CommandType = CommandType.Text;
MySqlDataReader rdr = cmd.ExecuteReader();

while(rdr.Read())
{
string url = rdr["Name of the Field Assuming PicAddress"].ToString();
Image1.ImageUrl = url;
}
}
catch(Exception ex)
{
throw ex;
}

这就是全部 - 快乐编码

关于c# - 如何保存图像数据库并将其显示在网页的面板上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41320243/

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