使用 ASP.NET
完成对数据的增删改查操作,数据库使用MSSQL.
![MSSQL](https://upload.wikimedia.org/wikipedia/commons/thumb/d/d8/SQL_Server_2005_Developer_Edition%2C_CTP_beta_DVD.jpg/220px-
SQL_Server_2005_Developer_Edition%2C_CTP_beta_DVD.jpg)
DATABASE TABLE: Sites
ID INT IDENTITY(1,1) PRIMARY KEY,
Name VARCHAR(255) NOT NULL,
NFCCode VARCHAR(16) NOT NULL,
IsArmorySite BIT NOT NULL,
ArmoryID INT NOT NULL,
CompanyID INT NOT NULL FOREIGN KEY REFERENCES Company(UID),
Status VARCHAR(20) NOT NULL
—|—
Question 1
Based on above MSSQL database table, using ASP.NET C# to insert one record
into database.
protected void rbtnAddSubmit_Click(object sender, EventArgs e)
{
if (ValidateInput(“ADD”))
{
/* VARIABLE /
string name = rtxtAddName.Text.Trim();
string nfcCode = rtxtAddNfcCode.Text.Trim();
bool isArmorySite = Convert.ToBoolean(rcbbAddArmorySite.SelectedValue);
string armoryId = rtxtAddArmoryID.Text.Trim();
int companyId = user.CompanyUid;
string status = “Active”;
SqlConnection connection = Utility.GetConnection();
SqlCommand command = new SqlCommand();
try
{
connection.Open();
command.Connection = connection;
/ INSERT YOUR CODE HERE */
RadGrid1.Rebind();
plhAddItem.Visible = false;
plhMain.Visible = true;
AlertMessage(“Success! New site has been added!”);
}
catch (Exception ex)
{
AlertMessage(“Error! Please contact company helpdesk!”);
}
finally
{
try { connection.Close(); }
catch { }
connection.Dispose();
}
}
}
—|—
Question 2
Based on above MSSQL database table, using ASP.NET C# to update one record by
given record ID and other information.
protected void rbtnEditSubmit_Click(object sender, EventArgs e)
{
if (ValidateInput(“EDIT”))
{
/* VARIABLE /
string id = hdfRecordId.Value.Trim();
string name = rtxtEditName.Text.Trim();
string nfcCode = rtxtEditNfcCode.Text.Trim();
bool isArmorySite = Convert.ToBoolean(rcbbEditArmorySite.SelectedValue);
string armoryId = rtxtEditArmoryID.Text.Trim();
int companyId = user.CompanyUid;
string status = “Active”;
SqlConnection connection = Utility.GetConnection();
SqlCommand command = new SqlCommand();
try
{
connection.Open();
command.Connection = connection;
/ INSERT YOUR CODE HERE */
RadGrid1.Rebind();
plhEditItem.Visible = false;
plhMain.Visible = true;
AlertMessage(“Success! Site has been updated!”);
}
catch (Exception ex)
{
AlertMessage(“Error! Please contact company helpdesk!”);
}
finally
{
try { connection.Close(); }
catch { }
connection.Dispose();
}
}
}
—|—
Question 3
Based on above MSSQL database table, using ASP.NET C# to delete one record by
given record ID.
protected void rbtnDeleteSubmit_Click(object sender, EventArgs e)
{
if (ValidateInput(“Delete”))
{
/* VARIABLE /
string id = hdfRecordId.Value.Trim();
SqlConnection connection = Utility.GetConnection();
SqlCommand command = new SqlCommand();
try
{
connection.Open();
command.Connection = connection;
/ INSERT YOUR CODE HERE */
RadGrid1.Rebind();
AlertMessage(“Success! Site has been removed!”);
}
catch (Exception ex)
{
AlertMessage(“Error! Please contact company helpdesk!”);
}
finally
{
try { connection.Close(); }
catch { }
connection.Dispose();
}
}
}
—|—