欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

ASP.NET往数据库添加数据实例

程序员文章站 2022-04-10 13:21:40
...
使用Sql Server建立一个数据库hovertree

然后使用以下脚本创建表hovertree_usermessage
USE [hovertree]
GO
/****** Object: Table [dbo].[hovertree_usermessage] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[hovertree_usermessage](
[hvtId] [int] IDENTITY(1,1) NOT NULL,
[hvtTitle] [nvarchar](500) NULL,
[hvtContent] [nvarchar](max) NULL,
[hvtUser] [nvarchar](50) NULL,
[hvtTime] [datetime] NULL,
[hvtReply] [nvarchar](max) NULL,
[hvtIsShow] [bit] NULL,
CONSTRAINT [PK_hovertree_usermessage] PRIMARY KEY CLUSTERED 
(
[hvtId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[hovertree_usermessage] ADD CONSTRAINT [DF_hovertree_usermessage_hvtTime] DEFAULT (getdate()) FOR [hvtTime]
GO

那么C#使用KeleyiSQLHelper类往数据库添加数据的代码为:

C#代码

string m_sql="insert into hovertree_usermessage (hvtTitle,hvtContent,hvtUser)values("  
+SQLTools.GetQuotes(txtTitle.Text)+","   
+ SQLTools.GetQuotes(txtContent.Text)+","   
+SQLTools.GetQuotes(txtUser.Text) +")";  
KeleyiSQLHelper.ExecuteSql(m_sql);

其中txtTitle、txtContent、txtUser为ASP.NET的TextBox控件