asp.net中的3层架构c# [英] 3 tier architecture in asp.net c#

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

问题描述

我的第一堂课是CustomerDal.cs

my first class is CustomerDal.cs

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web;

/// <summary>
/// Summary description for Class2
/// </summary>
public class CustomerDal
{
	public CustomerDal()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public int insert(string customer_id,string first_name,string last_name)  // public int insert(string customer_id,string first_name,string last_name)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["learnConnectionString"].ConnectionString);
        string s1 = "insert into customer(customer_id,first_name,last_name) values(@customer_id,@first_name,@last_name) ";
        SqlCommand cmd = new SqlCommand(s1,conn);
        //set the command type
        cmd.CommandType = CommandType.Text;
        try
        {
            cmd.Parameters.Add(new SqlParameter("customer_id", System.Data.SqlDbType.Int, 4));
           cmd.Parameters["@customer_id"].Direction = System.Data.ParameterDirection.Input;
            cmd.Parameters["@customer_id"].Size = 25;
            cmd.Parameters["@customer_id"].Value = customer_id;

            cmd.Parameters.Add(new SqlParameter("first_name", SqlDbType.VarChar));
            cmd.Parameters["@first_name"].Direction = ParameterDirection.Input;
            cmd.Parameters["@first_name"].Size = 25;
            cmd.Parameters["@first_name"].Value = first_name;

            cmd.Parameters.Add(new SqlParameter("last_name", SqlDbType.VarChar));
            cmd.Parameters["@last_name"].Direction = ParameterDirection.Input;
            cmd.Parameters["@last_name"].Size = 25;
            cmd.Parameters["@last_name"].Value = last_name;
            conn.Open();
           return cmd.ExecuteNonQuery();
        }
        catch
        {
          throw;
        }
        finally
        {
           conn.Close();
           cmd.Dispose();
            conn.Dispose();
        }
    }
}



我的第二类ic CustomerBal.cs


my second class ic CustomerBal.cs

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

/// <summary>
/// Summary description for Class1
/// </summary>
public class CustomerBal
{
	public CustomerBal()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public int insert(string customer_id, string first_name, string last_name)
    {
        CustomerDal cdal = new CustomerDal();
        try
        {
            return cdal.insert(customer_id, first_name, last_name);
        }
        catch 
        {
            throw;
        }
        finally
        {
            cdal=null;
        }
    }
}



和我的default.aspx页面是


and my default.aspx page is

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

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        CustomerBal Cbal = new CustomerBal();
        try
        {
            if (Cbal.insert(TextBox1.Text, TextBox2.Text, TextBox3.Text) > 0)
            {
                Label1.Text = "Record Inserted Successfully";
            }
            else
            {
                Label1.Text = "Record not Inserted";
            }
        }
        catch(Exception ex)
        {
            Label1.Text = ex.Message;

        }
        finally 
        {
            Cbal = null;
        }
    }
}



当我运行此程序时错误即将来临

错误:这个SqlParameterCollection不包含带有ParameterName''@ customer_id''的SqlParame。

请给我解决方案......


and when i run this program than the error is coming
Error:An SqlParameter with ParameterName ''@customer_id'' is not contained by this SqlParameterCollection.
please give me solution......

推荐答案

您正在使用



cmd.Parameters.Add(new SqlParameter( customer_id ,System.Data.SqlDbType.Int, 4));



我认为应该是



cmd.Parameters.Add(new SqlParameter( @customer_id ,System.Data.SqlDbType.Int,4));
You are using

cmd.Parameters.Add(new SqlParameter("customer_id", System.Data.SqlDbType.Int, 4));

I think it should be

cmd.Parameters.Add(new SqlParameter("@customer_id", System.Data.SqlDbType.Int, 4));


thank you very much
the problem is resolved but now a new problem arised
eror is:Invalid object name 'Customer'


请检查您在代码中指定的表名是是否存在于您的数据库中,如果存在,请查看we.config文件以获取连接信息。
Please check out that the table name which you have specify into the code is exist in your database or not and if it is exist then check out the we.config file for your connection inforamtion.


这篇关于asp.net中的3层架构c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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