0

SQLServerクエリから適切に機能するUPDATEストアドプロシージャがあります。

GO
ALTER PROCEDURE [dbo].[UpdateMedicalCard]
@RecordingCode int,
@BeginTreatmentDate date,
@EndTreatmentDate date,
@MainDiagnosis nchar(100),
@AttendantDiagnosis nchar(100),
@TreatmentResult nchar(50)
AS
BEGIN
    UPDATE MedicalCard
    SET BeginTreatmentDate = @BeginTreatmentDate,
        EndTreatmentDate = @EndTreatmentDate,
        MainDiagnosis = @MainDiagnosis,
        AttendantDiagnosis = @AttendantDiagnosis,
        TreatmentResult = @TreatmentResult
    WHERE RecordingCode = @RecordingCode
END

しかし、Visual Studioからこのプロシージャを呼び出すと、更新されません。

SqlConnection connection = new SqlConnection();
            connection.ConnectionString = @"Data Source=.;Initial Catalog=Policlinic;Integrated Security=SSPI";

            connection.Open();

            SqlCommand myCommand = connection.CreateCommand();
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.CommandText = "UpdateMedicalCard";

            myCommand.Parameters.Add("@RecordingCode", System.Data.SqlDbType.Int);
            myCommand.Parameters["@RecordingCode"].Value = dataGridView1.CurrentRow.Cells[0].Value;

            myCommand.Parameters.Add("@BeginTreatmentDate", System.Data.SqlDbType.Date);
            myCommand.Parameters["@BeginTreatmentDate"].Value = dataGridView1.CurrentRow.Cells[3].Value;

            myCommand.Parameters.Add("@EndTreatmentDate", System.Data.SqlDbType.Date);
            myCommand.Parameters["@EndTreatmentDate"].Value = dataGridView1.CurrentRow.Cells[4].Value;

            myCommand.Parameters.Add("@MainDiagnosis", System.Data.SqlDbType.NChar);
            myCommand.Parameters["@MainDiagnosis"].Value = "qwe";

            myCommand.Parameters.Add("@AttendantDiagnosis", System.Data.SqlDbType.NChar);
            myCommand.Parameters["@AttendantDiagnosis"].Value = dataGridView1.CurrentRow.Cells[6].Value;

            myCommand.Parameters.Add("@TreatmentResult", System.Data.SqlDbType.NChar);
            myCommand.Parameters["@TreatmentResult"].Value = dataGridView1.CurrentRow.Cells[7].Value;

            var dataAdapter = new SqlDataAdapter(myCommand);
            var dataTable = new DataTable();
            dataAdapter.Update(dataTable);
        connection.Close();

私は最後の4行で間違っていると思います。助けてください。

4

1 に答える 1

2

コマンドは結果行を返さないため、DataTable または DataAdapter を使用する必要はありません。connection.ExecuteNonQuery()代わりに電話するだけです。

また、データ (具体的には日付) が既存の行と一致するかどうかを再確認することもできます。

于 2012-12-25T16:49:36.060 に答える