DB에서 레코드를 하나 지우려고 아래와 같이 짜 보았습니다. 폼에서 cid를 입력하면 DB의 userList테이블에서 읽어와 사용자 정보를 지우는것인데요..
아래쪽에 MessageBox.Show("로우수 :" + ds.Tables["userList"].Rows.Count.ToString()); 요걸로 로우 수를 보면
dr.Delete(); 이후에 dataset의 로우수는 확실히 줄어든것 같은데
adaper.update를 해도 DB의 레코드는 변함이 없어요..
딱히 예외도 안생기구요..
하루종일 씨름했는데도 이유를 모르겠습니다.. 왜그럴까요 살려주세요 감사합니다..
#region userDel : DB에서 유저 삭제
public Boolean userDel(string cid)
{
string connectionString = null;
string name = "";
SqlConnection connection;
SqlCommand command;
SqlDataAdapter adapter = new SqlDataAdapter();
DataSet ds = new DataSet();
string sql = null;
connectionString = "Server = " + this.serverName + "; Database = " + this.DBName + "; User Id = " + DBUserID + "; Password = " + DBPassword;
sql = "select * from userList";
connection = new SqlConnection(connectionString);
connection.Open();
command = new SqlCommand(sql, connection);
adapter.SelectCommand = command;
adapter.FillSchema(ds, SchemaType.Source, "userList");
adapter.Fill(ds, "userList");
if (ds.Tables["userList"].Rows.Count == 0)
{
MessageBox.Show("존재하지 않는 ID입니다.");
return false;
}
DataRow dr = ds.Tables["userList"].Rows.Find(cid);
name = dr["name"].ToString();
if (MessageBox.Show("입력한 정보가 정확한가요?\r" + name + ", " + cid + "," , "확인", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//SqlDataAdaper를 이용하여 Dataset에 정보 입력
//sql = "insert userList (cid, name, phoneNumber) values ("+cid+","+name+","+phoneNumber+")" ;
try
{
//Dataset에 Db delete할 정보 입력
dr.Delete();
ds.Tables["userList"].AcceptChanges();
// BEGIN SEND CHANGES TO SQL SERVER
MessageBox.Show("로우수 :" + ds.Tables["userList"].Rows.Count.ToString());
SqlCommandBuilder objCommandBuilder = new SqlCommandBuilder(adapter);
adapter.Update(ds.Tables["userList"]);
// END SEND CHANGES TO SQL SERVER
objCommandBuilder.Dispose();
ds.Dispose();
adapter.Dispose();
command.Dispose();
connection.Close();
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
else
{
return false;
}
}
#endregion userDel
댓글 0