ADO.Net:使用DataReader向數據庫中插入數據
發表于:2007-06-30來源:作者:點擊數:
標簽:
連接 數據庫 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
連接
數據庫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();
}
原文轉自:http://www.kjueaiud.com