Ado.net: Inserting data into the database using DataReader

Source: Internet
Author: User
ado| Insert | data | Database connection Database COREDB, inserts an article into the table Mybbs. The table Mybbs is defined as follows:
if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ Mybbs] and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
drop table [dbo]. [Mybbs]
Go

CREATE TABLE [dbo]. [Mybbs] (
[ID] [bigint] IDENTITY (1, 1) is not NULL,
[Title] [Char] (160) COLLATE chinese_prc_ci_as NULL,
[Author] [Char] (m) COLLATE chinese_prc_ci_as NULL,
[Date_of_created] [DateTime] Null
[Abstract] [Char] (COLLATE) Chinese_prc_ci_as NULL,
[Content] [ntext] COLLATE Chinese_prc_ci_as not NULL
) on [PRIMARY] textimage_on [PRIMARY]
Go

Textbox1,textbox2 provides two textbox for user input title and article content, respectively,
Check and save the article to the database when the user clicks the Button1 button.

private void Button1_Click (object sender, System.EventArgs e)
{
SqlConnection coredb=new SqlConnection ();
coredb.connectionstring= "Workstation id=\" gqa-eric-lv\ ";p acket size=4096;integrated Security=sspi;"
"Data source=\" gqa-eric-lv\ ";p ersist security info=false;initial catalog=coredb";

String Title=textbox1.text;
String Content=textbox2.text;
if (title.trim () = = "" | | Content.trim () = = "") return;

String mySelectQuery =@ "insert into Mybbs (title,content) Values (' + Title +" ', ' "+content+") ";
SqlCommand mycommand = new SqlCommand (myselectquery,coredb);

Coredb.open ();
SqlDataReader myreader = Mycommand.executereader ();
Myreader.close ();
Coredb.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.