叙述通过ADO.NET执行常规数据库 *** 作过程 �1�3

叙述通过ADO.NET执行常规数据库 *** 作过程 �1�3,第1张

ADO.NET连接数据库实验步骤(1):定义连接字符串数据库的连接定义一般是以字符串的形式出现,该字符串的定义参见图所示。 连接字符串举例图不管用拖放方式在图形界面增加的SqlConnection1对象,还是通过代码编写生成的SqlConnection1对象,都需要定义连接字符串。界面增加的SqlConnection1对象,其连接字符串在配置数据库连接后自动生成,而代码编写生成的SqlConnection1对象,其连接字符串需要安装格式要求手工编写。连接字符串决定了连接某台服务器,某个数据库,还有连接方式及要求。编程时定义连接字符串的具体例子,参见图:定义连接字符串具体举例图图中,SqlConnection1为sqlconnection对象,通过sqlconnection对象加载数据库连接字符串与MSSQL进行连接。在连接字符串中,“Server=SQLDB;”表示存储“pubs”数据库的服务器名称,该服务器名称为SQLDB,也可以写成IP地址,如123.101.220.1。如果是本地数据库服务器,可以有以下几种写法:“(local)”或者“.”“127.0.0.1”、本地机子名称。在连接字符串中,“User id=sapwd=password”表示登录数据库服务器用户名称和密码,使用这种用户身份登录方式必须是,用户同样可以使用“SQL Server身份认证”登录数据库服务器,否则连接仍然会失败。最后的“database=pubs”表示登录服务器是pubs数据库。ADO.NET连接数据库实验步骤(2):创建Connection对象在代码编辑器里面,通过编写代码的方式创建Connection对象的过程参见图所示: 创建连接对象图在对象创建过程中将连接字符串作为参数传递给SqlConnection类的构造函数,这样连接对象SqlConnection1就可以用来访问数据了。构造函数还有个重载,就是参数为空的构造函数。在实例化时不将连接字符串作为参数传给构造函数,实例化之后,给对象SqlConnection1的属性ConnectionString赋值,完成对象SqlConnection1的设置。例如:SqlConnection SqlConnection1 = new SqlConnection() SqlConnection1.ConnectionString = "Data Source=10.5.0.30Initial Catalog=TTDB User ID=TrainingDeveloper Pwd=Password"

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

这句是存储过程, 不相配。。。。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6641014.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-26
下一篇 2023-03-26

发表评论

登录后才能评论

评论列表(0条)

保存