<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經轉移至github,大家請訪問 http://qaseven.github.io/

    如何將上傳圖片直接保存至SQL Server數據庫中

      這里演示的是如何將上傳圖片直接保存至SQL Server數據庫中。

      在數據庫中,圖片使用varbinary(MAX)存儲:

      這是個基于MVC3架構的例子。廢話不多說,直接上代碼:

      View:

    @{
        ViewBag.Title = "UpLoadImg";
    }
    @using (Html.BeginForm("Create", "UpLoadImg", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <h2>
            UpLoadImg</h2>
        <div id="mainform">
            <div>
                <input type="file" id="UpLoadFile" name="UpLoadFile" />
                <input id="btnUpLoad" type="submit" value="上傳" />
            </div>
            <div>
            </div>
        </div>
    }

      Controller后臺Action:

    public ActionResult Create()
            {
                string filename = string.Empty;
                string filetype=string.Empty;
                byte[]  filecontext=null;
                HttpPostedFileBase filebase = Request.Files["UpLoadFile"];
                if (filebase.ContentLength > 0)
                {
                    Stream stream = filebase.InputStream;
                    byte[] by = new byte[filebase.ContentLength];
                    int i = stream.Read(by,0,filebase.ContentLength);
                    stream.Close();
                    string[] arrs = filebase.FileName.Split('\\');
                    if (arrs.Length > 0)
                    {
                        filename = arrs[arrs.Length - 1];
                    }
                    else
                    {
                        filename = filebase.FileName;
                    }
                    filetype=filebase.ContentType;
                    filecontext=by;
                }//淡雅一抹繁華,幾多思念許他,他不知花開不易,他不懂人心需要珍惜。
                int count = 0;
                #region 插入數據
                try
                {
                    string ImageStore = System.Configuration.ConfigurationManager.AppSettings["ConnectionStrImageStore"].ToString().Trim();
                    string sqlStr = string.Empty;
                    sqlStr = @"INSERT INTO [Images] ([filename],[filetype],[filecontext],[uploadtime])
                        VALUES(@filename,@filetype,@filecontext,@uploadtime)";
                    SqlConnection connection = new SqlConnection(ImageStore);
                    SqlCommand command = new SqlCommand(sqlStr, connection);
                    command.Parameters.AddWithValue("@filename",filename);
                    command.Parameters.AddWithValue("@filetype",filetype);
                    command.Parameters.AddWithValue("@filecontext",filecontext);
                    command.Parameters.AddWithValue("@uploadtime",DateTime.Now);
                    command.Connection.Open();
                    count=command.ExecuteNonQuery();
                    command.Connection.Close();
                }
                catch
                {
                   
                }
                #endregion

                if (count > 0)
                {
                    return RedirectToAction("UpLoadImg");
                }
                else
                {
                    return RedirectToAction("Index");
                }
            }

    posted on 2013-09-13 10:55 順其自然EVO 閱讀(893) 評論(0)  編輯  收藏 所屬分類: 數據庫

    <2013年9月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導航

    統計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 在线精品一卡乱码免费| 全黄大全大色全免费大片| 最新欧洲大片免费在线| 亚洲最大的视频网站| 国产精品怡红院永久免费| 亚洲欧洲日本天天堂在线观看| 91短视频在线免费观看| 亚洲一级在线观看| 在线免费视频一区| 亚洲av无码兔费综合| 亚洲 无码 在线 专区| yellow免费网站| 亚洲精品免费视频| 久九九精品免费视频| 亚洲精品第一国产综合亚AV| 国产性生交xxxxx免费| 又大又硬又粗又黄的视频免费看 | 91成人免费观看在线观看| 久久精品国产精品亚洲艾草网| 久久久久久AV无码免费网站| 亚洲色成人网一二三区| 日本牲交大片免费观看| 国产99精品一区二区三区免费| 国产精品亚洲а∨无码播放| 91麻豆最新在线人成免费观看| 欧洲亚洲综合一区二区三区 | 亚洲欧美日韩中文高清www777| 免费大片在线观看网站| 国产在线精品免费aaa片| 亚洲视频在线观看2018| 国产成人无码a区在线观看视频免费 | 免费无码一区二区三区蜜桃大| 日韩免费码中文在线观看| 久久久久久久亚洲Av无码| 成年女人毛片免费视频| 精品免费久久久久国产一区 | 久久精品国产亚洲AV麻豆网站| 日本成人在线免费观看| 成人性生交大片免费看好| 亚洲日韩国产精品乱-久| 最新精品亚洲成a人在线观看|