-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();
            }
        }
}

コードをデバッグすると...キャッチに入り、エラーメッセージが表示されます:Incorrect syntax near '.'....誰でも私が間違っている場所を知ることができます....誰かが私のコードを修正してくれたら素晴らしいでしょう.これにより、データベースのテーブルが更新されます。

どうもありがとう。

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 parm 名を呼び出します。

  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 に答える