开发者

parameters not update in sql statement

开发者 https://www.devze.com 2023-02-04 14:22 出处:网络
I have that code : string query = \"select count(*) from MODEL m join KOLEKCJA ko on m.SEZON = ko.sezon

I have that code :

string query = "select count(*) 
                  from MODEL m 
                  join KOLEKCJA ko on m.SEZON = ko.sezon 
             left join PRODUCENCI p on p.PRODUCENT_ID = m.PRODUCENT_ID 
             left join KRAJ k on k.KOD = m.KRAJ_POCH 
                 where ko.SEZON like :ko.SEZON 
                   and m.DO_PRODUKCJI like :m.DO_PRODUKCJI 
                   and k.KOD like :KOD 
                   and p.PRODUCENT_ID like :开发者_StackOverflow中文版PRODUCENT_ID 
                   and m.MODEL_ID like :MODEL_ID";

OdbcCommand comm = new OdbcCommand();
comm.Connection = con;
comm.CommandText = query;
comm.CommandType = CommandType.Text;
comm.Parameters.AddWithValue("ko.SEZON", sezon);
comm.Parameters.AddWithValue("m.DO_PRODUKCJI", do_produkcji);
comm.Parameters.AddWithValue("KOD", kraj);
comm.Parameters.AddWithValue("PRODUCENT_ID", fabryka);
comm.Parameters.AddWithValue("MODEL_ID", model);

result = (int)comm.ExecuteScalar();

and always have error that parameters are not changed ;/ What do I wrong ?


You need to use placeholders, for AddWithValue to work. Here's an excerpt of an example from MSDN:

private static void UpdateDemographics(Int32 customerID,
    string demoXml, string connectionString)
{
    // Update the demographics for a store, which is stored 
    // in an xml column. 
    string commandText = "UPDATE Sales.Store SET Demographics = @demographics "
        + "WHERE CustomerID = @ID;";

    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(commandText, connection);
        command.Parameters.Add("@ID", SqlDbType.Int);
        command.Parameters["@ID"].Value = customerID;

        // Use AddWithValue to assign Demographics.
        // SQL Server will implicitly convert strings into XML.
        command.Parameters.AddWithValue("@demographics", demoXml);

        try
        {
            connection.Open();
            Int32 rowsAffected = command.ExecuteNonQuery();
            Console.WriteLine("RowsAffected: {0}", rowsAffected);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}


ODBC connections do not work with named placeholders like the other connection types do. ODBC uses positional parameters marked by a question mark (?). Replace your bind variables with question marks and be sure to add your parameters in the correct order. The parameter name you pass to AddWithValue() can be anything. Like so:

string query = "select count(*) 
                  from MODEL m 
                  join KOLEKCJA ko on m.SEZON = ko.sezon 
             left join PRODUCENCI p on p.PRODUCENT_ID = m.PRODUCENT_ID 
             left join KRAJ k on k.KOD = m.KRAJ_POCH 
                 where ko.SEZON       like ? 
                   and m.DO_PRODUKCJI like ? 
                   and k.KOD          like ? 
                   and p.PRODUCENT_ID like ? 
                   and m.MODEL_ID     like ?";

OdbcCommand comm = new OdbcCommand();
comm.Connection = con;
comm.CommandText = query;
comm.CommandType = CommandType.Text;
comm.Parameters.AddWithValue("ko.SEZON", sezon);
comm.Parameters.AddWithValue("m.DO_PRODUKCJI", do_produkcji);
comm.Parameters.AddWithValue("KOD", kraj);
comm.Parameters.AddWithValue("PRODUCENT_ID", fabryka);
comm.Parameters.AddWithValue("MODEL_ID", model);

result = (int)comm.ExecuteScalar();


Are you sure you can have a parameter by the name of "ko.SEZON", that is with a "." in the name? Try removing that "." from both occurences of the name (query and "AddWithValue").

0

精彩评论

暂无评论...
验证码 换一张
取 消