C # access the database using Stored Procedures

Source: Internet
Author: User
Suppose there is a data table named news in the database. The stored PROCEDURE for adding a record is as follows: create procedure AddNews
@ Id int,
@ Title nvarchar (200 ),
@ Content nvarchar (4000 ),
@ Publishtime datetime,
@ Replies int
AS
Insert news (id, title, content, publishtime, replies)
Values
(@ Id, @ title, @ content, @ publishtime, @ replies)


The database access code is as follows: string source =
"Server = 169.254.0.1;" +
"User id = softgroup;" +
"Password = 1234;" +
"Database = newsdb ";

String source =
"Server = 169.254.0.1;" +
"Integrated security = SSPI;" +
"Database = sstinews ";

SqlConnection conn = new SqlConnection (source );

Conn. Open ();
SqlCommand cmd = new SqlCommand ("AddNews", conn );
Cmd. CommandType = CommandType. StoredProcedure;
Cmd. Parameters. Add (new SqlParameter ("@ id", SqlDbType. Int, 4 ));
Cmd. Parameters. Add (new SqlParameter ("@ title", SqlDbType. NVarChar, 200 ));
Cmd. Parameters. Add (new SqlParameter ("@ content", SqlDbType. NVarChar, 4000 ));
Cmd. Parameters. Add (new SqlParameter ("@ publishtime", SqlDbType. DateTime, 8 ));
Cmd. Parameters. Add (new SqlParameter ("@ replies", SqlDbType. Int, 4 ));
Cmd. UpdatedRowSource = UpdateRowSource. None;

Cmd. Parameters ["@ id"]. Value = art. ID;
Cmd. Parameters ["@ title"]. Value = art. Title;
Cmd. Parameters ["@ content"]. Value = art. Content;
Cmd. Parameters ["@ publishtime"]. Value = art. PublishTime;
Cmd. Parameters ["@ replies"]. Value = art. Replies;
Cmd. ExecuteNonQuery ();
Conn. Close ();

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.