most puzzle question 1 about sqlparameters.My question is following the ///.thank you.

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 :
 
W

William Vaughn \(MVP\)

See >>>>

--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker’s Guide to Visual Studio and SQL Server (7th Edition)
http://betav.com http://betav.com/blog/billva
____________________________________________________________________________________________



maiyude said:
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 deleteforeach(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 :


 
M

maiyude

thank you very much.


William Vaughn (MVP) said:
See >>>>

--
__________________________________________________________________________
William R. Vaughn
President and Founder Beta V Corporation
Author, Mentor, Dad, Grandpa
Microsoft MVP
(425) 556-9205 (Pacific time)
Hitchhiker’s Guide to Visual Studio and SQL Server (7th Edition)
http://betav.com http://betav.com/blog/billva
____________________________________________________________________________________________



maiyude said:
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?
It means that the Visual Studio code generator built the code and
specified all of the values for the constructor.
///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?
No. The SelectCommand is not a property of the table but of the
DataAdapter.
///what does the above sentence mean?
///is there anything exists in the myDs.Tables[0]?
Perhaps, if the Fill returned a rowset.
///puzzle 3 end.
//look for the row for delete
Use a DataView filter to find the row.
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 :


 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top