Pass same parameters multiple times in SQLitecomma

2019-09-05 16:08发布

问题:

I like to run below query :

select 
     lon, lat 
  from  rdf_city_poi p, rdf_location l 
     where p.location_id = l.location_id and p.poi_id = ? 
union all select 
     lon, lat 
 from  rdf_poi_address p, rdf_location l 
     where p.location_id = l.location_id and p.poi_id = ? 
union all select 
    lon, lat 
 from xtdp_poi_address pa, xtdp_location l 
     where pa.location_id=l.location_id and pa.poi_id= ?

As you can see it's required 3 times same parameter and it's value

parameter ("?") ::: value (ex. 12345)

I have written below code which take argument but only 1 time.

            int Carto_id = Convert.ToInt32(dtsqlquery2.Rows[k][0]);
            string selectpoly = xNodelatlongquery2.Replace("\r\n", string.Empty);
            //SQLiteCommand selectpolygon = new SQLiteCommand(selectpoly + Carto_id, con);

            int countparameters = selectpoly.Split('?').Length - 1;

            SQLiteCommand selectpolygon = new SQLiteCommand(selectpoly, con);

            //var paramaters = new string[countparameters];

            //SQLiteParameter[] parameters = new SQLiteParameter[countparameters];
            for (int i = 0; i < countparameters; i++)
            {
                //SQLiteParameter[] parame = new SQLiteParameter[]{
                //new SQLiteParameter("?", Carto_id)};

                //paramaters[i] = string.Format("?", Carto_id);
                //selectpolygon.Parameters.AddWithValue(paramaters[i], Carto_id);

                selectpolygon.Parameters.AddWithValue("?", Carto_id);
            }


            //selectpolygon.Parameters.AddRange(paramaters);

            SQLiteDataReader dataReaderpoly = selectpolygon.ExecuteReader();
            DataSet ds1 = new DataSet();

You can see I have tried with some logics but they are not satisfying my requirement. How can I Add array of parameters into command of my logic ?

回答1:

In SQLite, you can give parameters a number explicitly:

... WHERE x = ?1 OR y = ?1 AND z <> ?1 ...

or give it a name:

... WHERE x = :PoiID OR y = :PoiID AND z <> :PoiID ...