被过滤广告
一 数据库连接方式
常用数据库为access,sqlserver,针对不同的数据库有不同的连接字符串,至于oracle,不同版本有不同的要求
access:
using System.Data.OleDb;
连接字符串:Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\\Northwind.mdb
\\Data Source=access数据库路径
sqlserver:
using System.Data.SqlClient;
连接字符串:Data Source=.;Initial Catalog=test;Persist Security Info=True;User ID=sa;Password=123
\\Data Source=服务器地址,如果是本机,可以用"."来代替;Initial Catalog=数据库名;User ID=用户名;Password=密码
oracle:
using System.Data.OracleClient; \\该引用需要专门添加,在winform下,点击项目-添加引用-System.Data.OracleClient;在网站下,点击网站-添加引用-System.Data.OracleClient
连接字符串:Data Source=.;user=admin;password=123;";
二 使用Command对象操作数据库中记录
页面源代码中添加一个GridView控件(GridView1),一个Button按钮(Button1),一个TextBox控件(txtName)
cs代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public SqlConnection GetConnection()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//读取web.config中的连接字符串,创建SqlConnection连接
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataBind();
//释放并关闭连接
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//如果输入姓名不为空
if (this.txtName.Text != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test where Name=@Name";
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
//添加一个Parameters
myCmd.Parameters.Add("@Name", SqlDbType.VarChar, 20).Value = this.txtName.Text.Trim();
SqlDataAdapter myDa = new SqlDataAdapter(myCmd);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
if (myDs.Tables[0].Rows.Count > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
GridView1.DataSource = myDs;
GridView1.DataBind();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("<script>alert('没有相关记录')</script>");
}
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.bind();
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
页面源代码中添加一个GridView控件(GridView1),一个Button按钮(Button1),一个TextBox控件(txtName),如果有多个信息要录入,增加TextBox控件,并修改sql语句即可。
注意:"test"表中有一个主键默认自动增加字段,该字段在添加时不需要追加数据,数据库会自动添加数据。
cs代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public SqlConnection GetConnection()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//读取web.config中的连接字符串,创建SqlConnection连接
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
GridView1.DataBind();
//释放并关闭连接
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//如果输入姓名不为空
if (this.txtName.Text != "")
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "insert into test(Name) value ('"
+ this.txtName.Text.Trim() + "')";
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//对连接执行语句,并返回影响的行数
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
this.bind();
}
else
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.bind();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
- 通过GridView1中的编辑按钮编辑数据库中数据
页面源代码中添加一个GridView控件(GridView1),设置AutoGenerateEditButton="True"。并设置GridView控件的RowEditing,RowUpdating,RowCancelingEdit事件。
cs代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public SqlConnection GetConnection()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//读取web.config中的连接字符串,创建SqlConnection连接
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
//设置绑定数据表的主键值为"ID"字段
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
GridView1.DataKeyNames = new string[]
{ "ID" };
GridView1.DataBind();
//释放并关闭连接
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//如果点击编辑按钮,则获取要编辑的行索引
GridView1.EditIndex = e.NewEditIndex;
this.bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//提交编辑时,先取得要编辑行的主键值
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
//获取同行中第2列目前数据值
string Name = ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.Trim();
string sqlStr = "update test set Name='" + Name + "' where ID=" + ID;
SqlConnection myConn = GetConnection();
myConn.Open();
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
//编辑完毕后,将编辑行索引=-1
GridView1.EditIndex = -1;
this.bind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//退出编辑后,将编辑行索引=-1
GridView1.EditIndex = -1;
this.bind();
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
- 通过GridView1中的删除按钮删除数据库中数据
页面源代码中添加一个GridView控件(GridView1),设置AutoGenerateDeleteButton="True"。并设置GridView控件的RowDeleting,RowDataBound事件。
cs代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public SqlConnection GetConnection()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//读取web.config中的连接字符串,创建SqlConnection连接
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
//设置绑定数据表的主键值为"ID"字段
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
GridView1.DataKeyNames = new string[]
{ "ID" };
GridView1.DataBind();
//释放并关闭连接
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//提交编辑时,先取得要编辑行的主键值
int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
string sqlStr = "delete from test where ID="+ID;
SqlConnection myConn = GetConnection();
myConn.Open();
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
myCmd.ExecuteNonQuery();
myCmd.Dispose();
myConn.Close();
//编辑完毕后,将编辑行索引=-1
GridView1.EditIndex = -1;
this.bind();
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//点击删除按钮后弹出对话框
if (e.Row.RowType == DataControlRowType.DataRow)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "return confirm('确定要删除吗?')");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
页面源代码中添加一个GridView控件(GridView1),一个Button按钮(Button1),一个TextBox控件(txtName)。
cs代码:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code
public SqlConnection GetConnection()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//读取web.config中的连接字符串,创建SqlConnection连接
string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
SqlConnection myConn = new SqlConnection(myStr);
return myConn;
}
protected void bind()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
//此处为GridView1绑定数据库
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "select * from test";
SqlDataAdapter myDa = new SqlDataAdapter(sqlStr, myConn);
DataSet myDs = new DataSet();
myDa.Fill(myDs);
GridView1.DataSource = myDs;
//设置绑定数据表的主键值为"ID"字段
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
GridView1.DataKeyNames = new string[]
{ "ID" };
GridView1.DataBind();
//释放并关闭连接
myDa.Dispose();
myDs.Dispose();
myConn.Close();
}
protected void Button1_Click(object sender, EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
SqlConnection myConn = GetConnection();
myConn.Open();
string sqlStr = "insert into test(Name) value ('"
+ this.txtName.Text.Trim() + "')";
//此处添加一个sql事务,并开始数据库事务。
//警告: 如果启动了某个事务,并且服务器上出现了 16 级或更高级别的错误,则在调用 Read 方法以前,该事务不会回滚。ExecuteReader 上不会引发异常。
SqlTransaction sqlTrans=myConn.BeginTransaction();
SqlCommand myCmd = new SqlCommand(sqlStr, myConn);
//设置在myCmd中执行事务。
myCmd.Transaction=sqlTrans;
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
myCmd.ExecuteNonQuery();
//提交事务,事务一旦提交就不能回滚。也就是说这里表示正确执行,不再需要事务回滚。
sqlTrans.Commit();
myCmd.Clone();
this.bind();
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Response.Write("<script>alert('插入失败,执行事务回滚')</script>");
//事务回滚。
sqlTrans.Rollback();
}
}