陈斌彬的技术博客

Stay foolish,stay hungry

iOS 上传图片保存服务器图片路径到SQL

数据库设计

img

测试结果

img

代码实现

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HeadURLDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.Files.Count == 0)
            {
                Response.Write("none file");
            }
            else
            {
                HttpPostedFile file = Request.Files["img"];
                String filename = Request.Form["name"];
                file.SaveAs(MapPath("~/" + filename + ".png"));
                string url = MapPath("~/" + filename + ".png");
                //连接SQL数据库
                System.Data.SqlClient.SqlConnection SqlCnn = new System.Data.SqlClient.SqlConnection("Data Source=JOHN;Initial Catalog=webservice;User ID=sa;Password=12345678;");
                //打开数据库连接
                SqlCnn.Open();
                string sqlstr = "insert into dbo.head (URLValues) values('" + url + "')";
                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
                cmd.Connection = SqlCnn;
                cmd.CommandType = System.Data.CommandType.Text;
                cmd.CommandText = sqlstr;
                //执行 
                cmd.ExecuteNonQuery();
                //关闭数据库
                SqlCnn.Close();

                Response.Write("ok");
            }

        }
    }
}