protected void BtnExecuteDisc_Click(object sender, EventArgs e) { string username = Username.Text.Trim(), password = Password.Text.Trim(); string query = "select * from [user_info] where [username] = @username and [password] = @password"; SqlParameter[] cmdParams = new SqlParameter[] { new SqlParameter("@username", username), new SqlParameter("@password", password) }; try { Connector conn = DbHelper.GetConnector("TestDB"); // DataTable data = conn.GetDataTable("select * from [user_info]"); // DataTable data = conn.GetDataTable(query, cmdParams) // 支持带安全参数 // DataTable data = conn.GetDataTable("GetUserInfo", CommandType.StoredProcedure, cmdParams); // 支持存储过程 DataSet data = conn.GetDataSet("GetUserInfo", CommandType.StoredProcedure, cmdParams); // 支持存储过程 ViewData.DataSource = data; ViewData.DataBind(); } catch (Exception ex) { display.InnerText = ex.Message; } }
protected void BtnExecuteBySp_Click(object sender, EventArgs e) { string username = Username.Text.Trim(), password = Password.Text.Trim(); SqlParameter[] cmdParams = new SqlParameter[] { new SqlParameter("@username", username), new SqlParameter("@password", password) }; try { Connector conn = DbHelper.GetConnector("TestDB"); // 支持存储过程的执行 SqlDataReader reader = (SqlDataReader)conn.Execute("reader", "GetUserInfo", CommandType.StoredProcedure, cmdParams); if (reader.Depth != 0) { ViewData.DataSource = reader; ViewData.DataBind(); } else { display.InnerText = "NULL"; } } catch (Exception ex) { display.InnerText = ex.Message; } }
protected void Like_Click(object sender, EventArgs e) { //set up object to use the web.config file string connectionString = WebConfigurationManager.ConnectionStrings["photocatconnection"].ConnectionString; //set up connection object called 'myConnection' SqlConnection myConnection = new SqlConnection(connectionString); //open database communication myConnection.Open(); //intialising values with URL int row = int.Parse(Request.QueryString["photoID"]); int beforelikes = int.Parse(Request.QueryString["Likes"]); int likes; //Increment likes value likes = beforelikes; likes += 1; Like.Text = " ♥ "; int likeupdate; likeupdate = likes; //Disable button after use Like.Enabled = false; string query = "UPDATE Photos SET Likes = @newlikes WHERE photoID=@id"; SqlCommand myCommand = new SqlCommand(query, myConnection); myCommand.Parameters.AddWithValue("@newlikes", likeupdate); myCommand.Parameters.AddWithValue("@id", row); myCommand.ExecuteNonQuery(); ViewPhotos.DataBind(); ViewData.DataBind(); myConnection.Close(); }
protected void BtnExecute_Click(object sender, EventArgs e) { string username = Username.Text.Trim(), password = Password.Text.Trim(); string query = "select * from [user_info] where [username] = @username and [password] = @password"; //string insert = String.Format("insert into [user_info] values('{0}', '{1}')", username, password); string insert = "insert into [user_info] values(@username, @password)"; SqlParameter[] cmdParams = new SqlParameter[] { new SqlParameter("@username", username), new SqlParameter("@password", password) }; try { Connector conn = DbHelper.GetConnector("TestDB"); if (conn.Execute("scalar", query, cmdParams) == null) { int line = (int)conn.Execute("non", insert, cmdParams); display.InnerText = line.ToString(); } else { display.InnerText = "已注册!"; } SqlDataReader reader = (SqlDataReader)conn.Execute("reader", query, cmdParams); ViewData.DataSource = reader; ViewData.DataBind(); } catch (Exception ex) { display.InnerText = ex.Message; } }
protected void DeleteMajor_Click(object sender, EventArgs e) { int[] arr = { 0 }; List <int> list = new List <int>(); for (int i = 0; i < ViewData.Rows.Count; i++) { CheckBox ckbox = (CheckBox)ViewData.Rows[i].FindControl("ItemCkBox"); // 每行的确认控件 if (ckbox.Checked) { // 法一: // ViewData.DeleteRow(i); int majorId = int.Parse(ViewData.Rows[i].Cells[1].Text.Trim()); // 被删课程ID list.Add(majorId); } } foreach (var id in list) { // 法二: Connector conn = ConnectorFactory.GetConnector("studentdb"); // 连接模式 //conn.Connect(); //int row = conn.ManageData<int>(0, "delete from [Major] where [MajorId] = " + id); //ViewData.DataBind(); //conn.CloseAll(); // 断开模式 conn.ManageDataOffMode("delete", "Major", id); ViewData.DataBind(); } }