判断连接数据库状态: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ Import Namespace= "System.Data.SqlClient" %> <mce:script runat = "server"><!-- protected void Page_Load(object sender, EventArgs e) { var cn = new SqlConnection(); cn.ConnectionString = @"Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|product_Data.MDF;Integrated Security=True;User Instance= True"; cn.Open(); if (cn.State == System.Data.ConnectionState.Open) { Response.Write("SQL连接开启"); cn.Close(); } if (cn.State == System.Data.ConnectionState.Closed) { Response.Write("SQL连接关闭"); } } // --></mce:script> 执行插入Sql语句,代码如下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ Import Namespace= "System.Data.SqlClient" %> <mce:script runat = "server"><!-- protected void Page_Load(object sender, EventArgs e) { var cn = new SqlConnection(); cn.ConnectionString = @"Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|product_Data.MDF;Integrated Security=True;User Instance= True"; var cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "INSERT Produce(ProductID,ProductName,Price) VALUES(155,'apple',125)"; cn.Open(); var records = cmd.ExecuteNonQuery(); cn.Close(); Response.Write("新增了"+records.ToString()+"条数据"); } // --></mce:script> 更新数据库语句: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ Import Namespace= "System.Data.SqlClient" %> <mce:script runat = "server"><!-- protected void Page_Load(object sender, EventArgs e) { var cn = new SqlConnection(); cn.ConnectionString = @"Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|product_Data.MDF;Integrated Security=True;User Instance= True"; var cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "UPDATE Produce SET ProductName ='oriange' where ProductID = 255"; cn.Open(); var records = cmd.ExecuteNonQuery(); cn.Close(); Response.Write("更新"+records.ToString()+"条数据"); } // --></mce:script> 删除数据库记录 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ Import Namespace= "System.Data.SqlClient" %> <mce:script runat = "server"><!-- protected void Page_Load(object sender, EventArgs e) { var cn = new SqlConnection(); cn.ConnectionString = @"Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|product_Data.MDF;Integrated Security=True;User Instance= True"; var cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "DELETE Produce where ProductID = 255"; cn.Open(); var records = cmd.ExecuteNonQuery(); cn.Close(); Response.Write("更新"+records.ToString()+"条数据"); } // --></mce:script> 异常的捕捉及处理 <%@ Import Namespace= "System.Data.SqlClient" %> <mce:script runat = "server"><!-- protected void Page_Load(object sender, EventArgs e) { var cn = new SqlConnection(); cn.ConnectionString = @"Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|product_Data.MDF;Integrated Security=True;User Instance= True"; try { cn.Open(); Response.Write("sql连接打开"); } catch (Exception ex) { Response.Write(ex.Message); } finally { if (cn.State == System.Data.ConnectionState.Open) { cn.Close(); Response.Write("SQL连接关闭。<p/>"); } } } // --></mce:script> 捕捉sql执行异常 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <%@ Import Namespace = "System.Data.SqlClient" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { var cn = new SqlConnection(); cn.ConnectionString=@"Data Source=./SQLEXPRESS;AttachDbFilename=|DataDirectory|Student.Mdf;Integrated Security=True;User Instance=True"; var cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandText = "insert Student (StudentID,StudentName) values (8,'wss')"; try { cn.Open(); /*if (cn.State == System.Data.ConnectionState.Open) { Response.Write("SQL is open Now"); cn.Close(); } if (cn.State == System.Data.ConnectionState.Closed) { Response.Write("SQL is close"); } Response.Write("SQL连接开启"); cn.Close(); Response.Write("SQL连接关闭");*/ var records = cmd.ExecuteNonQuery(); cn.Close(); Response.Write("新增了" + records.ToString() + "条数据"); } catch (SqlException ex) { Response.Write(ex.Number + ":" + ex.Message + " <br/>"); } finally { if (cn.State == System.Data.ConnectionState.Open) { cn.Close(); } } }