将图像上传到数据库 [英] Upload Image into database
本文介绍了将图像上传到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好我想将图片上传到数据库
请帮帮我
我创建方法
public void insert_blog( ref string connection_string, ref Int64 blog_id, ref string title, ref string intro, ref string text, ref < span class =code-keyword> string keyword, ref byte categotry, out byte affected_rows)
{
affected_rows = 0 ;
SqlConnection conn = new SqlConnection(connection_string);
SqlCommand cmd = new SqlCommand();
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.CommandText = insert_blog;
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add( @ blogid,SqlDbType.BigInt ).Value = blog_id;
cmd.Parameters.Add( @ intro,SqlDbType.VarChar).Value =介绍;
cmd.Parameters.Add( @ title,SqlDbType.VarChar).Value =标题;
cmd.Parameters.Add( @ text,SqlDbType.NVarChar).Value =文本;
cmd.Parameters.Add( @ keyword,SqlDbType.VarChar).Value =关键词;
cmd.Parameters.Add( @ category,SqlDbType.TinyInt).Value = categotry;
affected_rows = Convert.ToByte(cmd.ExecuteNonQuery());
conn.Close();
conn.Dispose();
}
和
string connection = System.Configuration.ConfigurationManager.ConnectionStrings [ 博客]。ToString();
blog_class cla = new blog_class();
string title = TextBox1_title.Text;
string intro = TextBox1_intro.Text;
string meta = TextBox1_meta_tags.Text;
string \\ temp = ;
Int64 blog_id = 0 ;
order_by_tag_photo_ids_wall( ref connection, out blog_id);
blog_id = blog_id + 1 ;
byte cat_id = Convert.ToByte(dropdown1.SelectedValue.ToString());
byte n;
cla.insert_blog( ref connection, ref blog_id, ref title, ref intro, ref \\ temp, ref meta, ref cat_id, out n);
if (n > 0 )
{
Response.Redirect( 〜/ user / redirect / edit_blog.aspx ?blogid = + blog_id);
}
else
{
Label1.Text = 错误发布;
}
我如何上传我创建的图片数据库中的字段
img varchar(300)
解决方案
您好,
请查看这些Code Project文章。他们应该帮助您解决问题。使用varchar(300)
不是一个好主意,除非你的图像非常小&该字段定义不允许您以二进制格式存储图像。
- 使用Strored Procedures和C#从SQL Server存储和检索图像 [ ^ ]
- C#从数据库保存和加载图像 [ ^ ]
- SQL Database Image Storage&简易缩略图 [ ^ ]
- 保存图像进入SQL Server 2000数据库 [ ^ ]
- 使用Microsoft .NET从SQL Server存储和检索图像 [ ^ ]
问候,
< blockquote>在sql中使用image作为数据类型
protected void Button1_Click(object sender,EventArgs e)
{
if(FileUpload1.PostedFile!= null&& FileUpload 1.PostedFile.FileName!=)
{
byte [] img = new byte [FileUpload1.PostedFile.ContentLength];
HttpPostedFile Image = FileUpload1.PostedFile;
Image.InputStream.Read(img,0,(int)FileUpload1.PostedFile.ContentLength);
string str =insert img values(@img);
SqlConnection cn = new SqlConnection(data source = localhost; initial catalog = aadarsh; user id = sa; password = cos123);
cn.Open();
SqlCommand cmd = new SqlCommand(str,cn);
cmd.Parameters.AddWithValue(@ img,img);
SqlDataReader dr = cmd.ExecuteReader();
cn.Close();
}
}
SqlConnection con = new SqlConnection(@Data Source = LUVLY; Initial Catalog = practice;用户ID = sa;密码= lenovo @ 345);
con.Open();
string cmdstr =select * from testing;
SqlCommand cmd = new SqlCommand(cmdstr,con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Hi I want to upload a image into database
Please help me out
I create a method
public void insert_blog(ref string connection_string, ref Int64 blog_id, ref string title, ref string intro, ref string text, ref string keyword, ref byte categotry, out byte affected_rows)
{
affected_rows = 0;
SqlConnection conn = new SqlConnection(connection_string);
SqlCommand cmd = new SqlCommand();
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.CommandText = "insert_blog";
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@blogid", SqlDbType.BigInt).Value = blog_id;
cmd.Parameters.Add("@intro", SqlDbType.VarChar).Value = intro;
cmd.Parameters.Add("@title", SqlDbType.VarChar).Value = title;
cmd.Parameters.Add("@text", SqlDbType.NVarChar).Value = text;
cmd.Parameters.Add("@keyword", SqlDbType.VarChar).Value = keyword;
cmd.Parameters.Add("@category", SqlDbType.TinyInt).Value = categotry;
affected_rows = Convert.ToByte(cmd.ExecuteNonQuery());
conn.Close();
conn.Dispose();
}
and
string connection = System.Configuration.ConfigurationManager.ConnectionStrings["blog"].ToString();
blog_class cla = new blog_class();
string title = TextBox1_title.Text;
string intro = TextBox1_intro.Text;
string meta = TextBox1_meta_tags.Text;
string emp = "";
Int64 blog_id = 0;
order_by_tag_photo_ids_wall(ref connection, out blog_id);
blog_id = blog_id + 1;
byte cat_id = Convert.ToByte(dropdown1.SelectedValue.ToString());
byte n;
cla.insert_blog(ref connection, ref blog_id, ref title, ref intro, ref emp, ref meta, ref cat_id, out n);
if (n > 0)
{
Response.Redirect("~/user/redirect/edit_blog.aspx?blogid=" + blog_id);
}
else
{
Label1.Text = "Error Occured";
}
How I upload Image I create a field in database
img varchar(300)
解决方案
Hello,
Please have a look at these Code Project articles. They should help you resolve the issue. Usingvarchar(300)
is not a good idea unless your images are pretty small & that field definition will not allow you to store images in binary format.
- Storing and Retrieving Images from SQL Server Using Strored Procedures and C#[^]
- C# Save and Load Image from Database[^]
- SQL Database Image Storage & Easy Thumbnails[^]
- Save An Image Into SQL Server 2000 Database[^]
- Storing and Retrieving Images from SQL Server using Microsoft .NET[^]
Regards,
use image as a data type in sql
protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") { byte[] img = new byte[FileUpload1.PostedFile.ContentLength]; HttpPostedFile Image = FileUpload1.PostedFile; Image.InputStream.Read(img, 0, (int)FileUpload1.PostedFile.ContentLength); string str = "insert img values(@img)"; SqlConnection cn = new SqlConnection("data source=localhost;initial catalog=aadarsh;user id=sa;password=cos123"); cn.Open(); SqlCommand cmd = new SqlCommand(str, cn); cmd.Parameters.AddWithValue("@img", img); SqlDataReader dr = cmd.ExecuteReader(); cn.Close(); } }
SqlConnection con = new SqlConnection(@"Data Source=LUVLY;Initial Catalog=practice;User ID=sa;Password=lenovo@345");
con.Open();
string cmdstr = "select * from testing";
SqlCommand cmd = new SqlCommand(cmdstr, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
这篇关于将图像上传到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文