代码之家  ›  专栏  ›  技术社区  ›  MrM

如何在模型mvc的表中插入数据?

  •  0
  • MrM  · 技术社区  · 14 年前

    我的模型中有数据,如何设置将数据插入表中?

        public string Name { get; set; }
        public string Address { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string Zip { get; set; }
    
        public Info()
        {
            using (SqlConnection connect = new SqlConnection(connections))
            {
                string query = "Insert Into Personnel_Data (Name, StreetAddress, City, State, Zip, HomePhone, WorkPhone)" +
                    "Values('" + Name + "','" + Address + "','" + City + "','" + State + "','" + Zip + "','" + ContactHPhone + "','" + ContactWPhone + "')";
    
                SqlCommand command = new SqlCommand(query, connect);
                connect.Open();
                command.ExecuteNonQuery();
            }
        }
    

    运行查询时,名称、地址、城市等为空。我该怎么设置?

    4 回复  |  直到 14 年前
        1
  •  1
  •   wassertim    14 年前

    在使用构造函数之前,需要在构造函数中初始化属性:

    public string Name { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
    public string ContactHPhone { get;set; }
    public string ContactWPhone { get;set; }
    
    public Info(string name, string address, string city, string state, string zip, string contactHPhone, string contactWPhone)
    {
        Name = name;
        Address = address;
        City = city;
        State = state;
        Zip = zip;
        ContactHPhone = contactHPhone;
        ContactWPhone = contactWPhone;
        using (SqlConnection connect = new SqlConnection(connections))
        {
            string query = "Insert Into Personnel_Data (Name, StreetAddress, City, State, Zip, HomePhone, WorkPhone)" +
                "Values('" + Name + "','" + Address + "','" + City + "','" + State + "','" + Zip + "','" + ContactHPhone + "','" + ContactWPhone + "')";
    
            SqlCommand command = new SqlCommand(query, connect);
            connect.Open();
            command.ExecuteNonQuery();
        }
    }
    

    编辑:

    更好的方法是在sql字符串中使用参数:

    using (SqlConnection connect = new SqlConnection(connections))
    {
        string query = "Insert Into Personnel_Data (Name, StreetAddress, City, State, Zip, HomePhone, WorkPhone) Values(@name, @address, @city, @state, @zip, @contactHPhone, @contactWPhone)";
    
        SqlCommand command = new SqlCommand(query, connect);
        command.Parameters.AddWithValue("name", Name);
        command.Parameters.AddWithValue("address", Address);
        command.Parameters.AddWithValue("city", City);
        command.Parameters.AddWithValue("state", State);
        command.Parameters.AddWithValue("zip", Zip);
        command.Parameters.AddWithValue("contactHPhone", ContactHPhone);
        command.Parameters.AddWithValue("contactWPhone", ContactWPhone);
        connect.Open();
        command.ExecuteNonQuery();
    }
    
        2
  •  2
  •   Michael Maddox    14 年前

    在调用构造函数并设置属性之后,向构造函数添加参数或从非构造函数方法执行插入。

    您提供的代码易受SQL注入攻击,因此也需要修复该问题。

    另外,从术语上讲,调用insert a查询是令人困惑的。查询是一个select,这不是您要做的。

    您不需要存储过程或orm,使用a do.net就可以了。您可能会发现,使用orm可以减少必须编写和维护的重复性、易出错的代码,但使用orms也有缺点。

        3
  •  0
  •   Mahesh Velaga    14 年前

    首先使用 parametrized queries 如果你要坚持你现在的方法

    我的建议是 ORM 喜欢 NHibernate ,这会让你轻松很多

        4
  •  0
  •   Dave Swersky    14 年前

    这看起来像来自您试图持久化的对象(您的模型)的代码。接收更新和提交记录的代码将进入控制器。请把你的控制器的代码寄出去。

    另外,我强烈建议您考虑像linq to sql或linq to entities这样的orm。至少创建一个存储过程-不要将原始SQL发送到数据库。