数据库修改代码
namespace WebApplication1
{
public class SQLClass
{
protected string SqlStr; //数据库字符串
public SqlConnection SqlCON; //数据库连接变量 public SQLClass()
{
SqlStr = "server=JINLANG;database=ChatWeb;uid=sa;pwd=programmsdn";
}
/// <summary>
/// 数据库打开
/// </summary>
public void open()
{
if (SqlCON == null)
{
SqlCON = new SqlConnection(SqlStr);
SqlCON.Open();
}
else if(SqlCON.State.Equals(ConnectionState.Closed))
{
SqlCON.Open();
}
}
/// <summary>
/// 数据库关闭
/// </summary>
public void close()
{
if(SqlCON.State.Equals(ConnectionState.Open))
{
SqlCON.Close();
}
} public bool Databaseoperation(string StrCOM)
{
SqlCommand com = new SqlCommand(StrCOM, SqlCON);
try
{
bool num = Convert.ToBoolean(com.ExecuteNonQuery());
return num;
}
catch
{
return false;
}
finally
{
close();
}
}
public SqlDataReader ExceRead(string sqlcom)
{
open();
SqlCommand com = new SqlCommand(sqlcom,SqlCON );
SqlDataReader read = com.ExecuteReader(); //创建数据阅读器
return read;
}
}
}录入数据 页 提交按钮面代码 string TSlr = "insert into BookTable values('"+TextBoxBookName.Text.Trim()+"','"+Convert.ToInt32(TextBoxBookNUM.Text.Trim())+"','"+Convert.ToInt32(TextBoxBookPrice.Text.Trim())+"')";
SQLClass sqlclass = new SQLClass();
sqlclass.open();
bool LRresult = sqlclass.Databaseoperation(TSlr);
if (LRresult)
{
sqlclass.close();
Response.Write("<script language=javascript>alert('录入成功');location='BookLR.aspx'</script>");
}
else
{
sqlclass.close();
Response.Write("<script language=javascript>alert('录入失败');location='BookLR.aspx'</script>");
}这个 是 数据显示 页面代码
SQLClass sqlclass = new SQLClass();
sqlclass.open();
SqlDataAdapter adp = new SqlDataAdapter("select * from BookTable",sqlclass.SqlCON);
DataSet ds = new DataSet();
int counter = adp.Fill(ds,"BookTable");
Response.Write("共获取" + counter + "条数据" + "<a href='BookLR.aspx'>录入新图书</a>"+"<br/>");
foreach (DataRow mydr in ds.Tables["BookTable"].Rows)
{
Response.Write(mydr["ID"].ToString()+" "+mydr["BookNmae"].ToString()+" "+mydr["BookNUM"].ToString()+" "+mydr["BookPrice"].ToString()+"<a href='TSxiugai.aspx?shuming="+mydr["BookNmae"].ToString()+"&shuliang="+mydr["BookNUM"].ToString()+"&jiage="+mydr["BookPrice"].ToString()+"'>修改</a>"+"<br/>");
}修改 数据页面代码
public partial class TSxiugai : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBoxSM.Text=Request.QueryString["shuming"];
TextBoxSL.Text=Request.QueryString["shuliang"];
TextBoxJG.Text=Request.QueryString["jiage"];
} protected void Button1_Click(object sender, EventArgs e)
{
string Strxiugai = "update BookTable set BookNmae='"+TextBoxSM.Text.Trim()+"',BookNUM='"+Convert.ToInt32(TextBoxSL.Text.Trim())+"',BookPrice='"+Convert.ToInt32(TextBoxJG.Text.Trim())+"' where BookNmae='"+Request.QueryString["shuming"]+"' ";
SQLClass sqlclass = new SQLClass();
sqlclass.open();
bool XGresult = sqlclass.Databaseoperation(Strxiugai);
if (XGresult)
{
sqlclass.close();
Response.Write("<script language=javascript>alert('修改成功');location='BookList.aspx'</script>"); }
else
{
sqlclass.close();
Response.Write("<script language=javascrip>alert('修改失败');location='TSxiugai.aspx'</script>");
} }
}
编译 运行都可以 从 显示数据页面点击修改连接 跳到 修改页面 修改数据后跳到 显示页面
查了 显示页面和 数据库里面的数据 都没有变 秋杰
namespace WebApplication1
{
public class SQLClass
{
protected string SqlStr; //数据库字符串
public SqlConnection SqlCON; //数据库连接变量 public SQLClass()
{
SqlStr = "server=JINLANG;database=ChatWeb;uid=sa;pwd=programmsdn";
}
/// <summary>
/// 数据库打开
/// </summary>
public void open()
{
if (SqlCON == null)
{
SqlCON = new SqlConnection(SqlStr);
SqlCON.Open();
}
else if(SqlCON.State.Equals(ConnectionState.Closed))
{
SqlCON.Open();
}
}
/// <summary>
/// 数据库关闭
/// </summary>
public void close()
{
if(SqlCON.State.Equals(ConnectionState.Open))
{
SqlCON.Close();
}
} public bool Databaseoperation(string StrCOM)
{
SqlCommand com = new SqlCommand(StrCOM, SqlCON);
try
{
bool num = Convert.ToBoolean(com.ExecuteNonQuery());
return num;
}
catch
{
return false;
}
finally
{
close();
}
}
public SqlDataReader ExceRead(string sqlcom)
{
open();
SqlCommand com = new SqlCommand(sqlcom,SqlCON );
SqlDataReader read = com.ExecuteReader(); //创建数据阅读器
return read;
}
}
}录入数据 页 提交按钮面代码 string TSlr = "insert into BookTable values('"+TextBoxBookName.Text.Trim()+"','"+Convert.ToInt32(TextBoxBookNUM.Text.Trim())+"','"+Convert.ToInt32(TextBoxBookPrice.Text.Trim())+"')";
SQLClass sqlclass = new SQLClass();
sqlclass.open();
bool LRresult = sqlclass.Databaseoperation(TSlr);
if (LRresult)
{
sqlclass.close();
Response.Write("<script language=javascript>alert('录入成功');location='BookLR.aspx'</script>");
}
else
{
sqlclass.close();
Response.Write("<script language=javascript>alert('录入失败');location='BookLR.aspx'</script>");
}这个 是 数据显示 页面代码
SQLClass sqlclass = new SQLClass();
sqlclass.open();
SqlDataAdapter adp = new SqlDataAdapter("select * from BookTable",sqlclass.SqlCON);
DataSet ds = new DataSet();
int counter = adp.Fill(ds,"BookTable");
Response.Write("共获取" + counter + "条数据" + "<a href='BookLR.aspx'>录入新图书</a>"+"<br/>");
foreach (DataRow mydr in ds.Tables["BookTable"].Rows)
{
Response.Write(mydr["ID"].ToString()+" "+mydr["BookNmae"].ToString()+" "+mydr["BookNUM"].ToString()+" "+mydr["BookPrice"].ToString()+"<a href='TSxiugai.aspx?shuming="+mydr["BookNmae"].ToString()+"&shuliang="+mydr["BookNUM"].ToString()+"&jiage="+mydr["BookPrice"].ToString()+"'>修改</a>"+"<br/>");
}修改 数据页面代码
public partial class TSxiugai : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TextBoxSM.Text=Request.QueryString["shuming"];
TextBoxSL.Text=Request.QueryString["shuliang"];
TextBoxJG.Text=Request.QueryString["jiage"];
} protected void Button1_Click(object sender, EventArgs e)
{
string Strxiugai = "update BookTable set BookNmae='"+TextBoxSM.Text.Trim()+"',BookNUM='"+Convert.ToInt32(TextBoxSL.Text.Trim())+"',BookPrice='"+Convert.ToInt32(TextBoxJG.Text.Trim())+"' where BookNmae='"+Request.QueryString["shuming"]+"' ";
SQLClass sqlclass = new SQLClass();
sqlclass.open();
bool XGresult = sqlclass.Databaseoperation(Strxiugai);
if (XGresult)
{
sqlclass.close();
Response.Write("<script language=javascript>alert('修改成功');location='BookList.aspx'</script>"); }
else
{
sqlclass.close();
Response.Write("<script language=javascrip>alert('修改失败');location='TSxiugai.aspx'</script>");
} }
}
编译 运行都可以 从 显示数据页面点击修改连接 跳到 修改页面 修改数据后跳到 显示页面
查了 显示页面和 数据库里面的数据 都没有变 秋杰
//这个问题每天都会在CSDN出现...
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TextBoxSM.Text=Request.QueryString["shuming"];
TextBoxSL.Text=Request.QueryString["shuliang"];
TextBoxJG.Text=Request.QueryString["jiage"];
}
}
此时 page_load事件被触发,导致对
TextBoxSM.Text=Request.QueryString["shuming"];
TextBoxSL.Text=Request.QueryString["shuliang"];
TextBoxJG.Text=Request.QueryString["jiage"];
三个Textbox又进行了一次付值操作,不论你之前修改了什么,都回到了修改前的值
所以你不可能修改到什么