如何使用ADO.NET轻松操纵数据库
- 提问者网友:疯孩纸
- 2021-02-12 07:04
- 五星知识达人网友:我住北渡口
- 2021-02-12 07:39
2
.NET Framework 数据提供程序的四个核心对象。
3
Connection有什么作用。Connection的作用是建立应用程序和数据库的连接,Connection有两个方法:Open()方法打开数据库,Close()关闭数据的连接;一个属性ConnectionString设置连接数据库字符串。它的作用可以用图形表示:
创建Connection对象的步骤。定义连接字符串,Sql Secer数据库的连接字符串格式一般为: Data Source=服务器名;Initial Catalog=数据库名; User ID=用户名;Pwd=密码
创建Command对象。
打开与数据库的连接。使用Open()方法。
关闭数据库连接。使用完数据库要记得即使关闭,使用Close()方法关闭,如果不关闭,会影响运行效率。关闭方式如下:
连接数据库整体示例。
测试数据库连接结果。
为什么使用异常处理。在连接数据库时常常出现连很多异常现象,例如下面的:
如何处理数据库连接异常。
为什么需要finally语句。使用finally语句可以执行我们结束的语句。
异常程序是如何执行的。可以看下执行过程:
- 1楼网友:荒野風
- 2021-02-12 08:01
不知道你有没有用三层,所以就写这样了。
public class student { private int studentid;
public int studentid { get { return studentid; } set { studentid = value; } } private string studentno;
public string studentno { get { return studentno; } set { studentno = value; } } private string studentname;
public string studentname { get { return studentname; } set { studentname = value; } } private string gender;
public string gender { get { return gender; } set { gender = value; } } private datetime birthday;
public datetime birthday { get { return birthday; } set { birthday = value; } } private int classid;
public int classid { get { return classid; } set { classid = value; } } private int status;
public int status { get { return status; } set { status = value; } } private string remark;
public string remark { get { return remark; } set { remark = value; } } }
下面的是获取界面输入的值:
namespace student
{ student stu = new student();
private void btncreate_click(object sender, eventargs e) { stu.studentname = this.txtname.text; stu.studentid = convert.toint32(this.txtstudentid.text); stu.studentno = this.txtstudentno.text; stu.gender = this.cobsex.text; stu.birthday = convert.todatetime(this.datetimepicker1.text); stu.classid = convert.toint32(this.cobclass.selectedvalue.tostring()); stu.remark = this.txtremark.text;
int count = stuinsert(stu); if (count > 0) { messagebox.show("添加成功!"); } else { messagebox.show("添加失败!"); } }
//插入数据库的方法(可以用存储过程)
public int stuinsert(student stu) { con = new sqlconnection("server=www-9e99f2ab2ea\\myserver2005;database=school;uid=sa;pwd=pengxue"); con.open(); comm = new sqlcommand("insert into student(studentid,studentno,studentname,gender,birthday,classid,status,remark) values(@studentid,@studentno,@studentname,@gender,@birthday,@classid,@status,@remark)", con); comm.parameters.add("@studentid", sqldbtype.int).value=stu.studentid; comm.parameters.add("@studentno",sqldbtype.varchar).value=stu.studentno; comm.parameters.add("@studentname", sqldbtype.varchar).value=stu.studentname; comm.parameters.add("@gender", sqldbtype.varchar).value=stu.gender; comm.parameters.add("@birthday", sqldbtype.datetime).value=stu.birthday; comm.parameters.add("@classid", sqldbtype.int).value=stu.classid; comm.parameters.add("@status",sqldbtype.int).value=1; comm.parameters.add("@remark", sqldbtype.varchar).value=stu.remark; int i = comm.executenonquery(); con.close(); return i; }
}