Friday, February 24, 2012

Inserting to SQL DB from a form using parameters

I have a web form for new members to fill out and save their info in a SQL database to be retrieved later. There will be like 20 elements in the form mostly textboxes. Is there a good example of how to read all the elements and have them INSERTed in a SQL Database? I imagine parameters and a SP would be the way to go. So if someone could provide me with some sample code or a good link.

Thanks in advance,Here's a sample calling a stored procedure with parameters and with a variety of data types.


SqlCommand cm= new SqlCommand();
cm.Connection= Connection;
cm.CommandType= CommandType.StoredProcedure;
cm.CommandText= "StoredProcedureName";
SqlParameter sp1 = cm.Parameters.Add( new SqlParameter("@.Field1",SqlDbType.Int,4)); sp1.Value = salesID;
SqlParameter sp2 = cm.Parameters.Add( new SqlParameter("@.Field2",SqlDbType.Int, 4)); sp2.Value = someIntValue;
SqlParameter sp3 = cm.Parameters.Add( new SqlParameter("@.Field3",SqlDbType.Money, 8));sp3.Value = someDoubleValue;
SqlParameter sp4 = cm.Parameters.Add( new SqlParameter("@.Field4",SqlDbType.Float, 8 )); sp4.Value = anotherDoubleValue;
SqlParameter sp5 = cm.Parameters.Add( new SqlParameter("@.Field5",SqlDbType.DateTime, 8)); sp5.Value = someDateTimeValue
Connection.Open();
cm.ExecuteNonQuery();
Connection.Close();

No comments:

Post a Comment