C #, sp_executesql and incorrect syntax

I call the code below.

In the line (IDataReader dr = cmd.ExecuteReader ()) sql barfs with the wrong syntax next to "CompanyUpdate".

   using (SqlCommand cmd = new SqlCommand("CompanyUpdate"))
        {
            cmd.Parameters.Add("@CompanyID",SqlDbType.Int);
            cmd.Parameters.Add("@Description",SqlDbType.VarChar,50);
            cmd.Parameters["@CompanyID"].Value = companyid;
            cmd.Parameters["@Description"].Value = description;

            SqlConnection cn = new SqlConnection("Data Source=[datasource];Initial Catalog=dotNext;User ID=[user];Password=[password];Pooling=True;Application Name=dotNext");
            cn.Open();
            cmd.Connection = cn;
            using (IDataReader dr = cmd.ExecuteReader())
            {
                if (dr.Read())
                {
                    this.CompanyID = dr.GetInt32(0);
                }
            }
        }

I looked at sqlprofiler and noticed the following:

exec sp_executesql N'CompanyUpdate',N'@CompanyID int,@Description varchar(50)',@CompanyID=56,@Description='APC'

Collapsing my command with sp_executesql. All my other sql commands run without problems.

So my question is twofold: 1. Why is sp_executesql used? 2. What am I doing wrong?

Details: sql2005, C #, vs2005

+5
source share
1 answer

I noticed that you did not specify CommandType for StoredProcedure ... I do not know if the cause of your problem is or not:

cmd.CommandType = CommandType.StoredProcedure;

, .

, :

SQL Query Profiler . , SQL, . SQL sp_executesql, , - .. cmd.CommandType = CommandType.Text, exec, , . , SQL, , .

+11

All Articles