从ASP表单向SQL插入数据 [英] Insert data to SQL from ASP form

查看:57
本文介绍了从ASP表单向SQL插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有问题。我有一个3层的项目。我用Ado。在人员表中我有3个外键。当我想将数据从asp Form插入到sql



我的数据库和程序准备就绪时没有任何反应。

这是我的数据层

Hi I have a problem. I have a project with 3 layers. I use Ado. In a person table I have 3 foreign keys. nothing happens when i want to insert data from asp Form to sql

my data base and procedures are ready.
that's my data layer

public class Data
   {
       public Data()
       {
           Connection1 = new SqlConnection("server=.; database=LLLL; Trusted_Connection=True");
           Command1 = Connection1.CreateCommand();
           Command1.CommandType = CommandType.StoredProcedure;
       }
       public SqlConnection Connection1 { get; private set; }
       public SqlCommand Command1 { get; set; }


   }



保存方法


save method

public  void Save()
       {

           Data dl= new Data();

           dl.Command1.CommandType = CommandType.StoredProcedure;
           dl.Command1.CommandText = this.GetType().Name + "_Save";
           foreach(PropertyInfo propertyinfo in this.GetType().GetProperties())
           {
               if (propertyinfo.Name != "Message")
               {

                   dl.Command1.Parameters.Add(new SqlParameter("@"+(propertyinfo.Name), propertyinfo.GetValue(this,null)));


               }
           }


           try
           {
               dl.Connection1.Open();
               dl.Command1.ExecuteNonQuery();

               this.Message = "Save id done successfully";

           }
           catch (SqlException ex)
           {
               this.Message = ex.Message;

           }
           finally
           {
               if (dl.Connection1.State == ConnectionState.Open)
               {
                   dl.Connection1.Close();
               }
           }

       }







这是BLL中人的构造者






and this is the constructor of person in BLL

public Person(int id, string firstname, string lastname, string fathername,
                      string nationalcode, string birthdate, int genderId, int eduDegreeId,
                      int addressId, string tel, string mobile, string email)
        {
            Id = id;
            FirstName = firstname;
            LastName = lastname;
            FatherName = fathername;
            NCode = nationalcode;
            BirthDate = birthdate;
            GenderId = genderId;
            DegreeId = eduDegreeId;
            AddressId = addressId;
            Phone = tel;
            Mobile = mobile;
            Email = email;

        }





我在Bll中编写了save方法。当我想从表示层插入数据时到SQL没有任何事情发生在人员表中。



and i wrote the save method in Bll.when i want to insert data from presentation layer to SQL nothing happens in person table.

protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

        var address = new Address(-1, PrvTxt.Text, CitTxt.Text, AvnTxt.Text, StrTxt.Text, NumTxt.Text, FlrTxt.Text);
        var person =  new Person(-1, NamTxt.Text, FmlTxt.Text, DadTxt.Text, NcdTxt.Text, BrdTxt.Text,
                                 Convert.ToInt32(GenderRadioBtnList.SelectedItem.Value), Convert.ToInt32(EduDropDownList.SelectedItem.Value),
                                 -1, PhnTxt.Text, CphTxt.Text, EmlTxt.Text);

            address.Save();
            person.Save();

    }

推荐答案

您没有简要描述您的编程结构。

希望有一个图层数据访问层。

步骤

1.从数据库或前端选择外键值

2.设置一个人对象(视图层)

3.在业务逻辑层上查看对象以实现您的逻辑(业务层)

4.在数据访问层上发布新对象(数据访问层)。

5.提取对象并存储在数据库中(使用商店程序也可以替代)



完全希望它有帮助.. ....

最好的问候
You don't describe briefly your programming structure.
Hope there is a layer data access layer.
Steps
1. Pick the Foreign Keys value from database or front end
2. Set on a person object(view layer)
3.Sent the object on Business logic layer to implement your logic(Business layer )
4.Sent the new object on data access layer(Data access layer).
5.Extract the object and store in database(using store procedure also alternatives)

Hope fully It helps......
Best Regards


这篇关于从ASP表单向SQL插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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