日期:2014-05-17 浏览次数:20445 次
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/>"); }