一.安装VS2019,二.写一个C#的ASP.NET MVC简单的接口

首先安装Visual Studio 2019,

链接:https://pan.baidu.com/s/1NF9o3cKbqvI6O6VwV2sGJw 
提取码:5540

 

 

 

 

 

 

 

 

 

 

 到这里就安装完成了,那么我给一个简单的asp.net mvc 接口,让他跑起来

创建项目(注意不要选择错误)

随便搞一个名字 

 

 

 加入数据库连接池,已经写好的,代码我提供,使用只需要改数据库链接信息,改成自己的

我这里给了一个控制器文件,同样你需要改成自己的项目名字

 

把ttController的代码复制进去

然后运行让浏览器显示出网址 ,然后用Apipost查接口    网址+tt控制器名+test方法名查出接口里面的json数据

 上面用到相关代码文件一  sqlHelper

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace oee_Api    //这个每次都要改成你当前项目的命名空间名字   namespace ok
{
    public class SqlHelper
    {
        //<add key="SqlContext" value="Server=101.33.248.192;Database=MEKDW;Uid=test;Pwd=test01!;"/>这段复制到app.config中,将数据库改为自己的数据库
      //  string connectionString = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None).AppSettings.Settings["SqlContext"].Value;
        public string connectionString = ConfigurationManager.AppSettings["SqlContext"];
        public SqlConnection Connection
        {
            get;
            set;
        }
        

        public SqlHelper()
        {
            var ConnectionStr = connectionString;
            Connection = new SqlConnection(connectionString);
        }

        public object ExecuteScalar(string cmdString)
        {
            try
            {
                Connection.Open();
                SqlCommand cmd = new SqlCommand(cmdString, Connection);
                return cmd.ExecuteScalar();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (Connection.State != System.Data.ConnectionState.Closed)
                    Connection.Close();
            }
        }

        public int ExecuteNonQuery(string cmdString)
        {
            try
            {
                //var list = cmdString.Split(',').ToList();
                //for (int i = 0; i < list.Count; i++)
                //{
                //    bool isString = false;
                //    if (list[i].StartsWith("'") && list[i].EndsWith("'"))
                //    {
                //        isString = true;
                //        list[i] = list[i].Substring(1, list[i].Length - 2);
                //    }

                //    if (list[i].Contains("'"))
                //        list[i] = list[i].Replace("'", "''");

                //    if (isString)
                //        list[i] = "'" + list[i] + "'";
                //}

                //StringBuilder sb = new StringBuilder();
                //foreach (var s in list)
                //    sb.Append(s + ",");

                Connection.Open();
                var cmd = new SqlCommand(cmdString, Connection);

                return cmd.ExecuteNonQuery();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (Connection.State != System.Data.ConnectionState.Closed)
                    Connection.Close();
            }
        }

        public DataTable GetData(string cmdString)
        {
            try
            {
                SqlDataAdapter da = new SqlDataAdapter(cmdString, Connection);
                DataSet ds = new DataSet();
                da.Fill(ds);
                if (ds.Tables.Count > 0)
                    return ds.Tables[0];
                else
                    throw new Exception("没有找到表");
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                if (Connection.State != ConnectionState.Closed)
                    Connection.Close();
            }
        }

        public bool ConnectTest()
        {
            try
            {
                var o = ExecuteScalar("select GetDate()");
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}

 文件二ttController

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace doorApi.Controllers
{
    public class ttController : Controller
    {
        // GET: tt
        public ActionResult Index()
        {
            return View();
        }
        [HttpPost]  //post请求

        public JsonResult test(string aa)  // 接收参数
        {
            // 列表
            List<T> list = new List<T>();
            SqlHelper sqlHelper = new SqlHelper();
            string cmd = "select DeviceId,RouteId from [Device]"; // sql语句
            var th = sqlHelper.GetData(cmd);    // GetData 查找    增删改用 ExecuteScalar

            for (int i = 0; i < th.Rows.Count; i++) // 循环表把内容添加到list
            {
                T t = new T();  
                t.RouteId = int.Parse(th.Rows[i][1].ToString());   // 第 i 行的第 1 列
                t.DeviceId = int.Parse(th.Rows[i][0].ToString());
                list.Add(t); 
            }
            return Json(new
            {
                info = list,
                result = aa

            }, JsonRequestBehavior.AllowGet);
        }
    }
    class T  //类
    {
        public int RouteId { get; set; }
        public int DeviceId { get; set; }
    }
}

相关的数据库数据表

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值