向数据库添加记录包括几张照片

本文关键字:照片 张照片 包括几 数据库 添加 加记录 | 更新日期: 2023-09-27 18:13:26

我有一个属于c#新闻网站的管理面板。每条记录(新闻)必须有一张或多张照片。照片将显示在滑块中。所以我不知道用户会上传多少照片。我有两个表在我的数据库,图片(PicID, NewsID, PicName, PicOrder)和新闻(NewsID, newheader, NewsContent)。我可以在news表中添加新闻标题和新闻内容。但是我不知道如何添加我的照片和我的照片的名字到Pics Table。FileUpload控件是有用的,但我不知道有多少照片将被上传。

string path= ConfigurationManager.ConnectionStrings["GUNABConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(path);
    SqlCommand[] cmd = new SqlCommand[1];
    string query= "Insert into News (NewsHeader, NewsContent) values (@NewsHeader, @NewsContent)";
    cmd[0] = new SqlCommand();
    cmd[0].CommandType = CommandType.Text;
    cmd[0].Parameters.Add("@NewsHeader", SqlDbType.VarChar, 40).Value = TextBox1.Text;
    cmd[0].Parameters.Add("@NewsContent", SqlDbType.VarChar, 8000).Value = TextBox2.Text;
    cmd[0].CommandText = query;
    if (new dab().execute(cmd) == true)
    {
        StatusLabel.Text = "News Added";
    }
    else
    {
    } 

向数据库添加记录包括几张照片

将插入内容更改为如下内容:

Insert into News
(NewsHeader, NewsContent)
OUTPUT inserted.NewsId
values
(@NewsHeader, @NewsContent)

然后使用cmd.ExecuteScalar()来检索输出提供的值,例如

int key = (int)cmd.ExecuteScalar();

您可以使用该键插入您的相关图像。

如果问题涉及到图像的添加,假设你有它作为System.Drawing.BitmapSystem.Drawing.Image在c#中,你应该能够通过添加它作为一个正常参数传递它到一个image数据库字段类型