【问题标题】:trying to connect web API's to oracle db尝试将 Web API 连接到 oracle db
【发布时间】:2019-12-03 08:46:14
【问题描述】:

正在尝试将 Web API 连接到 oracle DB。

我已成功连接 SQL 服务器,但我不知道 oracle DB。我所做的是=> 创建了一个控制器并在其中编写了一个 get 和 post API 方法。在 web.config 上我添加了 connectionStrings。我正在附上我的 SQL 连接代码。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace API2.Controllers
{
    public class IndexController : ApiController
        {
            [HttpGet]
            [Route("getUsers")]
            public HttpResponseMessage Get_Users()
            {
                DataTable dt = new DataTable();
                string str_Msg = string.Empty;
                string Query = string.Empty;
                string consString = ConfigurationManager.ConnectionStrings["xyz"].ConnectionString;
                Query = "SELECT * from xyz";
                using (SqlConnection con = new SqlConnection(consString))
                {
                    using (SqlCommand _SqlCommand = new SqlCommand(Query, con))
                    {
                        _SqlCommand.CommandType = CommandType.Text;
                        using (SqlDataAdapter _adapater = new SqlDataAdapter(_SqlCommand))
                        {
                            _adapater.Fill(dt);

                        }
                        Query = string.Empty;
                    }
                }
                return Request.CreateResponse(HttpStatusCode.OK, dt, Configuration.Formatters.JsonFormatter);
            }
        }


<connectionStrings>
    <add name="xyz" connectionString="Data Source= xyz; Integrated Security=false;Initial Catalog= xyz; uid=xyz; Password=xyz;" providerName="System.Data.SqlClient" />
  </connectionStrings>

我不知道如何使用 oracle DB 进行相同的连接。请帮我解决这个问题。

非常感谢。

【问题讨论】:

  • 您似乎需要有关连接字符串的帮助,对吗?
  • 是的,我猜。顺便说一句,我是 .net Web API 的新手,
  • 连接字符串应该是相同的,不管是什么语言(.net/java/python),也不管你有网络界面还是只有一个 CLI。

标签: c# asp.net .net oracle asp.net-web-api


【解决方案1】:

参考this

你的连接字符串看起来像

Data Source=MyOracleDB;User Id=myUsername;Password=myPassword;Integrated Security=no;

【讨论】:

  • 这些怎么样 => 使用 System.Data.SqlClient;使用 System.Data;使用 System.Configuration; SqlConnection、SqlCommand 和 SqlDataAdapter 方法?我必须使用相同的吗?
  • 这些不应该不同。
  • 非常感谢您的帮助。我做了一些必要的改变,它正在工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-03-13
  • 2016-11-25
  • 2019-04-05
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多