ado C# 添加删除记录

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

public void AddRecord ( int CategoryID , string time , string desc , string content

)

{

 

 

try

{

 

 

SqlCommand com

;

 

 

com = new SqlCommand ( "SELECT * FROM Content" , conn

);

 

 

SqlDataAdapter da = new SqlDataAdapter ( com

);

 

 

SqlCommandBuilder scb = new SqlCommandBuilder ( da

);

 

 

DataSet ds = new DataSet

();

 

 

da . Fill ( ds

);

 

 

DataTable dt = ds . Tables

[0];

 

 

DataRow newRow = dt . NewRow

();

 

 

newRow [ "CategoryID" ] = CategoryID

;

 

 

newRow [ "ContentDescription" ] = desc

;

 

 

newRow [ "ContentTxt" ] = content

;

 

 

dt . Rows . Add ( newRow

);

 

 

da . Update ( dt

);

}

 

 

catch ( System . Exception ex

)

{

 

}

}

 

 

public void RemoveRecord ( int CategoryID , int RecordID

)

{

 

 

SqlCommand com

;

 

 

com = new SqlCommand ( "SELECT * FROM Content Where CategoryID = " + CategoryID , conn

);

 

 

SqlDataAdapter da = new SqlDataAdapter ( com

);

 

 

SqlCommandBuilder scb = new SqlCommandBuilder ( da

);

 

 

DataSet ds = new DataSet

();

 

 

da . Fill ( ds

);

 

 

DataTable dt = ds . Tables

[0];

 

 

foreach ( DataRow row in dt . Rows

)

{

 

 

if ( row [ "ContentID" ]. Equals ( RecordID

))

{

 

 

row . Delete

();

 

 

break

;

}

}

 

 

da . Update ( dt

);

 

 

dt . AcceptChanges

();

你可能感兴趣的:(String,C#,dataset)