我有那段代码:
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 :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();
并始终存在参数未更改的错误; / 我错了什么?
答案 0 :(得分:4)
您需要使用占位符来使AddWithValue起作用。以下是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);
}
}
}
答案 1 :(得分:2)
ODBC连接不像其他连接类型那样使用命名占位符。 ODBC使用由问号(?)标记的位置参数。用问号替换绑定变量,并确保以正确的顺序添加参数。传递给AddWithValue()的参数名称可以是任何内容。像这样:
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();
答案 2 :(得分:1)
您确定您可以使用名称为“ko.SEZON”的参数,即“。”在名字里?尝试删除“。”从两个名称的出现(查询和“AddWithValue”)。