如何使用ADO.NET向数据库中插入数据
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用ADO.NET向数据库中插入数据相关的知识,希望对你有一定的参考价值。
。public class Studentprivate 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;
参考技术A insert into 表名values字段值例如: nInsert into student(student_id,student_name,department) values(“070011”,”张三”,”公共关系”); 参考技术B asp.net 三层 添加数据就加我QQ吧。 有现成的 参考技术C 不知道你有没有用三层,所以就写这样了。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;
以上是关于如何使用ADO.NET向数据库中插入数据的主要内容,如果未能解决你的问题,请参考以下文章
使用 ADO.Net 数据集将数据插入 SQl Server 数据库
如何在 ASP.NET Core MVC 中使用 ADO.NET 向存储过程添加参数?
SSIS Passing Parameters to an ADO .NET Source query;向ado.net数据源传递参数。