Insert numerical (decimal) data from textbox values

后端 未结 2 967
[愿得一人]
[愿得一人] 2021-01-26 12:01

I am confused by the following issue;

I have a C# (WindowsForms) application which I connect to a SQL Server DB and have no problem to INSERT, SELECT, UPDATE... until I

2条回答
  •  感动是毒
    2021-01-26 12:33

    This is not a direct answer to your question, but please (!) replace this ugly method by this:

    Create a class for your contracts. This will make it much easier to handle contracts. If you have several methods handlings contracts in some way, you will not have to change the almost endless parameter lists of all of them, when properties are added to the contract.

    public class Contract
    {
        public int EmployeeID { get; set; }
        public string Agency { get; set; }
        public string Role { get; set; }
        ... and so on
    }
    

    and change the method signature to

    public void CreateNewContract(Contract contract)
    

    Headers of methods loading contracts form the database would look like this

    public List LoadAllContracts()
    
    // Assuming contractID is the primary key
    public Contract LoadContractByID(int contractID)
    

    Much easier than returning 1000 variables!

    You can create a new contract with

    var contract = new Contract {
        EmployeeID = 22,
        Agency = "unknown",
        Role = "important", 
        ...
    };
    

    Also (as others have pointed out already) use command parameters.

    newCmd.Parameters.AddWithValue("@EmployeeID", contract.EmployeeID);
    newCmd.Parameters.AddWithValue("@Agency", contract.Agency);
    newCmd.Parameters.AddWithValue("@Role", contract.Role);
    

    (HaLaBi's post shows how to formulate your insert command string.)

提交回复
热议问题