向数据库添加数据的问题~~~~~大神速来~
SqlCommand selectcmd1 = new SqlCommand();
selectcmd1.CommandText = "select logincount,UserName from Role";
selectcmd1.Connection = con;
SqlCommand edit = new SqlCommand();
edit.CommandText = "updata Role set logincount=@logincount where UserName='"+Login1.UserName + "' ";
edit.Connection = con;
da.SelectCommand = selectcmd1;
da.Fill(ds1, "Role");
//DataRow row = ds1.Tables["Role"].Rows[0];
//Session["name"] = row[0];
edit.Parameters.Add("@logincount", SqlDbType.Int, 4, "logincount");
da.UpdateCommand = edit;
DataRow editdr = null;
foreach (DataRow dr1 in ds1.Tables["Role"].Rows)
{
if (dr1[1].ToString() == Login1.UserName)
{
editdr = dr1;
break;
}
}
editdr["logincount"] = int.Parse(editdr["logincount"].ToString()) + 1;
//Session["name"]=editdr["logincount"];
da.Update(ds1, "Role");
con.Close();
Login1.DestinationPageUrl = "~/Main.aspx";
此段为向数据库修改登录者登录次数 logincount 代码
但数据库死都不让我修改logincount字段 并且还不报错
求大神给力~!!!!!!!!!!!!! --------------------编程问答-------------------- 来人啊啊啊
顶个 --------------------编程问答-------------------- updata Role 中ROLE会不会是保留关键字? --------------------编程问答-------------------- 报错信息贴出来看下 --------------------编程问答-------------------- --------------------编程问答-------------------- 你好像没有给edit.Parameters的 logincount参数赋值
edit.Parameters.Add("@logincount", SqlDbType.Int, 4, "logincount").Value = xxxx; --------------------编程问答-------------------- 可以写成:updata Role set logincount=logincount + 1 where ....
出错可能是logincount超出了范围,我猜的。 --------------------编程问答-------------------- 把SQL语句放到数据库中执行以下试试,是不是语句出问题了? --------------------编程问答-------------------- @logincount 的值我怎么没有看到? --------------------编程问答-------------------- 语句没错。就是赋值增加的值搞错了 。@logincount 跟logincount --------------------编程问答--------------------
好像不是
我用Session测试了 Role表里面有值
--------------------编程问答--------------------
Vlaue 应该是填什么??? --------------------编程问答--------------------
Value 是你要更新的logincount值 --------------------编程问答--------------------
SqlCommand selectcmd1 = new SqlCommand();
selectcmd1.CommandText = "select logincount,UserName from Role";
selectcmd1.Connection = con;
SqlCommand edit = new SqlCommand();
edit.CommandText = "updata Role set logincount=@logincount where UserName=@UserName ";
edit.Connection = con;
da.SelectCommand = selectcmd1;
da.Fill(ds1, "Role");
//DataRow row = ds1.Tables["Role"].Rows[0];
//Session["name"] = row[0];
DataRow editdr = null;
foreach (DataRow dr1 in ds1.Tables["Role"].Rows)
{
if (dr1[1].ToString() == Login1.UserName)
{
editdr = dr1;
break;
}
}
editdr["logincount"] = int.Parse(editdr["logincount"].ToString()) + 1;
SqlParameter sp = new SqlParameter("@logincount", SqlDbType.Int, 4);
SqlParameter sp1 = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
sp.Value = editdr["logincount"].ToString();
sp1.Value = Login1.UserName;
edit.Parameters.Add(sp);
edit.Parameters.Add(sp1);
da.UpdateCommand = edit;
//edit.Parameters.Add("@logincount", SqlDbType.Int, 4, "logincount").Value=editdr["logincount"];//Session["name"]=editdr["logincount"];
da.Update(ds1, "Role");
con.Close();
Login1.DestinationPageUrl = "~/Main.aspx";
修改以后的代码
da.Update(ds1, "Role");被注释以后可以跳转到Main页
如果不注释 da.Update(ds1, "Role");
页面不跳转 数据库不添加数据 而且还不报错 --------------------编程问答--------------------
重新设过值后 问题依旧 我改过之后的代码
SqlCommand selectcmd1 = new SqlCommand();--------------------编程问答-------------------- 你值都没给,它什么更新? --------------------编程问答--------------------
selectcmd1.CommandText = "select logincount,UserName from Role";
selectcmd1.Connection = con;
SqlCommand edit = new SqlCommand();
edit.CommandText = "updata Role set logincount=@logincount where UserName=@UserName ";
edit.Connection = con;
da.SelectCommand = selectcmd1;
da.Fill(ds1, "Role");
//DataRow row = ds1.Tables["Role"].Rows[0];
//Session["name"] = row[0];
DataRow editdr = null;
foreach (DataRow dr1 in ds1.Tables["Role"].Rows)
{
if (dr1[1].ToString() == Login1.UserName)
{
editdr = dr1;
break;
}
}
editdr["logincount"] = int.Parse(editdr["logincount"].ToString()) + 1;
SqlParameter sp = new SqlParameter("@logincount", SqlDbType.Int, 4);
SqlParameter sp1 = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
sp.Value = editdr["logincount"].ToString();
sp1.Value = Login1.UserName;
edit.Parameters.Add(sp);
edit.Parameters.Add(sp1);
da.UpdateCommand = edit;
//edit.Parameters.Add("@logincount", SqlDbType.Int, 4, "logincount").Value=editdr["logincount"];//Session["name"]=editdr["logincount"];
da.Update(ds1, "Role");
con.Close();
Login1.DestinationPageUrl = "~/Main.aspx";
该过以后的应该给了值了吧
还是一样啊
你看下咯 --------------------编程问答--------------------
该过以后的应该给了值了吧
还是一样啊
你看下咯 --------------------编程问答-------------------- 晕 没人了么?
大神 速来
--------------------编程问答-------------------- edit.Parameters.AddWidthValue("@logincount","它的值");
--------------------编程问答--------------------
是 with吧??
--------------------编程问答--------------------
SqlCommand selectcmd1 = new SqlCommand();
selectcmd1.CommandText = "select logincount,UserName from Role";
selectcmd1.Connection = con;
SqlCommand edit = new SqlCommand();
edit.CommandText = "updata Role set logincount=@logincount where UserName=@UserName ";
edit.Connection = con;
da.SelectCommand = selectcmd1;
da.Fill(ds1, "Role");
//DataRow row = ds1.Tables["Role"].Rows[0];
//Session["name"] = row[0];
DataRow editdr = null;
foreach (DataRow dr1 in ds1.Tables["Role"].Rows)
{
if (dr1[1].ToString() == Login1.UserName)
{
editdr = dr1;
break;
}
}
editdr["logincount"] = int.Parse(editdr["logincount"].ToString()) + 1;
edit.Parameters.AddWithValue("@logincount", int.Parse(editdr["logincount"].ToString()));
edit.Parameters.AddWithValue("@UserName",Login1.UserName.ToString());
//SqlParameter sp = new SqlParameter("@logincount", SqlDbType.Int, 4);
//SqlParameter sp1 = new SqlParameter("@UserName", SqlDbType.VarChar, 50);
//sp.Value = int.Parse(editdr["logincount"].ToString());
//sp1.Value = Login1.UserName;
//edit.Parameters.Add(sp);
//edit.Parameters.Add(sp1);
da.UpdateCommand = edit;
//edit.Parameters.Add("@logincount", SqlDbType.Int, 4, "logincount").Value=editdr["logincount"];//Session["name"]=editdr["logincount"];
da.Update(ds1, "Role");
con.Close();
Login1.DestinationPageUrl = "~/Main.aspx";
还是不行
数据没加进去 --------------------编程问答-------------------- 路过学习中。。。。。 --------------------编程问答-------------------- var cmd=new SqlCommand("updata Role set logincount=logincount+1 where UserName=@UserName",con );
cmd.Parameters.AddWithValue("@UserName",Login1.UserName.ToString());
con.Open();
cmd.ExecuteNonQuery()
con.Close();
补充:.NET技术 , ASP.NET