ADO.NET连接数据库实验步骤(3):打开与数据库的连接用Connection对象的Open()方法就可以打开数据库连接。
DAL层:using System
using System.Data
using System.Configuration
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
using System.Data.SqlClient
/// <summary>
/// DALStudent 的摘要说明
/// </summary>
public class DALStudent
{
DBConnection db = new DBConnection()
public DALStudent()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public int AddStudent(Student student)
{
//执行插入的sql语句
string sql = string.Format("insert into student values('{0}',{1},'{2}')",student.StudentName, student.StudentAge, student.StudentAddress)
SqlConnection conn = db.getConn()
try
{
SqlCommand cmd = new SqlCommand(sql, conn)//调用command对象,并传入sql和conn
conn.Open()//打开数据库
int x = cmd.ExecuteNonQuery()//执行ExecuteNonQuery方法,添加到数据库
return x//返回x
}
catch
{
return -5
}
finally
{
conn.Close()//关闭数据库
}
}
public int ModStudent(Student student)
{
string sql = string.Format("update student set StudentName={0},StudentAge={1},StudentAddress={2} where StudentId={3}", student.StudentName, student.StudentAge, student.StudentAddress,student.StudentId)
SqlConnection conn = db.getConn()
try
{
SqlCommand cmd = new SqlCommand(sql, conn)
conn.Open()
int x = cmd.ExecuteNonQuery()
return x
}
catch
{
return -5
}
finally
{
conn.Close()
}
}
public int delStudent(Student student)
{
string sql = string.Format("delete from student where StudentId={0}",student.StudentId)
SqlConnection conn = db.getConn()
try
{
SqlCommand cmd = new SqlCommand(sql, conn)
conn.Open()
int f = cmd.ExecuteNonQuery()
return f
}
catch
{
return -3
}
finally
{
conn.Close()
}
}
}
BLL层:
using System
using System.Data
using System.Configuration
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
/// <summary>
/// BLLStudent 的摘要说明
/// </summary>
public class BLLStudent
{
public BLLStudent()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public int AddStudent(Student student)
{
if (student.StudentName == "")
{
return -1
}
if (student.StudentAge == 0)
{
return -2
}
return new DALStudent().AddStudent(student)
}
public int ModStudent(Student student)
{
if (student.StudentName == "")
{
return -1
}
if (student.StudentAge == 0)
{
return -2
}
return new DALStudent().ModStudent(student)
}
}
表示层cs文件:
using System
using System.Data
using System.Configuration
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
public partial class _Default : System.Web.UI.Page
{
BLLStudent bll = new BLLStudent()
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnAdd_Click(object sender, EventArgs e)
{
string studentName = this.txtName.Text//获取this.txtName文本框的值等于studentName
string studentAge = this.txtAge.Text
int age = 0
if (studentAge != "")
{
age = Convert.ToInt32(studentAge)//如果studentAge不为空,就将其赋给age
}
string studentAddress = this.txtAddress.Text
Student student = new Student()
student.StudentName = studentName//将获取的值赋给属性
student.StudentAge = age
student.StudentAddress = studentAddress
int result = bll.AddStudent(student)//这句话是什么意思?
/*判断*/
if (result == -1)
{
Response.Write("请输入姓名")
}
else if(result == -2)
{
Response.Write("请输入年龄")
}
else if (result == -5)
{
Response.Write("数据库访问有问题")
}
else if (result == 1)
{
Response.Write("OK")
}
}
}
表示层界面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ADDStudent.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
序号<asp:TextBox ID="txtId" runat="server"></asp:TextBox><br />
姓名<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
年龄<asp:TextBox ID="txtAge" runat="server"></asp:TextBox>
<br />
地址<asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
<br />
<asp:Button ID="btnAdd" runat="server" OnClick="btnAdd_Click" Text=" 添加 " /></div>
</form>
</body>
</html>
Model层:
using System
using System.Data
using System.Configuration
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
/// <summary>
/// Student 的摘要说明
/// </summary>
public class Student
{
public Student()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
private int _studentId
public int StudentId
{
get { return _studentId}
set { _studentId = value}
}
private string _studentName
public string StudentName
{
get { return _studentName}
set { _studentName = value}
}
private int _studentAge
public int StudentAge
{
get { return _studentAge}
set { _studentAge = value}
}
private string _studentAddress
public string StudentAddress
{
get { return _studentAddress}
set { _studentAddress = value}
}
}
错误在 //3因为 oleCmd.CommandText="select * from authors"这是查询语句,
而oleCmd.commandType=CommandType.StoredProcedure//3
这句是存储过程, 不相配。。。。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)