如何在Visual Studio中编写SQL查询?
How to write the SQL query in Visual Studio?
我想在Visual Studio中写所有的插入、select和删除查询,但我不知道怎么写。
SqlConnection con = new SqlConnection("Data Source=5CG50749V3\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO backup(Option,EquipmentID,SerialNumber,Description,Location,DueDate,DaytoDue,EquipmentWithdraworRemarks,NCRorOOTHistory,LastOOTissuanceDate,AvailableinSapphire,ResponsiblePerson,CalibrationOption,CalibrationSourceorLab,YearofManufacturing,ManufacturerorVendor,CalibrationCost,AssetNo,CalibrationTAT,SendInDate,Status), SELECT * FROM Equipment where (SerialNumber = '" + TextBox2.Text + "' or EquipmentID = '" + TextBox1.Text + "' ), DELETE FROM Equipment where (SerialNumber = '" + TextBox2.Text + "' or EquipmentID = '" + TextBox1.Text + "' )", con);
cmd.ExecuteNonQuery();
con.Close();
SqlCommand(String) 方法仅采用一个参数并使用查询文本初始化 SqlCommand class 的新实例。
您可以使用如下参数化查询来执行此操作:
string connectionstring = "Data Source=5CG50749V3\SQLEXPRESS;Initial Catalog=test;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionstring))
{
string sql = @"INSERT IntO [backup](Option,EquipmentID,SerialNumber,Description,Location,
DueDate,DaytoDue,EquipmentWithdraworRemarks,NCRorOOTHistory,LastOOTissuanceDate,
AvailableinSapphire,ResponsiblePerson,CalibrationOption,CalibrationSourceorLab,
YearofManufacturing,ManufacturerorVendor,CalibrationCost,AssetNo,CalibrationTAT,SendInDate,Status)
SELECT * FROM Equipment where SerialNumber = @serialnumber or EquipmentID = @equipmentId;
DELETE FROM Equipment where SerialNumber = @serialnumber or EquipmentID = @equipmentId";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.Add("@serialnumber", SqlDbType.NVarChar).Value = "testSerialNumber";
command.Parameters.Add("@equipmentId", SqlDbType.NVarChar).Value = "testequipmentid";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}
我想在Visual Studio中写所有的插入、select和删除查询,但我不知道怎么写。
SqlConnection con = new SqlConnection("Data Source=5CG50749V3\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO backup(Option,EquipmentID,SerialNumber,Description,Location,DueDate,DaytoDue,EquipmentWithdraworRemarks,NCRorOOTHistory,LastOOTissuanceDate,AvailableinSapphire,ResponsiblePerson,CalibrationOption,CalibrationSourceorLab,YearofManufacturing,ManufacturerorVendor,CalibrationCost,AssetNo,CalibrationTAT,SendInDate,Status), SELECT * FROM Equipment where (SerialNumber = '" + TextBox2.Text + "' or EquipmentID = '" + TextBox1.Text + "' ), DELETE FROM Equipment where (SerialNumber = '" + TextBox2.Text + "' or EquipmentID = '" + TextBox1.Text + "' )", con);
cmd.ExecuteNonQuery();
con.Close();
SqlCommand(String) 方法仅采用一个参数并使用查询文本初始化 SqlCommand class 的新实例。
您可以使用如下参数化查询来执行此操作:
string connectionstring = "Data Source=5CG50749V3\SQLEXPRESS;Initial Catalog=test;Integrated Security=True";
using (SqlConnection connection = new SqlConnection(connectionstring))
{
string sql = @"INSERT IntO [backup](Option,EquipmentID,SerialNumber,Description,Location,
DueDate,DaytoDue,EquipmentWithdraworRemarks,NCRorOOTHistory,LastOOTissuanceDate,
AvailableinSapphire,ResponsiblePerson,CalibrationOption,CalibrationSourceorLab,
YearofManufacturing,ManufacturerorVendor,CalibrationCost,AssetNo,CalibrationTAT,SendInDate,Status)
SELECT * FROM Equipment where SerialNumber = @serialnumber or EquipmentID = @equipmentId;
DELETE FROM Equipment where SerialNumber = @serialnumber or EquipmentID = @equipmentId";
using (SqlCommand command = new SqlCommand(sql, connection))
{
command.Parameters.Add("@serialnumber", SqlDbType.NVarChar).Value = "testSerialNumber";
command.Parameters.Add("@equipmentId", SqlDbType.NVarChar).Value = "testequipmentid";
connection.Open();
command.ExecuteNonQuery();
connection.Close();
}
}