在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("");
}
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
else
{
Response.Write("");
}
}
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("");
}
}
删除功能,更新:(在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进行数据的简单的增删查改操作