获取数据库值,再在其值上做修改
时间:2010-10-14 来源:Hpig
开始想用SqlDataReader读取数据值再修改
SqlDataReader rea = cmm.ExecuteReader();
if (rea.Read())
{
rea["yuedu"] = (int)rea["yuedu"] + 1;
}
结果报错,原因貌似SqlDataReader是只读的
后面用SqlDataAdapter解决了,
int i = Convert.ToInt32(Request.QueryString["id"]);
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlstr"].ConnectionString))
{
SqlCommand cmm = new SqlCommand("select * from new where ID='" + i + "'", con);
SqlDataAdapter ada = new SqlDataAdapter();
ada.SelectCommand = cmm;
SqlCommandBuilder bui = new SqlCommandBuilder(ada);
DataSet ds = new DataSet();
ada.Fill(ds, "tb1");
int num = Convert.ToInt32(ds.Tables["tb1"].Rows[0][5]);
ds.Tables["tb1"].Rows[0][5] = (num + 1).ToString();
ada.Update(ds, "tb1");
PS:1.SqlDataAdapter必须选取主键才能更新
2.不必开关执行sqlconnection.
}
留住以后参考.