-2

我不明白你收到这种错误...下面你可以看到更新 sqlquery。

protected void btnupdate_Click(object sender, EventArgs e)
{
    string pID = Convert.ToString(Session["PatientId"]);
    if (!string.IsNullOrEmpty(pID))
    {
        int patientID = Convert.ToInt32(pID);

        SqlConnection connew = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString);



            SqlCommand cmd = new SqlCommand("Upadate [dbo].[PatientDetails] set [title] = @pttit, [sex] = @ptgen, [lastname] = @ptlastnm, " +
                    " [birthday] = @ptbirth, [firstname] = @ptfirstnm, [middlename] = @ptmiddlenm, [remarkline] = @ptremarkln, [remarks] = @ptremark " +
                                                                        "where [PatientId] = '"+pID+"'", connew);



            cmd.Parameters.AddWithValue("@pttit", txtpttitle.Text);
            cmd.Parameters.AddWithValue("@ptgen", txtgender.Text);
            cmd.Parameters.AddWithValue("@ptlastnm", txtptlastnm.Text);
            cmd.Parameters.AddWithValue("@ptbirth", txtptbirthday.Text);
            cmd.Parameters.AddWithValue("@ptfirstnm", txtptfirstnm.Text);
            cmd.Parameters.AddWithValue("@ptmiddlenm", txtptmiddlenm.Text);
            cmd.Parameters.AddWithValue("@ptremarkln", txtptremarkline.Text);
            cmd.Parameters.AddWithValue("@ptremark", txtremarks.Text);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = connew;

            if (connew.State == ConnectionState.Closed)
            {
                connew.Open();
            }
            try
            {                        
                //rowsaffected = cmd.ExecuteNonQuery();
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                Response.Write("Error Occured: " + ex.Message.ToString());
            }
            finally
            {
                connew.Close();
                cmd.Dispose();
            }
        }
}

当我调试代码时...它进入 catch 并显示错误消息:'.' 附近的语法不正确......任何人都可以知道我错在哪里......如果有人更正我的代码,那就太好了这将更新数据库中的表。

非常感谢。

4

4 回答 4

2

你拼写错误更新...

SqlCommand cmd = new SqlCommand("Upadate [dbo].[PatientDetails] set [title] = @pttit, [sex] = @ptgen, [lastname] = @ptlastnm, " +
                 " [birthday] = @ptbirth, [firstname] = @ptfirstnm, [middlename] = @ptmiddlenm, [remarkline] = @ptremarkln, [remarks] = @ptremark " +
                                                                        "where [PatientId] = '"+pID+"'", connew);
于 2013-10-16T10:41:17.943 回答
1

好吧,首先upadate不是 SQL 命令。

现在您似乎对 SQL 命令参数的使用有了一些了解。那你到底为什么舍弃了真义之道,在最后串联了一个参数?!

where [PatientId] = '"+pID+"'
于 2013-10-16T10:39:48.673 回答
0

试试这个代码,在命令参数中添加 pID,然后在查询中调用 cmd 参数名称

  SqlCommand cmd = new SqlCommand(@"Update [dbo].[PatientDetails] set [title] = @pttit, [sex] =

 @ptgen, [lastname] = @ptlastnm,[birthday] = @ptbirth, [firstname] = @ptfirstnm, [middlename] = 

@ptmiddlenm, [remarkline] = @ptremarkln, [remarks] = @ptremark where [PatientId] = @pID", connew);



            cmd.Parameters.AddWithValue("@pttit", txtpttitle.Text);
            cmd.Parameters.AddWithValue("@ptgen", txtgender.Text);
            cmd.Parameters.AddWithValue("@ptlastnm", txtptlastnm.Text);
            cmd.Parameters.AddWithValue("@ptbirth", txtptbirthday.Text);
            cmd.Parameters.AddWithValue("@ptfirstnm", txtptfirstnm.Text);
            cmd.Parameters.AddWithValue("@ptmiddlenm", txtptmiddlenm.Text);
            cmd.Parameters.AddWithValue("@ptremarkln", txtptremarkline.Text);
            cmd.Parameters.AddWithValue("@ptremark", txtremarks.Text);
            cmd.Parameters.AddWithValue("@pID",pID);
于 2013-10-16T10:55:34.607 回答
0
 using (SqlConnection con = new SqlConnection())
        {
            string title = "niraj";
            SqlCommand cmd = new SqlCommand("update [dbo].[PatientDetails] set title=@title", con);
            cmd.Parameters.AddWithValue("@title", title.ToString());
            cmd.ExecuteNonQuery();
            con.Close();
        }
于 2013-10-16T11:07:54.503 回答