Because of the content, the title may contain single quotes, the direct use of SQL INSERT command will be an error, there are two ways to do this, one is to replace single quotes into two single quotes, the 2nd way is to use stored procedures.
The format of the table Mybbs is defined as follows:
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]
1. Replace single quotes with two single quotes:
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";
Single quotes are replaced with "'" to insert ' into SQL Server;
String Title=textbox1.text.replace ("'", "" ");
String Content=textbox2.text.replace ("'", "" ");
if (title.trim () = = "" | | Content.trim () = = "") return;
String Insertcmd =@ "insert into Mybbs (title,content) Values (' + Title +" ', ' "+content+") ";
1 Create the stored procedure:
Create proc Insertmybbsproc (@Title char (160), @Author char (), @Content ntext)
As
Insert into Mybbs (title,author,content) Values (@Title, @Author, @Content)
2 test stored procedures in Query Analyzer:
DECLARE @title char (160)
Declare @author char (20)
Declare @content char (600)
Set @title = ' Test Title 3 '
Set @author = ' David Euler 3 '
Set @content = ' It is the content 3 '
exec insertmybbsproc @title, @author, @content
3 Execute stored procedures through SqlCommand in C #:
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";
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.