我已将此代码段编码为克隆表格中的选定行:
protected void Button4_Click(object sender, EventArgs e)
{
DateTime ucode = DateTime.Now;
string slctedProj = DropDownList1.SelectedItem.ToString();
string new_code = "PR" + ucode.ToString("ssmmHHddMM");
string query = @"INSERT INTO Projects (proj_id, proj_prod_id, proj_cust_id, proj_man_id,
proj_name, proj_date, proj_num_of_vehicles, proj_coach_vehicle,
proj_contract_value, proj_length, proj_width, proj_height, proj_passenger_seats,
proj_passenger_total, proj_type, proj_notes, uname, proj_brand, proj_systemvoltage,
proj_gauge, proj_service_speed)
SELECT proj_id, proj_prod_id, proj_cust_id, proj_man_id,
proj_name, proj_date, proj_num_of_vehicles, proj_coach_vehicle,
proj_contract_value, proj_length, proj_width, proj_height, proj_passenger_seats,
proj_passenger_total, proj_type, proj_notes, uname, proj_brand, proj_systemvoltage,
proj_gauge, proj_service_speed
FROM Projects WHERE proj_name =" + "'" + slctedProj + "'";
System.Windows.Forms.MessageBox.Show(query);
string getconnstring = ConfigurationManager.ConnectionStrings["stad_conn"].ConnectionString;
SqlConnection conn = new SqlConnection(getconnstring);
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
代码工作正常,但我需要在新行中替换“proj_id”和字符串“new_code”,并在“proj_name”的开头附加“Cloned”。 我怎么能这样做?
答案 0 :(得分:0)
首先:Use parameters in your SQL command。
继承修改后的代码,带参数。请注意,参数上的Add方法既指定数据类型,也指定长度。 It's better to specify these explicitly而不是让ADO.Net认为它知道你想要什么。
DateTime ucode = DateTime.Now;
string slctedProj = DropDownList1.SelectedItem.ToString();
string new_code = "PR" + ucode.ToString("ssmmHHddMM");
string new_projectName = "Some new name";
string query = @"INSERT INTO Projects (proj_id, proj_prod_id, proj_cust_id, proj_man_id,
proj_name, proj_date, proj_num_of_vehicles, proj_coach_vehicle,
proj_contract_value, proj_length, proj_width, proj_height, proj_passenger_seats,
proj_passenger_total, proj_type, proj_notes, uname, proj_brand, proj_systemvoltage,
proj_gauge, proj_service_speed)
SELECT @NewProjectID, proj_prod_id, proj_cust_id, proj_man_id,
@NewProjectName, proj_date, proj_num_of_vehicles, proj_coach_vehicle,
proj_contract_value, proj_length, proj_width, proj_height, proj_passenger_seats,
proj_passenger_total, proj_type, proj_notes, uname, proj_brand, proj_systemvoltage,
proj_gauge, proj_service_speed
FROM Projects WHERE proj_name = @SelectedProj";
System.Windows.Forms.MessageBox.Show(query);
string getconnstring = ConfigurationManager.ConnectionStrings["stad_conn"].ConnectionString;
SqlConnection conn = new SqlConnection(getconnstring);
conn.Open();
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add("@NewProjectID", SqlDbType.VarChar, 20).Value = new_code;
cmd.Parameters.Add("@NewProjectName", SqlDbType.VarChar, 20).Value = new_projectName;
cmd.Parameters.Add("@SelectedProj", SqlDbType.VarChar, 20).Value = slctedProj;
cmd.ExecuteNonQuery();
conn.Close();
答案 1 :(得分:0)
这不是答案。您可以通过使用FK(仅适用于克隆项目)保留对源/初始项目的引用来改进此设计:
ALTER TABLE dbo.Projects
ADD source_proj_id INT NULL
REFERENCES dbo.Projects(proj_id);
GO
并且您还需要更改insert语句:
string query = @"INSERT INTO Projects
(proj_id, source_proj_id, proj_prod_id, ...)
SELECT @NewProjectID, proj_id, proj_prod_id, ...
FROM Projects WHERE proj_name = @SelectedProj";