ADO. NET 4-use the SqlCommand object to add records to the database --- ShinePans,

Source: Internet
Author: User

ADO. NET 4-use the SqlCommand object to add records to the database --- ShinePans,

Source code:


Using System; using System. collections. generic; using System. data; using System. data. sqlClient; using System. linq; using System. text; using System. threading. tasks; namespace SQLTest {class Program {static void Main (string [] args) {// connect to the database string connection = "server = Pan Shang \ SQLEXPRESS; database = db_test; trusted_Connection = true "; SqlConnection SC = new SqlConnection (); SC. connectionString = connection; try {s C. Open (); // Open the database connection Console. WriteLine ("the database connection has been enabled! "); SqlCommand cmd = new SqlCommand (); // create the SqlCommand object cmd. commandType = CommandType. text; // set the command to run the command cmd. connection = SC; // set the object property cmd. commandText = "insert into db_student (student_name, student_age, student_address, student_grade) VALUES (@ name, @ age, @ address, @ grade )"; // Add a parameter and assign it to cmd. parameters. add ("@ name", SqlDbType. varChar, 10 ). value = "pan"; cmd. parameters. add ("@ age", SqlDbType. int ). value = 19; cmd. Parameters. add ("@ address", SqlDbType. varChar ). value = "Wuhan"; cmd. parameters. add ("@ grade", SqlDbType. int ). value = 100; int I = cmd. executeNonQuery (); // execute the database add record command if (I> 0) Console. writeLine ("successfully added records"); // Console output add records} catch (Exception ex) {Console. writeLine ("Opening Database Error: {0}", ex. message);} finally {SC. close (); Console. writeLine ("the database connection is closed! ") ;}System. Console. ReadLine ();}}}


Database Design:




Run:




Database status after running:




What is the procedure for ADONET to access the database?

1. Create a database connection character
2. Import the namespace System. data. sqlcen...
3. Create a SQLCONNECTION object in jia and add the link characters
4. Open the database
5. Declare the SQL statement and connection object in the brackets of the SQLcommand object.

6command object. Method To execute related commands

In ADONET technology, the C # object can be connected to the SQL database.

Using System. Data;
Using System. Data. SqlClient;
..

String strConnection = "user id = sa; password = ;";
StrConnection + = "initial catalog = Northwind; Server = YourSQLServer ;";
StrConnection + = "Connect Timeout = 30 ";

SqlConnection objConnection = new SqlConnection (strConnection );
..

ObjConnection. Open ();
ObjConnection. Close ();

The strConnection variable stores the connection string required to connect to the database. It specifies the data provider to be used and the data source to be used.
First, the namespace used to connect to SQL Server is "System. Data. SqlClient ".
The second is his connection string. We will introduce them one by one (note: the parameters are separated by semicolons ):
"User id = sa": the authenticated user name used to connect to the database is sa. It also has an alias "uid", so we can also write this statement as "uid = sa ".
"Password =": The verification password for connecting to the database is blank. Its alias is "pwd", so we can write it as "pwd = ".
Note that your SQL Server must have a user name and password set to log on. Otherwise, you cannot log on using this method. if your SQL Server is set to Windows logon, you do not need to use the "user id" and "password" methods to log on here, you need to use "Trusted_Connection = SSPI" to log on.
"Initial catalog = Northwind": the data source used is "Northwind". Its alias is "Database". This statement can be written as "Database = Northwind ".
"Server = YourSQLServer": Use a Server named "YourSQLServer. its alias is "Data Source", "Address", "Addr ". if the local database is used and the Instance name is defined, it can be written as "Server = (local)/Instance name". If it is a remote Server) "Replace with the name or IP address of the remote server.
"Connect Timeout = 30": the connection Timeout is 30 seconds.

Here, the constructor used to create a connection object is SqlConnection.

Related Article

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.