3层编码 [英] 3 tier coding

查看:104
本文介绍了3层编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;
namespace adhousetier
{
    public abstract class clslndcon
  {
        protected SqlConnection con = new SqlConnection();
        public clslndcon()
        {
            con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString;
        }
}



    public interface landinfo
    {
        Int32 property_id { get; set; }
        string priceland { get; set; }
        string sqrft { get; set; }
        string pimage { get; set; }
        string propertytype { get; set; }
        string location { get; set; }
    }


    public class clslandprp : landinfo
    {
        Int32 Pid;
        string prc,sqr,img,prptyp,locatn ;
        #region landinfo Members

        public int  property_id
       {
	  get { return Pid; }
	  set { Pid = value; }
       }

       public string  priceland
       {
	  get { return prc; }
	  set { prc = value; }
       }

       public string  sqrft
       {
	  get {return sqr;}
	  set {sqr = value; }
       }

       public string  pimage
       {
	  get { return img; }
	  set { img = value;}
       }

       public string  propertytype
       {
	  get { return prptyp; 	}
	  set { prptyp=value; 	}
       }
      
      public string  location
      {
	  get { return locatn; }
	  set { locatn=value; }
      }
      #endregion
    }



    public class clslndfxn : clslndcon
    {
        public void  addland(clslandprp r)
        {
            SqlCommand cmd=new SqlCommand("insland",con);
            cmd.Parameters.Add("@priceland",SqlDbType.VarChar,50).Value=r.priceland;
            cmd.Parameters.Add("@sqrft",SqlDbType.VarChar,50).Value=r.sqrft;
            cmd.Parameters.Add("@Pimage",SqlDbType.VarChar,50).Value=r.propertytype;
            cmd.Parameters.Add("@location",SqlDbType.VarChar,50).Value=r.location;
            if(con.State==ConnectionState.Closed)
            {
                con.Open();
            }
            cmd.ExecuteNonQuery();
            cmd.Dispose();
            con.Close();
        }         
    }     
}


它给我错误l ====
错误1无法将类型字符串"隐式转换为"int"
错误2无法将类型字符串"隐式转换为"int"
错误3无法将类型字符串"隐式转换为"int"
错误4无法将类型字符串"隐式转换为"int"

如何清除此错误?
请帮助我,我是一个初学者,只是学习而已.


Its giving me error l====
Error 1 Cannot implicitly convert type ''string'' to ''int''
Error 2 Cannot implicitly convert type ''string'' to ''int''
Error 3 Cannot implicitly convert type ''string'' to ''int''
Error 4 Cannot implicitly convert type ''string'' to ''int''

How to remove this error?
Please help me out some body, I am a beginner, just learning.

推荐答案

问题是存储过程中的参数:
The problem is that the parameters on the the stored procedure:
@priceland


@sqrft


@Pimage


@location



在SQL端都需要整数,并且正在传递无法转换为整数的代码中的字符串.检查存储过程 insland ,您会如果我是正确的,我会明白我的意思.我怀疑这些都是加入条件.



Are all expecting integers on the SQL side and you are passing in strings in code that cannot be converted to integers.Check the stored procedure insland and you''ll see what I mean if I''m correct. I suspect these are all take part in a join condition.


我同意第一个答案.
我写我的答案是因为您将问题命名为"3层编码".

当仅使用SqlClient名称空间的SqlConnection时,为什么具有抽象数据库连接类?

我会理解您是否创建了摘要来处理不同类型的数据库.

为了您的目的,您只需要一个简单的基类,而不是抽象类.
建议您不要移动addland
,而不是创建clslndfxn类. 过程放入您的clslandprp类中,并使用以下结构.
在这种情况下,您无需检查连接状态并处理关闭状态

I agreed with the first answer.
I wrote my answer because you named the question "3 tier coding".

Why have the abstract database connection class when you use only SqlConnection of SqlClient namespace?

I would understand if you created abstract to handle different type of databases.

For your purposes you only need a simple base class, rather than abstract.
Instead of creating the class clslndfxn, I would recommend you to move addland
procedure into your clslandprp class and use the following structure.
In this case you don''t need to check the connection state and handle it closing

using (SqlConnection conn=new SqlConnection(......) )
{
 SqlCommand cmd=new SqlCommand("insland",con);
            cmd.Parameters.Add("@priceland",SqlDbType.VarChar,50).Value=r.priceland;
            cmd.Parameters.Add("@sqrft",SqlDbType.VarChar,50).Value=r.sqrft;
            cmd.Parameters.Add("@Pimage",SqlDbType.VarChar,50).Value=r.propertytype;
            cmd.Parameters.Add("@location",SqlDbType.VarChar,50).Value=r.location;

            con.Open();
            cmd.ExecuteNonQuery();
            cmd.Dispose();
  
}



只是有些想法...没什么私人的:-)

祝你好运.



Just some thinking... nothing personal :-)

Good luck.


cmd.Parameters.Add("@priceland", SqlDbType.VarChar, 50).Value=Convert.ToDouble(r.priceland);



等等...



and so on...


这篇关于3层编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆