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

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

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

    qileilove

    blog已經(jīng)轉(zhuǎn)移至github,大家請(qǐng)?jiān)L問 http://qaseven.github.io/

    如何將上傳圖片直接保存至SQL Server數(shù)據(jù)庫(kù)中

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

      在數(shù)據(jù)庫(kù)中,圖片使用varbinary(MAX)存儲(chǔ):

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

      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后臺(tái)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 插入數(shù)據(jù)
                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 閱讀(897) 評(píng)論(0)  編輯  收藏 所屬分類: 數(shù)據(jù)庫(kù)

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

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 亚洲色成人WWW永久在线观看| 国产在线98福利播放视频免费| 中文字幕成人免费高清在线视频| 亚洲AV无码专区在线电影成人| 黄色a级免费网站| 日本19禁啪啪无遮挡免费动图| 日本一道一区二区免费看 | 国产精品亚洲综合一区在线观看 | 亚洲国产欧洲综合997久久| 99久久久精品免费观看国产| 亚洲成a人片在线观看播放| 日韩精品免费视频| 亚洲人成影院在线| 午夜国产精品免费观看| 中文字幕 亚洲 有码 在线| 免费看大美女大黄大色| 日本免费精品一区二区三区| 在线观看亚洲精品福利片| 大地资源网高清在线观看免费| 久久不见久久见免费影院| 亚洲乱码中文字幕在线| 亚洲精品和日本精品| 91视频精品全国免费观看| 精品亚洲成a人片在线观看| 永久免费视频网站在线观看| 亚洲国产日韩视频观看| 亚洲性日韩精品一区二区三区| 亚洲美女色在线欧洲美女| 女人与禽交视频免费看| 一级毛片免费播放试看60分钟| 国产免费毛不卡片| 老司机午夜性生免费福利| 亚洲乳大丰满中文字幕| 足恋玩丝袜脚视频免费网站| 爱情岛亚洲论坛在线观看| 亚洲αv久久久噜噜噜噜噜| 性感美女视频免费网站午夜| 日本中文字幕免费看| 亚洲日本香蕉视频| 亚洲精品无码久久久| 亚洲精品免费在线|