-3

I got this code from Stackoverflow to insert data in table. But I wonder what it actually doing can someone please explain me this, What cmd.Parameters.add method does? 2: Why data conversion required? 3: What is @BSal here ?

cmd.Parameters.Add("@BSal", SqlDbType.Decimal).Value = Convert.ToDecimal(txtBSal.Text);
                    cmd.Connection = Connection.con;
                    cmd.ExecuteNonQuery();
surya
  • 1
  • 1
  • 5
  • LOL well if you got the code from `SO` then why not step thru the code and see for yourself what it's doing.. also if you are not familiar with SQL Command Parameters then MSDN is a great place to start.. come on now show some more effort – MethodMan Nov 24 '14 at 17:27
  • 1
    http://msdn.microsoft.com/en-us/library/System.Data.SqlClient.SqlParameterCollection.Add(v=vs.110).aspx Simple search will take you here. – Rahul Singh Nov 24 '14 at 17:27
  • It's a [parameterized](http://msdn.microsoft.com/en-us/library/vstudio/bb738521%28v=vs.100%29.aspx) query. They help make them reusable so you can execute the same query with different requirements. For example, if I have a database with 1,000 people, I shouldn't write 1,000 queries with different names when I can write one query and change the name required. – AdamMc331 Nov 24 '14 at 17:27

1 Answers1

1

@BSal is the name of a SQL parameter in your stored procedure or SQL statement.

cmd.Parameters.Add fills in a SQL parameter's values. The convert to Decimal is required because the type of the parameter is SqlDbType.Decimal and currently txtBSal is a text box, so you must convert the string to a decimal before it can be used.

Shriike
  • 1,351
  • 9
  • 20