这次我使用ADO.NET来插入一条数据,到数据库中。主用到存储过程。我不想每次都是用SQL文本的形式了,那样始终没有进步~~~
下面首先,我把我这次练习要用到的数据库脚本,贴出来:
1USE master --使用系统数据库2GO3IF EXISTS(SELECT * FROM sysdatabases WHERE name=N'DB_MyStudentLife')4DROP DATABASE [DB_MyStudentLife]; --如果要创建的数据库存在的话,就删除5GO6CREATE DATABASE [DB_MyStudentLife] --创建数据库7GO8USE [DB_MyStudentLife] --使用数据库9GO10IF EXISTS(SELECT * FROM sysobjects WHERE name=N'MyClass')11DROP TABLE [MyClass] --如果要创建的数据表存在的话,就删除(注意sysobjects,一定要全部是小写的,不然有错误,不能写成大写的。)12GO13CREATE TABLE MyClass --创建数据表14(15C_ID INT NOT NULLPRIMARY KEY, --班级编号16C_Name NVARCHAR(200) notnull, --班级名称17C_Descr nvarchar(max) notnull--班级简介1819);20GO21IF EXISTS(SELECT * FROM sysobjects WHERE name=N'MyStudent')22DROP TABLE MyStudent23GO24CREATE TABLE MyStudent25(26S_IDintnotnullprimary key, --学号27S_Name nvarchar(50) notnull, --姓名28S_Genderchar(2) notnull, --性别29S_Address nvarchar(max) notnull, --地址30S_Phone nvarchar(50)notnull, --电话31S_Ageintnotnull, --年龄32S_Birthday datetime notnull, --生日33S_CardIDintnotnull, --身份证号码34S_CIDintnotnullreferences MyClass(C_ID) --班级编号3536);
创建数据库,创建数据表语句
接着大家选中刚才执行脚本,创建好的数据库,然后使用我下面的数据,向数据库表里面添加数据吧
1insert into MyClass(C_ID,C_Name,C_Descr)values(1,'软件1108班','武汉软件工程职业学院');2insert into MyClass(C_ID,C_Name,C_Descr)values(2,'软件1107班','武汉软件工程职业学院');3insert into MyClass(C_ID,C_Name,C_Descr)values(3,'实验班','武汉软件工程职业学院');456insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('1','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);7insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('2','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);8insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('3','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);9insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('4','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);10insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('5','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);11insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('6','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);12insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('7','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);13insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('8','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);14insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('9','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);15insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('10','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);16insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('11','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);17insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('12','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);18insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('13','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);19insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('14','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);20insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('15','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);21insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('16','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);22insert into MyStudent(S_ID,S_Name,S_Gender,S_Age,S_Birthday,S_CardID,S_Phone,S_Address,S_CID)values('17','李四','男','22','1992-09-26','111111111','11232131234',N'深圳宝安石岩',1);
插入数据到数据库表中
说明一下,等会我要向MyClass表中插入数据,现在为这个表创建一个插入的存储过程:
1IF OBJECT_ID('Ins_ClasseD','P') IS NOT NULL2DROP PROCEDURE Ins_ClasseD3GO4CREATE PROCEDURE Ins_ClasseD5@C_IDint,6@C_Name nvarchar(200) ,7@C_Descr nvarchar(max)8AS9INSERT INTO dbo.MyClass10( C_ID, C_Name, C_Descr )11VALUES ( @C_ID, -- C_ID -int12@C_Name, -- C_Name - nvarchar(200)13@C_Descr -- C_Descr -nvarchar(max)14);1516GO
给MyClass表存储过程
下面开始程序实现:
我是复习,ADO.NET,现在就随便建了一个控制台的应用程序,来开始我的测试:
注意;在下面的例子中,为了尽可能简单易于理解,我没有把连接字符串的那部分代码,放到配置文件中。
如果要放的话,要用到System.Configuration命名空间,还有一个ConfigurationManager类..具体的细节就不说了。
请看具体实现代码:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Data;usingSystem.Data.SqlClient;namespaceADO.NET插入一条数据到数据库中
{classProgram
{//连接字符串privatestaticstringsqlCon ="server=.;database=DB_MyStudentLife;uid=sa;pwd=PassWord_1";staticvoidMain(string[] args)
{//1创建连接对象(连接字符串)SqlConnection scon =newSqlConnection(sqlCon);//2创建命令对象(为命令对象设置属性)SqlCommand scmd =newSqlCommand();
scmd.CommandText="Ins_ClasseD";
scmd.CommandType= CommandType.StoredProcedure;//这里我使用存储过程来插入数据scmd.Connection =scon;//3打开数据库连接scon.Open();//设置参数scmd.Parameters.Add(newSqlParameter("@C_ID",6));
scmd.Parameters.Add(newSqlParameter("@C_Name","测试班"));
scmd.Parameters.Add(newSqlParameter("@C_Descr","软件测试技术"));//4发送命令intresult=scmd.ExecuteNonQuery();//5处理数据if(result >0)
{
Console.WriteLine("插入数据成功");
}else{
Console.WriteLine("插入数据失败");
}
//6最后一步,差点忘记了,一定要关闭连接
scon.Close();
Console.ReadKey();
}
}
}
程序执行玩之后的效果图: