-3
我在asp.net下面的C#代码:什么是错误'slip_id'附近的语法不正确?
public partial class UpdateSlip : System.Web.UI.Page
{
protected void search_Click(object sender, EventArgs e)
{
// Clear text controls
txtSlipID.Text = string.Empty;
txtSlipLength.Text = string.Empty;
txtSlipWidth.Text = string.Empty;
txtCovered.Text = string.Empty;
txtFee.Text = string.Empty;
intDockID.Text = string.Empty;
intBoatID.Text = string.Empty;
string connection =
WebConfigurationManager.ConnectionStrings["popeye_marina"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
string selectSQL;
selectSQL = "SELECT * FROM slip WHERE [email protected]";
SqlCommand cmd = new SqlCommand(selectSQL, con);
int anInteger = int.Parse(intSlipID_srch.Text);
cmd.Parameters.Add("@id", SqlDbType.Int).Value = anInteger;
// Open database and read information
try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
// Fill the page controls
txtSlipID.Text = reader["slip_id"].ToString();
txtSlipLength.Text = reader["slip_length"].ToString();
txtSlipWidth.Text = reader["slip_width"].ToString();
txtCovered.Text = reader["covered"].ToString();
txtFee.Text = reader["annual_fee"].ToString();
intDockID.Text = reader["dock_id"].ToString();
intBoatID.Text = reader["boat_id"].ToString();
reader.Close();
lblStatus.Text = "";
}
catch (Exception err)
{
lblStatus.Text = "Error getting Slip. ";
lblStatus.Text += err.Message;
}
finally
{
con.Close();
}
}
protected void btn_update_Click(object sender, EventArgs e)
{
string connection =
WebConfigurationManager.ConnectionStrings["popeye_marina"].ConnectionString;
SqlConnection con = new SqlConnection(connection);
string updateSQL;
int anInt = int.Parse(intBoatID.Text);
updateSQL = "UPDATE slip SET ";
/*updateSQL += "slip_length = 'txtlength.Text'";
updateSQL += "slip_width = txtSlipWidth.Text, ";
updateSQL += "covered = txtCovered.Text, ";
updateSQL += "annual_fee = 'fee' ";
updateSQL += "dock_id = intDockID.Text, ";*/
updateSQL += "boat_id = anInt";
updateSQL += "WHERE [email protected]";
SqlCommand cmd = new SqlCommand(updateSQL, con);
int anInteger = int.Parse(intSlipID_srch.Text);
cmd.Parameters.Add("@id", SqlDbType.Int).Value = anInteger;
int updated = 0;
try
{
con.Open();
updated = cmd.ExecuteNonQuery();
lblStatus.Text = updated.ToString() + " record updated.";
}
catch (Exception err)
{
lblStatus.Text = "Error saving Slip. ";
lblStatus.Text += err.Message;
}
finally
{
con.Close();
}
}
}
功能search_click按预期工作,并从数据库中检索的记录。但是当我尝试使用函数btn_update_Click更新记录时,我收到错误保存滑动错误。 'slip_id'附近语法不正确。注意为了缩小问题,我已经注释了很多代码。
我在做什么错误?
你缺少的字段=值和Where子句之间的空间。我建议使用逐字字符串而不是+ =来构建查询文本 – Steve
Steve是正确的,也可以将查询写为一个字符串以使这类问题更加明显。你可以通过在它之前加一个'@'来使它成为一个逐字串,并且允许你将字符串放在多行中。 – juharr
除了缺少空格之外,在您的注释代码中,缺少'''多个字段之间',更重要的是,使用更新值的参数(如使用WHERE'时),不要连接字符串。 – Habib