为什么不能更新到数据库呢
以下代码我是想实现把 data_ask_style表的数据ID随机的插入到data_ask的styleid字段里但是当执行到cmd2.ExecuteNonQuery();时不动了.
namespace Foosun.Web.ask
{
public partial class Rand : System.Web.UI.Page
{
string strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["data"].ConnectionString;
protected string mySession;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["Admin"] != null)
{
kl();
}
else
{
Response.Redirect("Admin_Login.aspx");
}
}
}
protected void main()
{
try
{
string strsql = "select * from data_ask order by id asc";
using (SqlConnection conn = new SqlConnection(strConn))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(strsql, conn))
{
using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
while (dr.Read())
{
updates(Convert.ToInt16(dr["id"].ToString()));
}
}
}
}
}
Response.Write("<script>window.alert('成功加入随机模块!')</script>");
return;
}
catch
{
Response.Write("<script>window.alert('参数出错!')</script>");
return;
}
}
protected void updates(int askID)
{
try
{
int styleid;
string sqll = "select * from data_ask_style where IsDelete=0 order by styleid desc";
using (SqlConnection conn = new SqlConnection(strConn))
{
conn.Open();
using (SqlCommand cmd3 = new SqlCommand())
{
cmd3.Connection = conn;
string sqlk = "select count(*) from data_ask_style where IsDelete=0";
cmd3.CommandText = sqlk;
int kdo = Convert.ToInt16((int)cmd3.ExecuteScalar());
cmd3.CommandText = sqll;
using (SqlDataReader dr1 = cmd3.ExecuteReader())
{
int i = 0;
int[] kok = new int[kdo];
while (dr1.Read())
{
kok[i] = Convert.ToInt16(dr1["styleid"]);
i++;
}
Random r = new Random();
int p;
p = r.Next(0, i);
styleid = kok[p];
}
}
string strsql1 = "update data_ask set styleid=@styleid where id=" + askID + "";
using (SqlCommand cmd2=new SqlCommand(strsql1,conn))
{
cmd2.Parameters.AddWithValue("@styleid", styleid);
Response.Write(strsql1);
Response.Write("<br>");
cmd2.ExecuteNonQuery();
return;
}
}
}
catch
{
//Response.Write("<script>window.alert('数据异常!')</script>");
//return;
}
return;
}
}
} --------------------编程问答-------------------- 怎么没人理我呢? --------------------编程问答-------------------- kl方法是干嘛的? --------------------编程问答-------------------- 呵呵..打错了的
应该是main(); --------------------编程问答--------------------
那是打错,调用的是main方法 --------------------编程问答--------------------
大致看了下,没看到什么错
你可以调试下,看到底除了什么异常,
还有sql执行的很慢的话,你可以sql放到查询分析器里面试试。
这个问题估计要调试了 --------------------编程问答-------------------- 太多的using
看真眼晕。
--------------------编程问答--------------------
试了一下,如果只更新10条是很快的,但是10条以上就很慢了。
就当执行到cmd2.ExecuteNonQuery();
然后就很慢,大概要等1分钟的时间再循环到下一条。 --------------------编程问答--------------------
顶
--------------------编程问答-------------------- 程序运行的慢,不一定就是程序的原因,也有可能是机器的原因。当然还是应该先查找程序是不是有错误。 --------------------编程问答--------------------
现在的情况是只要更新10条就能运行,并且能成功更新数据库
但是更新10以上就不动了,并且数据库也没有更新 --------------------编程问答-------------------- 高手呢? --------------------编程问答-------------------- 再顶顶
问题依旧 --------------------编程问答-------------------- 可在Main函数中将
"select * from data_ask_style where IsDelete=0 order by styleid desc";
的查询结果用DataTable保存下来
然后修改Updates函数
protected void updates(int askID,datatable dt)
{
Random random = new Random();
int nPos;
nPos = random.Next(0, dt.Rows.Count);
string styleid = Convert.ToInt16(dt.Rows[nPos]["styleid"]);
string strsql = "update data_ask set styleid=@styleid where id=" + askID + "";
try
{
using (SqlCommand cmd2=new SqlCommand(strsql1,conn))
{
cmd2.Parameters.AddWithValue("@styleid", styleid);
Response.Write(strsql);
Response.Write(" <br>");
cmd2.ExecuteNonQuery();
return;
}
}
catch
{
}
return;
} --------------------编程问答-------------------- --------------------编程问答-------------------- using (SqlDataReader dr = cmd.ExecuteReader())
{
if (dr.HasRows)
{
while (dr.Read())
{
updates(Convert.ToInt16(dr["id"].ToString()));
}
}
}
怎么可以一边用SqlDataReader 一边update数据库呢? --------------------编程问答-------------------- 还是自己弄好了
代码如下,直接把数据读到Table表,全部替换后再更新到数据库
namespace Hekl.Web.ask
{
public partial class Rand : System.Web.UI.Page
{
string strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["foosun"].ConnectionString;
protected string mySession;
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
if (Session["Admin"] != null)
{
main();
}
else
{
Response.Redirect("Admin_Login.aspx");
}
}
}
protected void main()
{
try
{
string strsql = "select * from data_ask order by id asc";
using (SqlConnection conn = new SqlConnection(strConn))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(strsql, conn))
{
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds,"table1");
DataTable dt = ds.Tables["table1"];
updates(dt);
SqlCommandBuilder cmbuild = new SqlCommandBuilder(da);
da.Update(dt);
}
}
Response.Write("<script>window.alert('成功加入随机模块!')</script>");
return;
}
catch (System.Data.SqlClient.SqlException exception)
{
//Response.Write(string.Format("异常信息:{0}",exception.Message));
Response.Write("<script>window.alert('参数出错!')</script>");
return;
}
}
protected void updates(DataTable dt)
{
int j = dt.Rows.Count;
try
{
int styleid;
string sqll = "select * from data_ask_style where IsDelete=0 order by styleid desc";
using (SqlConnection conn = new SqlConnection(strConn))
{
conn.Open();
using (SqlCommand cmd3 = new SqlCommand())
{
cmd3.Connection = conn;
string sqlk = "select count(*) from data_ask_style where IsDelete=0";
cmd3.CommandText = sqlk;
int kdo = Convert.ToInt16((int)cmd3.ExecuteScalar());
cmd3.CommandText = sqll;
using (SqlDataReader dr1 = cmd3.ExecuteReader())
{
int i = 0;
int[] kok = new int[kdo];
while (dr1.Read())
{
kok[i] = Convert.ToInt16(dr1["styleid"]);
i++;
}
Random r = new Random();
int p;
for (int k = 0; k < j+1; k++)
{
p = r.Next(0, i);
styleid = kok[p];
dt.Rows[k]["styleid"] = styleid;
Response.Write("更新ID:" + dt.Rows[k]["id"] + "的styleid为:" + dt.Rows[k]["styleid"] + "<br>");
}
}
}
}
}
catch
{
//Response.Write(" <script>window.alert('数据异常!') </script>");
//return;
}
return;
} --------------------编程问答-------------------- for (int k = 0; k < j+1; k++) 这个应该是
for (int k = 0; k < j; k++)
补充:.NET技术 , ASP.NET