前端需要后端调用数据库指定数据使用时,接口代码如下:
public JArray GetzuobiaoData(string GNSSID)
{
string sqlStr = " sql语句筛选条件 "; //sql语句筛选条件
DataSet ds = SqlHelper.Query(sqlStr);//SqlHlper是一个自定义sql语句类
DataTable outdtb = ds.Tables[0];
JArray json = new JArray();
for (int i = 0; i < outdtb.Rows.Count; i++)
{
DataRow row = outdtb.Rows[i];
JObject jo = new JObject();
jo.Add(new JProperty("属性", row["字段名"].ToString()));
jo.Add(new JProperty("属性" , row["字段名"].ToString()));
jo.Add(new JProperty("属性", row["字段名"].ToString()));
json.Add(jo);
}
return json;
}
//SqlHelper.Query方法:
public static DataSet Query(string sqlStr)
{
string connstr="数据库连接语句";
using (SqlConnection conn = new SqlConnection(connstr))
{
using (SqlDataAdapter ada = new SqlDataAdapter(sqlStr, conn))
{
try
{
conn.Open();
DataSet ds = new DataSet();
return ds;
}
catch (SqlException e)
{
throw e;
}
finally
{
conn.Close();
}
}
}
}