Simple discussion on Ado.net database script _ Practical skills

Source: Internet
Author: User
Tags create database

This time I use ado.net to insert a piece of data into the database. The master uses the stored procedure. I do not want to be in the form of SQL text every time, so never progress ~ ~ ~

First, I post the database script that I'm going to use for this exercise:

 Use master-DROP database using the system database Go IF EXISTS (SELECT * from sysdatabases WHERE name=n ' db_mystudentlife ') [Db_mystu Dentlife]; --If the database you want to create is present, delete go CREATE DATABASE [Db_mystudentlife]--Create the databases go using [Db_mystudentlife]--Use the database went IF EXISTS (SE Lect * from sysobjects WHERE name=n ' MyClass ') drop TABLE [MyClass]--If the data table to be created exists, delete it (note sysobjects, be sure to be all lowercase, otherwise there are errors, cannot write In uppercase.    Go CREATE TABLE MyClass--Creates a datasheet (c_id INT NOT null PRIMARY KEY,--class number c_name NVARCHAR NOT NULL,
 --Class name C_descr nvarchar (max) NOT null--class introduction);  Go IF EXISTS (SELECT * from sysobjects WHERE name=n ' mystudent ") DROP TABLE mystudent go CREATE table mystudent (s_id int NOT NULL PRIMARY key,--s_name nvarchar (a) not NULL,--Name S_gender char (2) NOT null--sex s_address N varchar (max) NOT NULL,--address s_phone nvarchar () NOT NULL, phone s_age int not NULL, age S_birthday Dateti Me NOT NULL,--Birthday s_cardid int NOT NULL,--ID number s_cid int Not NULL references MyClass (C_ID)--class number);

 

Then you select the script you just executed, create a good database, and then use the data below to add data to the database table

 Insert into MyClass (C_ID,C_NAME,C_DESCR) VALUES (1, ' Software class 1108 ', ' Wuhan Software Engineering Vocational College ');
 Insert into MyClass (C_ID,C_NAME,C_DESCR) VALUES (2, ' software class 1107 ', ' Wuhan Software Engineering Vocational College ');
 Insert into MyClass (C_ID,C_NAME,C_DESCR) VALUES (3, ' Experimental class ', ' Wuhan Software Engineering Vocational College '); Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 1 ', ' dick ', ' male ')
 , ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 2 ', ' Dick ', ' male ')
 , ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 3 ', ' Dick ', ' male ')
 , ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 4 ', ' Dick ', ' male ')
 , ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert INTO Mystudent (s_id,s_name,s_gender,s_age,s_birthday,s_cardid,s_phone,s_address,s_CID) VALUES (' 5 ', ' Dick ', ' Men ', ', ', ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1); Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 6 ', ' Dick ', ' male ')
 , ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 7 ', ' Dick ', ' male ')
 , ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 8 ', ' Dick ', ' male ')
 , ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 9 ', ' Dick ', ' male ')
 , ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 10 ', ' Dick ', '
 Male ', ', ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert INTO Mystudent (S_id,s_name,s_gender,s_age,s_birthDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 11 ', ' Dick ', ' Men ', ', ', ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan
 ', 1); Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 12 ', ' Dick ', '
 Male ', ', ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 13 ', ' Dick ', '
 Male ', ', ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 14 ', ' Dick ', '
 Male ', ', ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 15 ', ' Dick ', '
 Male ', ', ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; Insert into mystudent (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 16 ', ' Dick ', '
 Male ', ', ' 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1; INSERT INTO MystudENT (S_ID,S_NAME,S_GENDER,S_AGE,S_BIRTHDAY,S_CARDID,S_PHONE,S_ADDRESS,S_CID) VALUES (' 17 ', ' Dick ', ' Male ', ' 22 ', '

 1992-09-26 ', ' 111111111 ', ' 11232131234 ', N ' Shenzhen Bao Baoan ', 1;

Explain, wait. I'm going to insert data into the MyClass table and now create an inserted stored procedure for this table:

IF object_id (' ins_classed ', ' P ') is not NULL 
 DROP PROCEDURE ins_classed
 go 
 CREATE PROCEDURE ins_classed
 @C_ID int, 
 @C_Name nvarchar,
 @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)
    @C_D ESCR-C_descr-nvarchar (max)
    );
    
 Go 

The following starts the program implementation:

I was reviewing, ado.net, now casually built a console application to start my test:

Note that in the following example, in order to be as simple and easy to understand as possible, I did not put that part of the connection string into the configuration file.

If you want to put it, use the System.Configuration namespace, and a ConfigurationManager class. The details will not be said.

Please see the specific implementation code:

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 inserts a piece of data into the database {class Program {//connection string private static string Sqlcon = ' server=.;
  Database=db_mystudentlife;uid=sa;pwd=password_1 ";
   
   static void Main (string[] args) {//1 Creates a Connection object (connection string) SqlConnection Scon = new SqlConnection (Sqlcon);
   2 Create a Command object (set properties for the Command object) SqlCommand scmd = new SqlCommand ();
   Scmd.commandtext = "ins_classed"; Scmd.commandtype = CommandType.StoredProcedure; Here I use stored procedures to insert data scmd.

   Connection = Scon; 3 Open the database connection Scon.
   

   Open (); Sets the parameter scmd.
   Parameters.Add (New SqlParameter ("@C_ID", 6)); Scmd.
   Parameters.Add (New SqlParameter ("@C_Name", "Test Class")); Scmd.
   Parameters.Add (New SqlParameter ("@C_Descr", "Software Testing Technology")); 4 Send command int result= scmd.

   ExecuteNonQuery ();
   5 Processing data if (Result > 0) {Console.WriteLine ("Insert data succeeded"); } else {Console.
   WriteLine ("Insert data Failed"); //6 The last step, almost forget, be sure to close the connection Scon.

   Close ();
 
  Console.readkey ();

 }
 }
}

The effect diagram after the program performs the play:

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.