首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Eclipse中调用web API 2

如何在Eclipse中调用web API 2
EN

Stack Overflow用户
提问于 2015-07-06 15:18:43
回答 2查看 160关注 0票数 1

我有一个ASP.Net MVC5Web API项目。我不能在安卓中使用asp.net web API2Web服务我的web服务在mvc5下,然后我在Eclipse Juno中创建了移动应用程序,我使用android SDK21,下面是我编辑过的代码

代码语言:javascript
复制
namespace AppServices.Models
{
    public class AdvertisingRepository
    {
        private List<Advertising> Advertising = new List<Advertising>();
        private int _nextId = 1;

        public AdvertisingRepository()
        {

        }

        public List<Advertising> GetAll()
        {
            Advertising.Clear();

            SqlDataReader reader = null;
            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = @"Server=.;Database=AppServices;User ID=sa;Password=123;";

            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.CommandText = "SELECT * FROM tblAdvertising";
            sqlCmd.Connection = myConnection;
            myConnection.Open();
            reader = sqlCmd.ExecuteReader();
            Advertising emp = null;
            while (reader.Read())
            {
                emp = new Advertising();
                emp.Id = Convert.ToInt32(reader.GetValue(0));
                emp.SearchString = reader.GetValue(1).ToString();
                emp.OstanID = Convert.ToInt32(reader.GetValue(2));
                emp.AdsGroupID = Convert.ToInt32(reader.GetValue(3));
                Advertising.Add(emp);
            }
            myConnection.Close();

            return Advertising;
        }

        public Advertising Get(int id)
        {
            Advertising.Clear();

            SqlDataReader reader = null;
            SqlConnection myConnection = new SqlConnection();
            myConnection.ConnectionString = @"Server=.;Database=AppServices;User ID=sa;Password=123;";

            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.CommandText = "SELECT * FROM tblAdvertising WHERE Id=" + id + "";
            sqlCmd.Connection = myConnection;
            myConnection.Open();
            reader = sqlCmd.ExecuteReader();
            Advertising emp = null;
            while (reader.Read())
            {
                emp = new Advertising();
                emp.Id = Convert.ToInt32(reader.GetValue(0));
                emp.SearchString = reader.GetValue(1).ToString();
                emp.OstanID = Convert.ToInt32(reader.GetValue(2));
                emp.AdsGroupID = Convert.ToInt32(reader.GetValue(3));
                Advertising.Add(emp);
            }
            myConnection.Close();

             return Advertising.Find(p => p.Id == id);      
        }

        public Advertising Add(Advertising item)
        {
            SqlConnection myConnection = new SqlConnection();

            myConnection.ConnectionString = @"Server=.;Database=AppServices;User ID=sa;Password=123;";
            SqlCommand sqlCmd = new SqlCommand();
            sqlCmd.CommandType = CommandType.Text;
            sqlCmd.CommandText = "INSERT INTO tblAdvertising (SearchString, OstanID, AdsGroupID) VALUES (@SearchString, @OstanID, @AdsGroupID)";
            sqlCmd.Connection = myConnection;


            sqlCmd.Parameters.AddWithValue("@SearchString", item.SearchString);
            sqlCmd.Parameters.AddWithValue("@OstanID", item.OstanID);
            sqlCmd.Parameters.AddWithValue("@AdsGroupID", item.AdsGroupID);
            myConnection.Open();
            int rowInserted = sqlCmd.ExecuteNonQuery();

            // Get new record id
            sqlCmd.CommandText = "SELECT TOP (1) Id FROM tblAdvertising ORDER BY Id DESC";
            if (sqlCmd.ExecuteScalar() != DBNull.Value)
                _nextId = (int)sqlCmd.ExecuteScalar();
            ////////////////////

            myConnection.Close();

            // old code
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            item.Id = _nextId;
            Advertising.Add(item);
            return item;
        }

        public void Remove(int id)
        {
            Advertising.RemoveAll(p => p.Id == id);
        }

        public bool Update(Advertising item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }
            int index = Advertising.FindIndex(p => p.Id == item.Id);
            if (index == -1)
            {
                return false;
            }
            Advertising.RemoveAt(index);
            Advertising.Add(item);
            return true;
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-07-06 15:56:32

我有一个ASP.Net MVC5Web API项目。我不能在安卓中使用asp.net web API2Web服务我的web服务在mvc5下,然后我在Eclipse Juno中创建了移动应用程序,我使用android SDK21,下面是我编辑过的代码

票数 0
EN

Stack Overflow用户

发布于 2015-10-27 19:38:06

有很多问题。

它从看起来有点粗心的"SELECT * FROM tblAdvertising“开始

这是违反SQL规则的三宗罪。首先,没有名为tblAdvertising的表。有一个叫做dbo.tblAdvertising。因此,如果缺少SQL的真实名称,就会认为您指的是dbo,但这也可能是其他原因。这会影响你的表现。最好总是使用完全限定的名称。第二个问题是使用*作为要选择的项。我在SQL开发方面有很长的经验,根据自己的经验可以看出表被更改的频率有多高。这样做的人可能甚至不知道你的应用程序,所以简单的添加就会导致你的代码崩溃。始终使用完全限定的SQL列名。SQL的最后一个问题是您使用了一个命令,但却插入了文本字符串,即使是那些没有受到SQL注入保护的字符串也是如此。如果有人将其添加为字符串"'',0,0;truncate table tblAdvertising;--“怎么办?你可能会惊讶地发现,在过去,我可以简单地用"'‘“或”1=1 --“的一些变体来登录网站。最好避免不受保护的字符串进入MSSQL或MySQL或任何SQL。此外,您的SQLstring还需要编译,这会进一步影响性能。如果结果不存在,也没有针对错误参数或适当反馈的保护。此外,您真的不想选择整个表。应该有一个前100名,或者你可以实现一些分页。因此,解决方案是使用适当的存储过程,检查参数输入,并提供有限且格式正确的输出。

此外,插入到tblAdvertising (SearchString,OstanID,AdsGroupID)值(@SearchString,@OstanID,@AdsGroupID)也可以得到很大的改进。如果update或insert,SQL中有一个output语句比查询最大值快得多

创建类型dbo.IntTable as table(i int) go

create proc dbo.AdvertisementInsert @SearchString varchar,@OstanID int,@AdsGroupID int,@NewID int =0输出为set nocount on --这对于避免网络上的第二次往返很重要

将@i声明为dbo.IntTable --使用预定义的表变量

--如果@SearchString为null返回50001,则始终首先检查输入的质量--也要避免回滚事务,因为这是代价高昂的。--如果存在,最好先询问这个字符串是否还不存在(select 42 from dbo.tblAdvertising where SearchString = @SearchString)返回50002

INSERT INTO tblAdvertising (SearchString,OstanID,AdsGroupID)输出字符串into @i --直接将新id捕获到表变量值(@SearchString,@OstanID,@AdsGroupID)中

--报告结果select @NewID =i from @i --将表变量转换为单个int go

现在,只需使用参数就可以调用您的proc,它会告诉您返回0 ..在这种情况下,结果代码将告诉您失败的原因,或者告诉您与新标识匹配的某个其他数字

尽情享受

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31239835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档