我有这个程序:
CREATE PROCEDURE UpdateVolunteers
@Vol_ID INT,
@Vol_Name NVARCHAR(50),
@Vol_Zone NVARCHAR(50),
@vol_street NVARCHAR(50),
@Vol_Sex INT,
@Vol_Date_of_Birth DATE,
@Vol_Home_Phone INT,
@Vol_Work_Phone INT,
@Vol_Mobile1 INT,
@Vol_Mobile2 INT,
@Vol_Email NVARCHAR(50),
@Vol_Job NVARCHAR(50),
@Vol_Affiliation NVARCHAR(50),
@vol_Education INT,
@vol_Education_Place NVARCHAR(50),
@vol_Education_Department NVARCHAR(50),
@vol_Interesting INT,
@Team_ID INT,
@vol_Notes NVARCHAR(50)
AS
IF @team_id = -1
BEGIN
SET @team_id = NULL
END
UPDATE Personal_Info
SET Vol_Name = @Vol_Name,
Vol_Zone = @Vol_Zone,
vol_street = @vol_street,
Vol_Sex = @Vol_Sex,
Vol_Date_of_Birth = @Vol_Date_of_Birth,
Vol_Home_Phone = @Vol_Home_Phone,
Vol_Work_Phone = @Vol_Work_Phone,
Vol_Mobile1 = @Vol_Mobile1,
Vol_Mobile2 = @Vol_Mobile2,
Vol_Email = @Vol_Email,
Vol_Job = @Vol_Job,
Vol_Affiliation = @Vol_Affiliation,
vol_Education = @vol_Education,
vol_Education_Place = @vol_Education_Place,
vol_Education_Department = @vol_Education_Department,
vol_Interesting = @vol_Interesting,
Team_ID = @Team_ID,
vol_Notes = @vol_Notes
WHERE Vol_ID = @Vol_ID
我通过以下代码将其调用到 C# 应用程序:
private static void UpdateVolunteer(string volID, string volName, string volZone,
string volStreet, int volSex, DateTime volBirthday, int volHomePhone,
int volWorkPhone, int volMobile1, int volMobile2, string volEmail,
string volJob, string volAffiliation, int volEducation,
string volEducationPlace, string volEducationDepartment, int volInteresting,
int Team_ID, string volNotes)
{
try
{
SqlCommand com = new SqlCommand("UpdateVolunteers", con);
com.Parameters.Add("@Vol_ID", SqlDbType.Int).Value = volID;
com.Parameters.Add("@Vol_Name", SqlDbType.NVarChar, 50).Value = volName;
com.Parameters.Add("@Vol_Zone", SqlDbType.NVarChar, 50).Value = volZone;
com.Parameters.Add("@vol_street", SqlDbType.NVarChar, 50).Value = volStreet;
com.Parameters.Add("@Vol_Sex", SqlDbType.Int).Value = volSex;
com.Parameters.Add("@Vol_Date_of_Birth", SqlDbType.DateTime).Value = volBirthday;
com.Parameters.Add("@Vol_Home_Phone", SqlDbType.Int).Value = volHomePhone;
com.Parameters.Add("@Vol_Work_Phone", SqlDbType.Int).Value = volWorkPhone;
com.Parameters.Add("@Vol_Mobile1", SqlDbType.Int).Value = volMobile1;
com.Parameters.Add("@Vol_Mobile2", SqlDbType.Int).Value = volMobile2;
com.Parameters.Add("@Vol_Email", SqlDbType.NVarChar, 50).Value = volEmail;
com.Parameters.Add("@Vol_Job", SqlDbType.NVarChar, 50).Value = volJob;
com.Parameters.Add("@Vol_Affiliation", SqlDbType.NVarChar, 50).Value = volAffiliation;
com.Parameters.Add("@vol_Education", SqlDbType.Int).Value = volEducation;
com.Parameters.Add("@vol_Education_Place", SqlDbType.NVarChar, 50).Value = volEducationPlace;
com.Parameters.Add("@vol_Education_Department", SqlDbType.NVarChar, 50).Value = volEducationDepartment;
com.Parameters.Add("@vol_Interesting", SqlDbType.Int).Value = volInteresting;
com.Parameters.Add("@vol_Notes", SqlDbType.NVarChar, 50).Value = volNotes;
com.Parameters.Add("@Team_ID", SqlDbType.Int);
com.Parameters["@Team_ID"].Value = Team_ID;
com.CommandType = System.Data.CommandType.StoredProcedure;
if (con.State != ConnectionState.Open)
con.Open();
com.ExecuteNonQuery();
con.Close();
}
catch (SqlException sqlEx)
{
if (con.State == ConnectionState.Open)
con.Close();
throw sqlEx;
}
}
C# 中的这个函数:在 Button_Click 事件中调用
public void showVolunteers(int x)
{
SqlCommand com = new SqlCommand("SELECT [Vol_ID]
,[Vol_Name]
,[Vol_Zone]
,[vol_street]
,[Vol_Sex]
,[vol_Age]
,[Vol_Date_of_Birth]
,[Vol_Home_Phone]
,[Vol_Work_Phone]
,[Vol_Mobile1]
,[Vol_Mobile2]
,[Vol_Email]
,[Vol_Job]
,[Vol_Affiliation]
,[vol_Education]
,[vol_Education_Place]
,[vol_Education_Department]
,[vol_Interesting]
,[vol_Hours]
,[vol_Notes]
,[Team_ID]
FROM [VolunteersAffairs].[dbo].[Personal_Info]", con);
SqlDataAdapter da = new SqlDataAdapter();
DataSet dats = new DataSet();
da.SelectCommand = com;
da.Fill(dats, "Personal_Info");
dataGridViewX1.DataSource = dats.Tables["Personal_Info"];
dataGridViewX1.Columns[2].DefaultCellStyle.NullValue = "Nothing";
updateComboBox.SelectedIndex = updateComboBox.Items.Count - 1;
if (x != -1)
{
dataGridViewX1.Rows[x].Selected = true;
dataGridViewX1.CurrentCell = dataGridViewX1[0, x];
}
labelItem1.Text = "Number of Volunteers = " + dataGridViewX1.Rows.Count;
int X = 0, Y = 0;
foreach (DataGridViewRow n in dataGridViewX1.Rows)
{
if (n.Cells[5].Value.ToString() == "Male")
X++;
else
Y++;
}
labelItem2.Text = " | Number of Males = " + X + " |Number of Female = " + Y;
//When I put the following as a comment the update is so fast, else very slowly.
//This operation takes the birthday of volunteers and calculate the age for every one.
//And calculate the age in runTime and the value of the column in database is null and always be.
#region the problem has been detected here
DateTime brithday;
for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
{
brithday = (DateTime)dataGridViewX1[6, i].Value;
TimeSpan age = DateTime.Today - brithday;
dataGridViewX1[7, i].Value = age.Days / 365;
}
#endregion
}
问题是当我通过 C# 的形式更新表时,代码需要很长时间(大约 1 分钟)才能执行,但是当我通过 sql 查询中的代码更新它时,它的执行速度非常快。问题出在 C# 的代码中还是在被调用的过程中?告诉我这个问题的解决方案是什么。请!!