c# - Why do I get @firstname and @lastname in my SQL table? -
I have the following code in my ASP.NET project and I still can not understand that saving my SQL Server table Why would any values of values be named instead of parameters help would be appreciated. [WebMethod] Public Static Zero InsertMethod (String First Name, String Last Name) {SqlConnection con = new SqlConnection (@ "Data Source = Kimberly / SQLSERVER02; Initial Catalog = Chalegh; User ID = * **; password = *** "); SqlCommand CMD = New SQL Commands ("Enter in Test Table Values ('Firstname', '@ Lastname')", Con); SqlParameter paramFirstName = New SqlParameter (); ParamFirstName.ParameterName = "@FirstName"; ParamFirstName.Value = firstname; Cmd.Parameters.Add (paramFirstName); SqlParameter paramLastName = New SqlParameter (); ParamLastName.ParameterName = "@lastname"; ParamLastName.Value = lastname; Cmd.Parameters.Add (paramLastName); Con.Open (); Cmd.ExecuteNonQuery ();
Remove quotation marks and use the method like this:
SQL Commands CMD = New SQL Commands ("Enter in Test Table Values (First Name, @ Last Name)", Con?); Cmd.Parameters.AddWithValue ("@first", firstname); Cmd.Parameters.AddWithValue ("Last name", lastname);
Your real problem is the quote mark . When you use quotation marks @ first name
and @lastname
, the parameters are considered as actual values instead. You do not have to use the AddWithValue
method but it is less and easy to use. You do not need to create SqlParameter
for each parameter, and each property has one And set it.
Comments
Post a Comment