在ASP.Net窗口中,進行數據庫的查詢,添加,刪除,修改操作
在網頁中使用GridView進行數據庫表單的顯示,以及基本的數據庫操作,SqlCommand和SqlDataAdapter
查詢功能,插入數據功能:(在Textbox中輸入內容,點擊按鈕執行查詢插入功能)
protected void Button1_Click(object sender, EventArgs e) //查詢功能 { if (this.TextBox1.Text != "") //判斷輸入框是否爲空 { //與數據庫進行連接 SqlConnection myConn = new SqlConnection("Data Source=WIN8;Initial Catalog=SqlDataTest01;Persist Security Info=True;User ID=sa;Password=123456"); myConn.Open(); //查詢操作 string sqlStr = "select * from UserTable where UserName=@UserName"; SqlCommand myCmd = new SqlCommand(sqlStr, myConn); myCmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = this.TextBox1.Text.Trim(); SqlDataAdapter myDa = new SqlDataAdapter(myCmd); //將查詢的結果添加到DataSet中 DataSet myDs = new DataSet(); myDa.Fill(myDs); //在GridView中顯示查詢到的結果 if (myDs.Tables[0].Rows.Count > 0) { GridView1.DataSource = myDs; GridView1.DataBind(); } else { Response.Write("<script>alert('查詢結果爲空')</script>"); } myDa.Dispose(); myDs.Dispose(); myConn.Close(); } else { Response.Write("<script>alert('請輸入查詢的用戶名')</script>"); } }
protected void Button2_Click(object sender, EventArgs e)
{
if (this.TextBox2.Text != ""||this.TextBox3.Text!="")
{
SqlConnection myConn = new SqlConnection("Data Source=WIN8;Initial Catalog=SqlDataTest01;Persist Security Info=True;User ID=sa;Password=123456");
myConn.Open();
string sqlStr = "insert into UserTable values ('" + this.TextBoxID.Text.Trim() + "','" + this.TextBox2.Text.Trim() + "','" + this.TextBox3.Text.Trim() + "')";
SqlCommand myCmd = new SqlCommand(sqlStr,myConn);
myCmd.ExecuteNonQuery();
myConn.Close();
this.bind();
}
else
{
Response.Write("<script>alert('請輸入用戶名密碼')</script>");
}
}
刪除功能,更新:(在GridView屬性事件中進行數據刪除操作)
protected void GridView2_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// string userN =GridView2.DataKeys[e.RowIndex].Value.ToString();
//獲取刪除字段所在行的關鍵字段的值
int userid = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value);
string sqlStr = "delete from UserTable where ID= "+userid;
SqlConnection myConn = new SqlConnection("Data Source=WIN8;Initial Catalog=SqlDataTest01;Persist Security Info=True;User ID=sa;Password=123456");
myConn.Open();
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
GridView2.EditIndex = -1;
this.bind();
}
protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView2.EditIndex = e.NewEditIndex;
this.bind();
}
protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//更新操作時會對頁面進行刷新
int userid = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value);
string UName = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
string PWord = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString();
string sqlStr = "update UserTable set UserName='"+UName+ "',PassWord='" +PWord+ "' where ID =" + userid;
SqlConnection myConn = new SqlConnection("Data Source=WIN8;Initial Catalog=SqlDataTest01;Persist Security Info=True;User ID=sa;Password=123456");
myConn.Open();
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
GridView2.EditIndex = -1;
this.bind();
}
protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView2.EditIndex = -1;
this.bind();
}
protected void bind()
{
SqlConnection myConn = new SqlConnection("Data Source=WIN8;Initial Catalog=SqlDataTest01;Persist Security Info=True;User ID=sa;Password=123456");
myConn.Open();
string sqlStr = "select * from UserTable";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView2.DataSource = myDs;
GridView2.DataKeyNames = new string[] { "ID" };
GridView2.DataBind();
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
結果截圖:
在函數編輯過程中屬性事件的選擇:
在查詢操作中:
myCmd.Parameters.Add("@UserName", SqlDbType.VarChar, 50).Value = this.TextBox1.Text.Trim();
這一句實現將輸入框中的值傳遞到數據庫中的UserName參數中;
其中Parameters.Add()是 將 System.Data.SqlClient.SqlParameter 及其參數名、數據類型和列寬添加到System.Data.SqlClient.SqlParameterCollection;
Trim()方法是 從當前 System.String 對象移除所有前導空白字符和尾部空白字符。
在刪除數據操作中:
int userid = Convert.ToInt32(GridView2.DataKeys[e.RowIndex].Value);
定義userid是獲取該刪除行所在的主鍵值,其中【GridViewDeleteEventArgs 】e.RowIndex的值就是該事件對象所在行的索引值,DataKeys.Valuequ了該行的主鍵值
在更新操作中:
string UName = ((TextBox)(GridView2.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString();
定義UName是將單元格轉換成輸入框再獲取其中的值,獲取索引中第2格單元格中的索引值爲0的控件,官方對Controls的解釋爲獲取表示一個指定的服務器控件的子控件在 UI 層次結構的 System.Web.UI.ControlCollection 對象。返回 子控件的集合指定的服務器控件的。(不明白Controls[0]在這裏所表示的含義,[0]表示的是控件)
(過程中遇到在數據框彙總輸入完後,點擊更新後併爲進行數據的更新,【解決方法】由於頁面在點擊操作是會對頁面進行刷新,沒有獲取到新的輸入值,需要在Page_Load函數中,增加判斷語句 if(!IsPostBack))
其中 IsPostBack判斷該頁是第一次呈現還是爲了響應回發而加載,如果是爲響應客戶端回發而加載該頁則爲true
在插入操作中:
myCmd.ExecuteNonQuery();
ExcuteNonQuery()是對連接執行 Transact-SQL 語句並返回受影響的行數
以上就是使用ADO.NET進行數據的簡單的增刪查改操作