c#獲取資料庫內資料得到字串,將字串轉成陣列
阿新 • • 發佈:2020-12-08
記錄一下用c#獲取資料庫內資料得到陣列
[HttpGet, Route("Shop")]
public string Shop()
{
_log.Info(string.Format("Get:獲取商家資訊!"));
//獲取所有的
string sql = string.Format("select * from t_dpxx");
DataTable dt = sqlHelper.ExecuteReaderEx (sql);
if (dt.Rows.Count == 0)
return "";
string ret = "";
for (int i=0;i<dt.Rows.Count;i++)
{
ret += dt.Rows[i][0].ToString() + "|" + dt.Rows[i][1].ToString() + "|" + dt.Rows[i][ 2].ToString() + "|" + dt.Rows[i][3].ToString() + "|" + dt.Rows[i][4].ToString() + "|" + dt.Rows[i][5].ToString()+ "|" + dt.Rows[i][6].ToString()+"&";
}
return ret.Substring(0,ret.Length-1);//將最後一個&去除
}
備註:
dt.Rows.Count:資料庫中行個數
我們獲取到的資料:
當我們在程式中獲取到該陣列,可以用split分割的方法一個字串分割成字串陣列用for迴圈進行分割,得到我們需要的陣列。
var arr=res.data.split('&');
var newarray;
for (let i = 0; i < arr.length; i++)
{
var val= arr[i].split('|')
newarray={id: val[0],shopimg:val[1],shopname:val[2],send:val[3],delivery:val[4],month:val[5],cent:val[6]};
that.data.myArr.push(newarray);
}
that.setData
({
myArr:that.data.myArr
})