如何使用表在一行上插入多个图像? (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.