数据库修改代码
- C# code
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; } }}
录入数据 页 提交按钮面代码
- C# code
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>"); }
这个 是 数据显示 页面代码
- C# code
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/>"); }
修改 数据页面代码
- C# code
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>"); } } }