Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
370 views
in Technique[技术] by (71.8m points)

sql - C# SqlParameters Short Hand

I'm taking data that is in a List of Record objects and putting their contents in to a database:

// Processes a Record and adds it to the database
public bool addRecord(SqlConnection db, List<Record> recordsToAdd)
{
    using (SqlCommand command = db.CreateCommand())
    {
        foreach (Record record in recordsToAdd)
        {
            // Set the query command text
            command.CommandText = @"INSERT INTO SMDGROUP_STPRODMASTER (PRODCODE, TOTFREE, TOTPHYS, ITEMTYPE, PRODESC) VALUES ('@PRODCODE', '@TOTFREE', '@TOTPHYS', '@ITEMTYPE', '@PRODESC')";

            SqlParameter param1 = new SqlParameter("@CURSTAT", record.curstat);
            SqlParameter param2 = new SqlParameter("@ITEMDESC", record.itemdesc);
            SqlParameter param3 = new SqlParameter("@PRODCODE", record.prodcode);
            SqlParameter param4 = new SqlParameter("@TOTFREE", record.totfree);
            SqlParameter param5 = new SqlParameter("@TOTPHYS", record.totphys);
            SqlParameter param6 = new SqlParameter("@ITEMTYPE", record.itemtype);
            SqlParameter param7 = new SqlParameter("@PRODESC", record.proddesc);

            command.Parameters.Add(param1);
            command.Parameters.Add(param2);
            command.Parameters.Add(param3);
            command.Parameters.Add(param4);
            command.Parameters.Add(param5);
            command.Parameters.Add(param6);
            command.Parameters.Add(param7);

            // Execute the query
            command.ExecuteNonQuery();
        }
        return true;
    }
}

Here's my Record class:

class Record
{
    public string curstat { get; set; }
    public string itemtype { get; set; }
    public string itemdesc { get; set; }
    public string prodcode { get; set; }
    public string proddesc { get; set; }
    public string totfree { get; set; }
    public string totphys { get; set; }
}

Just from looking at the code, I've got a feeling that there is a shorter way of achieving this.

But secondly, I'm not even sure if I've done it correctly that the @PARAMETER values are being replaced.

If I view the contents of command, it still shows the query string with the @ parameters.

Also, I'm getting this error on command.ExecuteNonQuery():

String or binary data would be truncated.

The statement has been terminated.

So, my questions are:

  • Is there a shorter way to set and add multiple parameters to the query?
  • What could be causing the error?
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You have a bigger constructor:

 command.Parameters.Add(
    "@CategoryName", SqlDbType.VarChar, 80).Value = "toasters";

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...