首页  >  文章  >  后端开发  >  ASP.NET往数据库添加数据实例

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

巴扎黑
巴扎黑原创
2016-12-19 16:00:472322浏览

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

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn