获得数据库指定字段的值,赋给本地变量
(1)如下,获得userinfo数据表里的字段"userid"、"orgid",
string userid=""; string orgid=""; using (SQLiteConnection con = new SQLiteConnection(Constants.DATA_SOURCE)) { con.Open(); using (SQLiteCommand cmd = new SQLiteCommand()) { cmd.Connection = con; cmd.CommandText = string.Format("select * from userinfo t "); using (SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection)) { while(dr.Read()) { userid = dr["userid"].ToString(); orgid = dr["orgid"].ToString(); } } } }
(如果多表查询时,需要的多张表有相同字段,select时给相同的字段再设置别名)
(2)同上,根据下标获得搜索到的字段的值
如下,将select到的第三个字段的值赋给aaa
string aaa = "";
while(dr.Read()) { aaa = dr[2].ToString(); }
其他:
NameValueCollection :<key,value>集合,根据索引或key获得value
(1)根据索引获得value
可以用NameValueCollection 存储查询到的信息,根据索引获得value
dr.GetValues():获取查询到的所有<key,value>
if (dr.Read()) { NameValueCollection user = dr.GetValues(); string[] value = user.GetValues(0); return user; }
(2)根据key获得value
if (dr.Read()) { NameValueCollection user = dr.GetValues(); string values = user.Get("id"); return user; }
id:就是数据库中的字段
NameValueCollection 的小例子:
参考:http://www.cnblogs.com/jirigala/archive/2012/02/07/2341979.html