没有使用MVC将文本框中的值传递给SQL

本文关键字:值传 SQL 文本 MVC | 更新日期: 2023-09-27 18:20:24

表单代码:

public partial class Customer_Information : Form
{
    private Controller controller;
    Customer_Information info;
    public Customer_Information()
    {
        InitializeComponent();
        controller = new Controller();                 
    }
    private void Customer_Information_Load(object sender, EventArgs e)
    {
      //  btnnext.Enabled = false;
        info = new Customer_Information();           
    }
private void btnsave_Click(object sender, EventArgs e)
    {
               View view = controller.CreateProduct(info);
                controller.InsertProduct(view);
    }

视图.cs:

class View
{
    public string BILL_TO { get; set; }
    public string SHIP_TO { get; set; }
    public string VENDOR { get; set; }
    public string BUYER { get; set; }
    public string TERMS { get; set; }
    public string CONTACTS { get; set; }
    public string CURRENCY { get; set; }
    public string PONUM { get; set; }
    public string ENTRYDATE { get; set; }
    public string SHIPDATE { get; set; }
    public string CANCELDATE { get; set; }
    public string ORDERTYPE { get; set; }

    public View(string billto, string shipto, string vendor, string buyer, string terms, string contacts, string currency, string ponum, string entryd, string shipd, string canceld, string ordert)
    {
        BILL_TO = billto;
        SHIP_TO = shipto;
        VENDOR = vendor;
        BUYER = buyer;
        TERMS = terms;
        CONTACTS = contacts;
        CURRENCY = currency;
        PONUM = ponum;
        ENTRYDATE = entryd;
        SHIPDATE = shipd;
        CANCELDATE = canceld;
        ORDERTYPE = ordert;
    }

Controller.cs:

class Controller
{
    private Model model;
    public Controller()
    {
        model = new Model();
    }
    public View CreateProduct(Customer_Information info)
    {
        string billto = info.txtbillto.Text;
        string shipto = info.txtshipto.Text;
        string vendor = info.txtvendor.Text;
        string buyer = info.txtbuyer.Text;
        string terms = info.txtterms.Text;
        string contacts = info.txtcontact.Text;
        string currency = info.cmbcurrrecncy.Text;
        string ponum = info.txtponum.Text;
        string entryd = info.txtentrydate.Text; 
        string shipd = info.txtshipdate.Text;
        string canceld = info.txtcanceldate.Text;
        string ordert = info.txtorder.Text;
        View view = new View(billto, shipto, vendor, buyer, terms, contacts, currency, ponum, entryd, shipd, canceld, ordert);
        return view;
    }
    public void InsertProduct(View view)
    {
        model.InsertProduct(view);
    }

型号.cs:

class Model{
    SqlConnection connect;
    SqlDataAdapter data = new SqlDataAdapter();
    public Model()
    {
        connect = new SqlConnection("connection");
    }
    public void InsertProduct(View view)
    {
        string billto = view.BILL_TO;
        string shipto = view.SHIP_TO;
        string vendor = view.VENDOR;
        string buyer = view.BUYER;
        string terms = view.TERMS;
        string contacts = view.CONTACTS;
        string currency = view.CURRENCY;
        string ponum = view.PONUM;
        string entryd = view.ENTRYDATE;
        string shipd = view.SHIPDATE;
        string canceld = view.CANCELDATE;
        string ordert = view.ORDERTYPE;
        data.InsertCommand = new SqlCommand("INSERT INTO PURCHASE VALUES(@Bill_to, @Ship_to, @Vendor, @Buyer, @Terms, @Contact, @Currency, @PO_number, @Entry_date, @Ship_date, @Cancel_Date, @Order_type)", connect);
        data.InsertCommand.Parameters.Add("@Bill_to", SqlDbType.VarChar).Value = billto;
        data.InsertCommand.Parameters.Add("@Ship_to", SqlDbType.VarChar).Value = shipto;
        data.InsertCommand.Parameters.Add("@Vendor", SqlDbType.VarChar).Value = vendor;
        data.InsertCommand.Parameters.Add("@Buyer", SqlDbType.VarChar).Value = buyer;
        data.InsertCommand.Parameters.Add("@Terms", SqlDbType.VarChar).Value = terms;
        data.InsertCommand.Parameters.Add("@Contact", SqlDbType.VarChar).Value = contacts;
        data.InsertCommand.Parameters.Add("@Currency", SqlDbType.VarChar).Value = currency;
        data.InsertCommand.Parameters.Add("@PO_number", SqlDbType.VarChar).Value = ponum;
        data.InsertCommand.Parameters.Add("@Entry_date", SqlDbType.VarChar).Value = entryd;
        data.InsertCommand.Parameters.Add("@Ship_date", SqlDbType.VarChar).Value = shipd;
        data.InsertCommand.Parameters.Add("@Cancel_Date", SqlDbType.VarChar).Value = canceld;
        data.InsertCommand.Parameters.Add("@Order_type", SqlDbType.VarChar).Value = ordert;
        connect.Open();
        data.InsertCommand.ExecuteNonQuery();
        connect.Close();
    }

程序完成,但在检查数据库时,传递的值为空。

没有使用MVC将文本框中的值传递给SQL

您可以使用

TableName object=new Tablename();
object.field1=value;
object.field2=value;
db.TableName.AddObject(object);