淺談ADO.NET數(shù)據(jù)庫(kù)腳本
這次我使用ADO.NET來(lái)插入一條數(shù)據(jù),到數(shù)據(jù)庫(kù)中。主用到存儲(chǔ)過(guò)程。我不想每次都是用SQL文本的形式了,那樣始終沒有進(jìn)步~~~
下面首先,我把我這次練習(xí)要用到的數(shù)據(jù)庫(kù)腳本,貼出來(lái):
USE master --使用系統(tǒng)數(shù)據(jù)庫(kù) GO IF EXISTS(SELECT * FROM sysdatabases WHERE name=N'DB_MyStudentLife') DROP DATABASE [DB_MyStudentLife]; --如果要?jiǎng)?chuàng)建的數(shù)據(jù)庫(kù)存在的話,就刪除 GO CREATE DATABASE [DB_MyStudentLife] --創(chuàng)建數(shù)據(jù)庫(kù) GO USE [DB_MyStudentLife] --使用數(shù)據(jù)庫(kù) GO IF EXISTS(SELECT * FROM sysobjects WHERE name=N'MyClass') DROP TABLE [MyClass] --如果要?jiǎng)?chuàng)建的數(shù)據(jù)表存在的話,就刪除(注意sysobjects,一定要全部是小寫的,不然有錯(cuò)誤,不能寫成大寫的。) GO CREATE TABLE MyClass --創(chuàng)建數(shù)據(jù)表 ( C_ID INT NOT NULL PRIMARY KEY, --班級(jí)編號(hào) C_Name NVARCHAR(200) not null, --班級(jí)名稱 C_Descr nvarchar(max) not null --班級(jí)簡(jiǎn)介 ); GO IF EXISTS(SELECT * FROM sysobjects WHERE name=N'MyStudent') DROP TABLE MyStudent GO CREATE TABLE MyStudent ( S_ID int not null primary key, --學(xué)號(hào) S_Name nvarchar(50) not null, --姓名 S_Gender char(2) not null, --性別 S_Address nvarchar(max) not null , --地址 S_Phone nvarchar(50)not null, --電話 S_Age int not null, --年齡 S_Birthday datetime not null, --生日 S_CardID int not null, --身份證號(hào)碼 S_CID int not null references MyClass(C_ID) --班級(jí)編號(hào) );
接著大家選中剛才執(zhí)行腳本,創(chuàng)建好的數(shù)據(jù)庫(kù),然后使用我下面的數(shù)據(jù),向數(shù)據(jù)庫(kù)表里面添加數(shù)據(jù)吧
insert into MyClass(C_ID,C_Name,C_Descr)values(1,'軟件1108班','武漢軟件工程職業(yè)學(xué)院');
insert into MyClass(C_ID,C_Name,C_Descr)values(2,'軟件1107班','武漢軟件工程職業(yè)學(xué)院');
insert into MyClass(C_ID,C_Name,C_Descr)values(3,'實(shí)驗(yàn)班','武漢軟件工程職業(yè)學(xué)院');
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
insert 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);
說(shuō)明一下,等會(huì)我要向MyClass表中插入數(shù)據(jù),現(xiàn)在為這個(gè)表創(chuàng)建一個(gè)插入的存儲(chǔ)過(guò)程:
IF OBJECT_ID('Ins_ClasseD','P') IS NOT NULL
DROP PROCEDURE Ins_ClasseD
GO
CREATE PROCEDURE Ins_ClasseD
@C_ID int ,
@C_Name nvarchar(200) ,
@C_Descr nvarchar(max)
AS
INSERT INTO dbo.MyClass
( C_ID, C_Name, C_Descr )
VALUES ( @C_ID, -- C_ID - int
@C_Name, -- C_Name - nvarchar(200)
@C_Descr -- C_Descr - nvarchar(max)
);
GO
下面開始程序?qū)崿F(xiàn):
我是復(fù)習(xí),ADO.NET,現(xiàn)在就隨便建了一個(gè)控制臺(tái)的應(yīng)用程序,來(lái)開始我的測(cè)試:
注意;在下面的例子中,為了盡可能簡(jiǎn)單易于理解,我沒有把連接字符串的那部分代碼,放到配置文件中。
如果要放的話,要用到System.Configuration命名空間,還有一個(gè)ConfigurationManager類..具體的細(xì)節(jié)就不說(shuō)了。
請(qǐng)看具體實(shí)現(xiàn)代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
namespace ADO.NET插入一條數(shù)據(jù)到數(shù)據(jù)庫(kù)中
{
class Program
{
//連接字符串
private static string sqlCon = "server=.;database=DB_MyStudentLife;uid=sa;pwd=Password_1";
static void Main(string[] args)
{
//1創(chuàng)建連接對(duì)象(連接字符串)
SqlConnection scon = new SqlConnection(sqlCon);
//2創(chuàng)建命令對(duì)象(為命令對(duì)象設(shè)置屬性)
SqlCommand scmd = new SqlCommand();
scmd.CommandText = "Ins_ClasseD";
scmd.CommandType = CommandType.StoredProcedure; //這里我使用存儲(chǔ)過(guò)程來(lái)插入數(shù)據(jù)
scmd.Connection = scon;
//3打開數(shù)據(jù)庫(kù)連接
scon.Open();
//設(shè)置參數(shù)
scmd.Parameters.Add(new SqlParameter("@C_ID",6));
scmd.Parameters.Add(new SqlParameter("@C_Name", "測(cè)試班"));
scmd.Parameters.Add(new SqlParameter("@C_Descr", "軟件測(cè)試技術(shù)"));
//4發(fā)送命令
int result= scmd.ExecuteNonQuery();
//5處理數(shù)據(jù)
if (result > 0)
{
Console.WriteLine("插入數(shù)據(jù)成功");
}
else
{
Console.WriteLine("插入數(shù)據(jù)失敗");
}
//6最后一步,差點(diǎn)忘記了,一定要關(guān)閉連接
scon.Close();
Console.ReadKey();
}
}
}
程序執(zhí)行玩之后的效果圖:

以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
- ADO.NET實(shí)現(xiàn)對(duì)SQL Server數(shù)據(jù)庫(kù)的增刪改查示例
- ADO.NET通用數(shù)據(jù)庫(kù)訪問(wèn)類
- ADO.NET數(shù)據(jù)庫(kù)訪問(wèn)技術(shù)
- ADO.NET 連接數(shù)據(jù)庫(kù)字符串小結(jié)(Oracle、SqlServer、Access、ODBC)
- 用C#對(duì)ADO.NET數(shù)據(jù)庫(kù)完成簡(jiǎn)單操作的方法
- 數(shù)據(jù)庫(kù)開發(fā)總結(jié)(ADO.NET小結(jié))
- C#使用ADO.Net連接數(shù)據(jù)庫(kù)與DbProviderFactory實(shí)現(xiàn)多數(shù)據(jù)庫(kù)訪問(wèn)
相關(guān)文章
Repeater的FooterTemplate顯示某列總計(jì)思路與代碼
在Repeater的FooterTemplate顯示某列總計(jì),接下來(lái)與大家分享詳細(xì)的實(shí)現(xiàn)方案,感興趣的各位可以參考下哈2013-03-03
如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF詳解
這篇文章主要給大家介紹了關(guān)于如何使用Rotativa在ASP.NET Core MVC中創(chuàng)建PDF的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02
.NET6?ConfigurationManager的實(shí)現(xiàn)及使用方式
這篇文章主要介紹了.NET6?ConfigurationManager的實(shí)現(xiàn),我們上面展示的這一部分的ConfigurationManager代碼,其實(shí)就是替代了原來(lái)的ConfigurationBuilder類的功能,需要的朋友可以參考下2021-12-12
.NET微服務(wù)架構(gòu)CI/CD鏡像自動(dòng)分發(fā)
這篇文章介紹了.NET微服務(wù)架構(gòu)CI/CD實(shí)現(xiàn)鏡像自動(dòng)分發(fā)的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01

