简体   繁体   English

C#如何插入数据库

[英]c# how to insert into database

I need help on this, I'm inserting file directory into the database but it does not take into account of the txtStoryTitle.Text in the database, for example, if I type HelloWorld in txtStoryTitle. 我需要帮助,例如,我正在将文件目录插入数据库中,但没有考虑数据库中的txtStoryTitle.Text,例如,如果我在txtStoryTitle中键入HelloWorld。 It appears as Images/Story//(filename) instead of Images/Story/HelloWorld/(filename) in the DB. 它在数据库中显示为Images / Story //(文件名),而不是Images / Story / HelloWorld /(文件名)。 I am using MySQL (workbench). 我正在使用MySQL(工作台)。

please give me an advice/solutions on this, thanks in advance! 请给我一个建议/解决方案,在此先感谢!

Here are the partial codes: 以下是部分代码:

    protected void AjaxFileUpload1_UploadComplete(object sender, AjaxControlToolkit.AjaxFileUploadEventArgs e)
    {
        EnsureDirectoriesExist();


        String filepathImage = (@"Images/Story/" + txtStoryTitle.Text + "/" + e.FileName);


        AjaxFileUpload1.SaveAs(Server.MapPath(filepathImage));


        Session["filepathImage"] = filepathImage;

    }

   public void EnsureDirectoriesExist()
    {
        if (!System.IO.Directory.Exists(Server.MapPath(@"Images/Story/" + txtStoryTitle.Text + "/")))
        {
            System.IO.Directory.CreateDirectory(Server.MapPath(@"Images/Story/" + txtStoryTitle.Text + "/"));
        }
    }

 protected void btnDone_Click(object sender, EventArgs e)
    {
        if (Session["filepathImage"] != null)
        {
            string filepathImage = Session["filepathImage"] as string;
            act.ActivityName = dropListActivity.SelectedItem.Text;
            act.Title = txtStoryTitle.Text;

            act.FileURL = filepathImage;

            daoStory.Insert(act);
            daoStory.Save();

        }

As per your code.. the file path is "Images/Story/" + txtStoryTitle.Text + "/" + e.FileName" and after providing txtStoryTitle.Text it saved as "Images/Story//FileName".. then it means txtStoryTitle.Text does'nt contain any text.. 根据您的代码..文件路径为“ Images / Story /” + txtStoryTitle.Text +“ /” + e.FileName”,并在提供txtStoryTitle.Text之后将其保存为“ Images / Story // FileName”。表示txtStoryTitle.Text不包含任何文本。

If its in .Net then make sure you set autopostback property of txtStoryTitle textbox to true. 如果它在.Net中,请确保将txtStoryTitle文本框的autopostback属性设置为true。 and if it is already true then try to find out why this textbox does'nt resist its state. 并且如果它已经是真的,那么尝试找出为什么此文本框不抵抗其状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM