最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
ADO.Net:使用DataReader向数据库中插入数据
时间:2022-06-30 10:38:37 编辑:袖梨 来源:一聚教程网
连接数据库coreDB,向表myBBS中插入文章。表myBBS的定义如下:
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[myBBS]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[myBBS]
GO
CREATE TABLE [dbo].[myBBS] (
[ID] [bigint] IDENTITY (1, 1) NOT NULL ,
[Title] [char] (160) COLLATE Chinese_PRC_CI_AS NULL ,
[Author] [char] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[Date_of_Created] [datetime] NULL ,
[Abstract] [char] (480) COLLATE Chinese_PRC_CI_AS NULL ,
[Content] [ntext] COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
TextBox1,TextBox2分别为提供用户输入标题和文章内容的两个textBox,
用户点击Button1按钮时检查并保存文章到数据库中。
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection coreDB=new SqlConnection();
coreDB.ConnectionString= "workstation id="GQA-ERIC-LV";packet size=4096;integrated security=SSPI;" +
"data source="gqa-eric-lv";persist security info=False;initial catalog=CoreDB";
string Title=TextBox1.Text;
string Content=TextBox2.Text;
if(Title.Trim()==""||Content.Trim()=="")return;
string mySelectQuery =@"insert into myBBS (Title,Content) Values('"+ Title + "','" +Content+"')";
SqlCommand myCommand = new SqlCommand(mySelectQuery,coreDB);
coreDB.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
myReader.Close();
coreDB.Close();
}
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[myBBS]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[myBBS]
GO
CREATE TABLE [dbo].[myBBS] (
[ID] [bigint] IDENTITY (1, 1) NOT NULL ,
[Title] [char] (160) COLLATE Chinese_PRC_CI_AS NULL ,
[Author] [char] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[Date_of_Created] [datetime] NULL ,
[Abstract] [char] (480) COLLATE Chinese_PRC_CI_AS NULL ,
[Content] [ntext] COLLATE Chinese_PRC_CI_AS NOT NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
TextBox1,TextBox2分别为提供用户输入标题和文章内容的两个textBox,
用户点击Button1按钮时检查并保存文章到数据库中。
private void Button1_Click(object sender, System.EventArgs e)
{
SqlConnection coreDB=new SqlConnection();
coreDB.ConnectionString= "workstation id="GQA-ERIC-LV";packet size=4096;integrated security=SSPI;" +
"data source="gqa-eric-lv";persist security info=False;initial catalog=CoreDB";
string Title=TextBox1.Text;
string Content=TextBox2.Text;
if(Title.Trim()==""||Content.Trim()=="")return;
string mySelectQuery =@"insert into myBBS (Title,Content) Values('"+ Title + "','" +Content+"')";
SqlCommand myCommand = new SqlCommand(mySelectQuery,coreDB);
coreDB.Open();
SqlDataReader myReader = myCommand.ExecuteReader();
myReader.Close();
coreDB.Close();
}
相关文章
- 寂静岭f御守乌鸦在哪 御守乌鸦效果介绍及获取攻略 09-30
- 最后纪元兑换码是什么 最后纪元最新2025兑换码一览 09-30
- 双点博物馆兑换码分享 双点博物馆最新兑换码2025 09-30
- 消逝的光芒兑换码是什么 消逝的光芒最新2025兑换码大全 09-30
- 边境开拓者兑换码分享 边境开拓者最新2025兑换码一览 09-30
- 燕云十六声活人医馆二楼怎么开锁-活人医馆二楼进入方法 09-30