您的位置:首页 - ASP.NET
使用KeleyiSQLHelper往数据库添加数据
使用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

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

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

其中SQLTools的代码为:
namespace HoverTreeWeb
{
public class SQLTools
{
public static string GetQuotes(string sql)
{
if (string.IsNullOrEmpty(sql))
return "''";
return "'" + sql.Replace("'", "''") + "'";
}
}
}

最新源代码请到http://hovertree.codeplex.com的SOURCE CODE查看。
本站导航
相关文章
柯乐义 Copyright© keleyi.com