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

使用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类往数据库添加数据的代码为:

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控件

KeleyiSQLHelper类的代码参考:http://keleyi.com/a/bjac/5e1754ac389f5fcb.htm

其中SQLTools的代码等最新源代码请到http://hovertree.codeplex.com的SOURCE CODE查看。

http://keleyi.com/a/bjae/ue6stb42.htm

http://hvt.iteye.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。