Pass same parameters multiple times in SQLitecommand

╄→尐↘猪︶ㄣ 提交于 2019-12-11 19:19:32

问题


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 ...


来源:https://stackoverflow.com/questions/22527639/pass-same-parameters-multiple-times-in-sqlitecommand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!