1

これは常にエラーを返しcmd.ExecuteScalar()ますThe parameterized query '(@Name nvarchar(4000))select count(*) from Locations where name=' expects the parameter '@Name', which was not supplied.

私は何を間違えましたか?場所は文字列です。

        int count = 0;
        using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("select count(*) from Locations where name=@Name", conn);
            cmd.Parameters.AddWithValue("@Name",location);

            conn.Open();
            count = (int)cmd.ExecuteScalar();
            conn.Close();
        }
4

2 に答える 2

-1

このように使用すると役立ちます....

int count = 0;
        using(SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("select count(*) from Locations where name=@Name", conn);


                SqlParameter paramName = new SqlParameter("@Name", SqlDbType.VarChar, 255) { Value = "Avinash" };
                command.Parameters.Add(paramName);

            conn.Open();
            count = (int)cmd.ExecuteScalar();
            conn.Close();
        }
于 2012-04-23T07:03:04.587 に答える