如何使用表在一行上插入多个图像? (C#网站) [英] How to insert multiple images on a single row using table? (C# web)

查看:48
本文介绍了如何使用表在一行上插入多个图像? (C#网站)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try
           {
               string filename = Path.GetFileName(img_upload1.PostedFile.FileName);

               img_upload1.SaveAs(Server.MapPath("~/Content/Img/cars/" + filename));

               string cs = ConfigurationManager.ConnectionStrings["cs_ki"].ToString();
               SqlConnection con = new SqlConnection(cs);
               con.Open();

               SqlCommand cmd = new SqlCommand("insert into tbl_car "
                 + "(cars_brand,cars_type,cars_img1) values (@cars_brand,@cars_type,@cars_img1)", con);
               cmd.Parameters.AddWithValue("@cars_img1", "~/Content/Img/cars/" + filename);

               cmd.Parameters.AddWithValue("@cars_brand", txt_brand.Text);
                cmd.Parameters.AddWithValue("@cars_type", txt_type.Text);
      
               //cmd.Parameters.AddWithValue("@cars_img1", txt_brand.Text);

               cmd.Connection = con;
               cmd.ExecuteNonQuery();




               lbl_alert.Visible = true;
               lbl_alert.Text = 
               con.Close();
           }





我的尝试:



如何为一个id插入多个图像?



What I have tried:

how to insert multiple images for one id?

推荐答案

试试这样



try like this

SqlConnection con = new SqlConnection();
           con.Open();
           SqlCommand cmd = new SqlCommand("insert into tbl_car (name) values (@name); select scope_identity()", con);
           cmd.Parameters.AddWithValue("@name", txt_name.Text);
           cmd.Connection = con;
           SqlDataAdapter da = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           da.Fill(dt);
           int id = Convert.ToInt32(dt.Rows[0][0]);

           cmd.ExecuteNonQuery();
           con.Close();

           foreach (var file in uploader.PostedFiles)
           {
               int length = uploader.PostedFile.ContentLength;
               byte[] pic = new byte[length];
               uploader.PostedFile.InputStream.Read(pic, 0, length);

                   string filename = Path.GetFileName(uploader.PostedFile.FileName);
                   uploader.SaveAs(Server.MapPath("~/Insert/Img/" + filename));
                   string cs = ConfigurationManager.ConnectionStrings["theoneConnectionString"].ToString();
                   SqlConnection con = new SqlConnection(cs);
                   con.Open();
                   SqlCommand cmd1 = new SqlCommand("insert into tbl_img (imgpath,imgid) values (@imgpath,@id)", con);
                   cmd1.Parameters.AddWithValue("@imgpath", "~/Insert/Img/" + filename);
                   cmd1.Parameters.AddWithValue("@id", id);
                   cmd1.Connection = con;
                   cmd1.ExecuteNonQuery();

                   con.Close();

           }


将多个图像路径插入到btl_car的单行中是个坏主意。每辆车的图像数量永远不会相同。所以你最好创建另一个表来包含图像文件路径,该路径应该将carid作为外键。
It is a bad idea to have multiple images paths are inserted in to a single row on the btl_car. The number of images for each car will never be same. So you better create another table to contain the image file path, which should have the carid as foreign key on it.


这篇关于如何使用表在一行上插入多个图像? (C#网站)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆