M
maiyude
i found an hardest problem in the ado.net, does any expert could help me ?
the source code is below:
public partial class Delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String sqlconn = "Data Source=localhost;Initial
Catalog=pubs;Integrated Security=True;User ID =sa;Password =";
SqlConnection myConnection = new SqlConnection(sqlconn);
myConnection.Open();
///puzzle1 begin.
SqlCommand myCommand = new SqlCommand("select * from authors",
myConnection);
//set DeleteCommand
SqlCommand sqlDeleteCommand1 = new SqlCommand();
sqlDeleteCommand1.CommandText = @"DELETE FROM authors WHERE
au_id = @Original_au_id";
sqlDeleteCommand1.Connection = myConnection;
///what is the difference between the myCommand statement and the
sqlDeleteCommand1?
///could i replace the above 3 sentence of the set DeleteCommand for
"SqlCommand sqlDeleteCommand1=new SqlCommand("DELETE FROM authors WHERE
au_id = @Original_au_id",myConnection);? why?
///if the @Original_au_id is the parameter which will be replaced with value
in the future, then what sentence below would do this(change the
@Original_au_id from unknown value(null?) to "322-22-2222"?
///puzzle1 end.
///puzzle2 begin
sqlDeleteCommand1.Parameters.Add(new SqlParameter("@Original_au_id",
System.Data.SqlDbType.VarChar, 11, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "au_id",
System.Data.DataRowVersion.Original, null));
///what does the above sentence mean?
///Could i write in this way (sqlDeleteCommand1.Parameters.Add(new
SqlParameter("@Original_au_id", System.Data.SqlDbType.VarChar, 11,"au_id");?
///puzzle2 end
SqlDataAdapter Adapter=new SqlDataAdapter();
Adapter.SelectCommand=myCommand;
Adapter.DeleteCommand = sqlDeleteCommand1;
DataSet myDs=new DataSet();
Adapter.Fill(myDs);
Response.Write("<h3>dele data</h3><hr>");
//get DataTable
///puzzle 3 begin
DataTable myTable = myDs.Tables[0];
///could i rewrite the above sentence with name ? eg, DataTable
myTalbe=myDs.Table.SelectCommand?
///what does the above sentence mean?
///is there anything exists in the myDs.Tables[0]?
///puzzle 3 end.
//look for the row for delete
foreach(DataRow row in myTable.Rows)
{
if (row["au_id"].ToString() == "322-22-2222")
{
//delete row
row.Delete();
Response.Write("delete success");
Adapter.Update(myDs);
//close the connection to the database
myConnection.Close();
return;
}
}
Response.Write("Not found the record for delete");
//close the database connection.
myConnection.Close();
}
}
My Question is all above in the /// codes, thank you :
the source code is below:
public partial class Delete : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String sqlconn = "Data Source=localhost;Initial
Catalog=pubs;Integrated Security=True;User ID =sa;Password =";
SqlConnection myConnection = new SqlConnection(sqlconn);
myConnection.Open();
///puzzle1 begin.
SqlCommand myCommand = new SqlCommand("select * from authors",
myConnection);
//set DeleteCommand
SqlCommand sqlDeleteCommand1 = new SqlCommand();
sqlDeleteCommand1.CommandText = @"DELETE FROM authors WHERE
au_id = @Original_au_id";
sqlDeleteCommand1.Connection = myConnection;
///what is the difference between the myCommand statement and the
sqlDeleteCommand1?
///could i replace the above 3 sentence of the set DeleteCommand for
"SqlCommand sqlDeleteCommand1=new SqlCommand("DELETE FROM authors WHERE
au_id = @Original_au_id",myConnection);? why?
///if the @Original_au_id is the parameter which will be replaced with value
in the future, then what sentence below would do this(change the
@Original_au_id from unknown value(null?) to "322-22-2222"?
///puzzle1 end.
///puzzle2 begin
sqlDeleteCommand1.Parameters.Add(new SqlParameter("@Original_au_id",
System.Data.SqlDbType.VarChar, 11, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "au_id",
System.Data.DataRowVersion.Original, null));
///what does the above sentence mean?
///Could i write in this way (sqlDeleteCommand1.Parameters.Add(new
SqlParameter("@Original_au_id", System.Data.SqlDbType.VarChar, 11,"au_id");?
///puzzle2 end
SqlDataAdapter Adapter=new SqlDataAdapter();
Adapter.SelectCommand=myCommand;
Adapter.DeleteCommand = sqlDeleteCommand1;
DataSet myDs=new DataSet();
Adapter.Fill(myDs);
Response.Write("<h3>dele data</h3><hr>");
//get DataTable
///puzzle 3 begin
DataTable myTable = myDs.Tables[0];
///could i rewrite the above sentence with name ? eg, DataTable
myTalbe=myDs.Table.SelectCommand?
///what does the above sentence mean?
///is there anything exists in the myDs.Tables[0]?
///puzzle 3 end.
//look for the row for delete
foreach(DataRow row in myTable.Rows)
{
if (row["au_id"].ToString() == "322-22-2222")
{
//delete row
row.Delete();
Response.Write("delete success");
Adapter.Update(myDs);
//close the connection to the database
myConnection.Close();
return;
}
}
Response.Write("Not found the record for delete");
//close the database connection.
myConnection.Close();
}
}
My Question is all above in the /// codes, thank you :