這是我使用的代碼,它會在第3個命令中彈出一個Exception .CommandText賦值,但它與我在第2個命令中使用的方法相同.CommandText賦值,輸入字符串在SqlCommand中的格式不正確
SqlCommand command = conn.CreateCommand();
conn.Open();
//1st
command.CommandText = query;
SqlDataReader reader = command.ExecuteReader();
ArrayList alMainGrid = new ArrayList();
while (reader.Read())
{
SupportTable table = new SupportTable();
table.LaySheetNo = reader.GetValue(0).ToString();
table.PlnLayStartTime = reader.GetDateTime(1).ToString();
table.PlnLayEndTime = reader.GetValue(2).ToString();
table.LayTableId = reader.GetValue(3).ToString();// reader.GetValue(3).ToString();
table.LayTeamId = reader.GetValue(4).ToString();
alMainGrid.Add(table);
}
reader.Close();
foreach (SupportTable table in alMainGrid)
{
//2nd
command.CommandText = String.Format("SELECT CTDesc FROM CutTable WHERE CTId ={0}", int.Parse(table.LayTableId));
string tableDesc = (string)command.ExecuteScalar();
table.LayTeamId = tableDesc;
//3rd-In this command.CommandText
command.CommandText = String.Format("SELECT TeamDesc FROM Team WHERE TeamId ={0}", int.Parse(table.LayTeamId));
string teamDesc = (string)command.ExecuteScalar();
table.LayTeamId = teamDesc;
}
dgvMain.DataSource = alMainGrid;
什麼是例外? –
看起來像'table.LayTeamId'是一個空字符串或包含非數字字符。 – ekad
順便說一句,你不應該連接字符串來形成你的查詢。這是讓自己容易受到SQL注入攻擊的好方法 –