M
maiyude
public partial class Insert : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string strCon = "Data Source=localhost;Initial
Catalog=pubs;Integrated Security=True;User ID =sa;Password =sa";
SqlConnection myConnection = new SqlConnection(strCon);
// myConnection.Open();
SqlCommand myCommand = new SqlCommand("select * from authors",
myConnection);
//
///puzzle 1 begin
//begin *
SqlCommand sqlInsertCommand1 = new SqlCommand();
sqlInsertCommand1.CommandText = @"INSERT INTO authors(au_id,
au_lname, au_fname, phone, address, city, state, zip, contract)
VALUES (@au_id, @au_lname, @au_fname, @phone, @address, @city, @state, @zip,
@contract);
SELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contract
FROM authors WHERE (au_id = @au_id)";
sqlInsertCommand1.Connection = myConnection;
sqlInsertCommand1.Parameters.Add(new SqlParameter("@au_id",
System.Data.SqlDbType.VarChar, 11, "au_id"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@au_lname",
System.Data.SqlDbType.VarChar, 40, "au_lname"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@au_fname",
System.Data.SqlDbType.VarChar, 20, "au_fname"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@phone",
System.Data.SqlDbType.VarChar, 12, "phone"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@address",
System.Data.SqlDbType.VarChar, 40, "address"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@city",
System.Data.SqlDbType.VarChar, 20, "city"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@state",
System.Data.SqlDbType.VarChar, 2, "state"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@zip",
System.Data.SqlDbType.VarChar, 5, "zip"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@contract",
System.Data.SqlDbType.Bit, 1, "contract"));
//end *
SqlDataAdapter Adapter = new SqlDataAdapter();
Adapter.SelectCommand = myCommand;
Adapter.InsertCommand = sqlInsertCommand1;//if begin* and end*'s
codes is omitted, this line should be omitted too?
DataSet myDs = new DataSet();
Adapter.Fill(myDs);
//
DataTable myTable = myDs.Tables[0];
//
DataRow myRow = myTable.NewRow();
//
myRow["au_id"] = "322-22-2222";
myRow["au_lname"] = "Li";
myRow["au_fname"] = "Paul";
myRow["phone"] = "12345678";
myRow["city"] = "Chengdu";
myRow["contract"] = 1;
//
myTable.Rows.Add(myRow);
//
Adapter.Update(myDs);
//
myConnection.Close();
myConnection.Open();
Adapter.Fill(myDs);
///it seems that there is no use for the codes between begin* and
end*? Because i found there is no Insertcommand
///below except the Adapter.InsertCommand = sqlInsertCommand1?
///what codes would call the function of the Adapter.InsertCommand?
///puzzle1 end
//
Response.Write("<h3>Insert Data</h3><hr>");
Response.Write("<table border=1 cellspacing=0 cellpadding=2>");
//
Response.Write("<tr bgcolor=#DAB4B4>");
foreach (DataColumn myColumn in myTable.Columns)
{
Response.Write("<td>" + myColumn.ColumnName + "</td>");
}
Response.Write("</tr>");
//
foreach (DataRow row in myTable.Rows)
{
if (row["au_id"].ToString() == "322-22-2222")
{
Response.Write("<tr>");
foreach (DataColumn myColumn in myTable.Columns)
{
Response.Write("<td>" + row[myColumn] + "</td>");
}
Response.Write("</tr>");
break;
}
}
Response.Write("</table>");
myConnection.Close();
}
}
{
protected void Page_Load(object sender, EventArgs e)
{
string strCon = "Data Source=localhost;Initial
Catalog=pubs;Integrated Security=True;User ID =sa;Password =sa";
SqlConnection myConnection = new SqlConnection(strCon);
// myConnection.Open();
SqlCommand myCommand = new SqlCommand("select * from authors",
myConnection);
//
///puzzle 1 begin
//begin *
SqlCommand sqlInsertCommand1 = new SqlCommand();
sqlInsertCommand1.CommandText = @"INSERT INTO authors(au_id,
au_lname, au_fname, phone, address, city, state, zip, contract)
VALUES (@au_id, @au_lname, @au_fname, @phone, @address, @city, @state, @zip,
@contract);
SELECT au_id, au_lname, au_fname, phone, address, city, state, zip, contract
FROM authors WHERE (au_id = @au_id)";
sqlInsertCommand1.Connection = myConnection;
sqlInsertCommand1.Parameters.Add(new SqlParameter("@au_id",
System.Data.SqlDbType.VarChar, 11, "au_id"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@au_lname",
System.Data.SqlDbType.VarChar, 40, "au_lname"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@au_fname",
System.Data.SqlDbType.VarChar, 20, "au_fname"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@phone",
System.Data.SqlDbType.VarChar, 12, "phone"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@address",
System.Data.SqlDbType.VarChar, 40, "address"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@city",
System.Data.SqlDbType.VarChar, 20, "city"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@state",
System.Data.SqlDbType.VarChar, 2, "state"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@zip",
System.Data.SqlDbType.VarChar, 5, "zip"));
sqlInsertCommand1.Parameters.Add(new SqlParameter("@contract",
System.Data.SqlDbType.Bit, 1, "contract"));
//end *
SqlDataAdapter Adapter = new SqlDataAdapter();
Adapter.SelectCommand = myCommand;
Adapter.InsertCommand = sqlInsertCommand1;//if begin* and end*'s
codes is omitted, this line should be omitted too?
DataSet myDs = new DataSet();
Adapter.Fill(myDs);
//
DataTable myTable = myDs.Tables[0];
//
DataRow myRow = myTable.NewRow();
//
myRow["au_id"] = "322-22-2222";
myRow["au_lname"] = "Li";
myRow["au_fname"] = "Paul";
myRow["phone"] = "12345678";
myRow["city"] = "Chengdu";
myRow["contract"] = 1;
//
myTable.Rows.Add(myRow);
//
Adapter.Update(myDs);
//
myConnection.Close();
myConnection.Open();
Adapter.Fill(myDs);
///it seems that there is no use for the codes between begin* and
end*? Because i found there is no Insertcommand
///below except the Adapter.InsertCommand = sqlInsertCommand1?
///what codes would call the function of the Adapter.InsertCommand?
///puzzle1 end
//
Response.Write("<h3>Insert Data</h3><hr>");
Response.Write("<table border=1 cellspacing=0 cellpadding=2>");
//
Response.Write("<tr bgcolor=#DAB4B4>");
foreach (DataColumn myColumn in myTable.Columns)
{
Response.Write("<td>" + myColumn.ColumnName + "</td>");
}
Response.Write("</tr>");
//
foreach (DataRow row in myTable.Rows)
{
if (row["au_id"].ToString() == "322-22-2222")
{
Response.Write("<tr>");
foreach (DataColumn myColumn in myTable.Columns)
{
Response.Write("<td>" + row[myColumn] + "</td>");
}
Response.Write("</tr>");
break;
}
}
Response.Write("</table>");
myConnection.Close();
}
}