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 ();
}