0

我有一个 UPDATE 存储过程,它适用于 SQL Server 查询:

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 回答