###前提・実現したいこと
作成したプログラムの単体テストをしているのですがreadLineとdrawstringの所で意図的に例外を発生させたい
###該当のソースコード
C#
1/// <summary> 2 /// FormProductDisplayのイベントハンドラ 3 /// DBに接続して任意のファイルをINSERTする 4 /// </summary> 5 /// <param name="sender">使用しない</param> 6 /// <param name="e">使用しない</param> 7 private void FormProductDisplay_Load(object sender, EventArgs e) 8 { 9 string sqlDelete = @"DELETE FROM product_table"; 10 11 using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString)) 12 using (SqlCommand command = new SqlCommand(sqlDelete, connection)) 13 { 14 try 15 { 16 connection.Open(); 17 } 18 catch (Exception ex) 19 { 20 MessageBox.Show("データベースを開くのに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 21 System.Diagnostics.Debug.WriteLine(ex.ToString()); 22 return; 23 } 24 25 try 26 { 27 command.ExecuteNonQuery(); 28 } 29 catch (Exception ex) 30 { 31 MessageBox.Show("SQL文を実行出来ませんでした。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 32 System.Diagnostics.Debug.WriteLine(ex.ToString()); 33 return; 34 } 35 } 36 37 string filePath; 38 using (OpenFileDialog dialog = new OpenFileDialog()) 39 { 40 dialog.FileName = "default.txt"; 41 dialog.InitialDirectory = ""; 42 dialog.Title = "開くファイルを選択してください。"; 43 dialog.RestoreDirectory = true; 44 dialog.CheckFileExists = false; 45 TopMost = false; 46 47 if (dialog.ShowDialog() == DialogResult.Cancel) 48 { 49 return; 50 } 51 52 filePath = dialog.FileName; 53 } 54 55 using (SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString)) 56 { 57 string sqlInsert = @" 58INSERT INTO product_table ( 59product_id, 60basic_order_lot, 61product_code, 62chassis_number, 63assembled_ymd, 64assembled_hms, 65struct_number, 66yyyymm 67) 68VALUES ( 69@product_id, 70@basic_order_lot, 71@product_code, 72@chassis_number, 73@assembled_ymd, 74@assembled_hms, 75@struct_number, 76@yyyymm 77); 78"; 79 try 80 { 81 connection.Open(); 82 } 83 catch (Exception ex) 84 { 85 MessageBox.Show("データベースを開くのに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 86 System.Diagnostics.Debug.WriteLine(ex.ToString()); 87 return; 88 } 89 90 using (SqlCommand command = new SqlCommand(sqlInsert, connection)) 91 { 92 FileStream stream; 93 94 try 95 { 96 stream = File.Open(filePath, FileMode.Open); 97 } 98 catch (Exception ex) 99 { 100 MessageBox.Show("ファイルを開くのに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 101 System.Diagnostics.Debug.WriteLine(ex.ToString()); 102 return; 103 } 104 105 using (StreamReader read = new StreamReader(stream)) 106 { 107 while (read.EndOfStream == false) 108 { 109 string line; 110 try 111 { 112 line = read.ReadLine(); 113 } 114 catch (Exception ex) 115 { 116 MessageBox.Show("ファイルの読み込みに失敗しました。", "error", MessageBoxButtons.OK, MessageBoxIcon.Error); 117 System.Diagnostics.Debug.WriteLine(ex.ToString()); 118 return; 119 } 120 121 if (line == null) 122 { 123 break; 124 } 125 126 command.Parameters.AddWithValue("@product_id", line.Substring(0, 11)); 127 command.Parameters.AddWithValue("@basic_order_lot", line.Substring(11, 8)); 128 command.Parameters.AddWithValue("@product_code", line.Substring(19, 6)); 129 command.Parameters.AddWithValue("@chassis_number", line.Substring(25, 4)); 130 command.Parameters.AddWithValue("@assembled_ymd", line.Substring(29, 8)); 131 command.Parameters.AddWithValue("@assembled_hms", line.Substring(37, 11)); 132 command.Parameters.AddWithValue("@struct_number", line.Substring(48, 7)); 133 command.Parameters.AddWithValue("@yyyymm", line.Substring(55, 6)); 134 135 try 136 { 137 command.ExecuteNonQuery(); 138 } 139 catch (Exception ex) 140 { 141 MessageBox.Show("データの追加に失敗しました。", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 142 System.Diagnostics.Debug.WriteLine(ex.ToString()); 143 } 144 command.Parameters.Clear(); 145 } 146 read.Close(); 147 } 148 } 149 } 150 } 151 . 152 . 153 . 154 /// <summary> 155 /// 1ページ印刷するイベントハンドラ 156 /// 1ページ分の印刷設定 157 /// </summary> 158 /// <param name="sender">使用しない</param> 159 /// <param name="e">使用する</param> 160 private void PrintPage(object sender, PrintPageEventArgs e) 161 { 162 int x, y = 20; 163 e.HasMorePages = true; 164 Font font = new Font("MS ゴシック", 12); 165 var productList = productDataBindingSource.DataSource as List<ProductData>; 166 167 if (productList == null) 168 { 169 e.HasMorePages = false; 170 return; 171 } 172 173 for (int i = ((page - 1) * 50); 174 (i < productList.Count && (i < (page * 50))); 175 i++) 176 { 177 ProductData data = productList[i]; 178 try 179 { 180 if (data == null) 181 { 182 return; 183 } 184 185 x = 30; 186 e.Graphics.DrawString(data.ProductId, font, Brushes.Black, x, y); 187 x = x + 80; 188 e.Graphics.DrawString(data.BasicOrderLot, font, Brushes.Black, x, y); 189 x = x + 90; 190 e.Graphics.DrawString(data.ProductCode, font, Brushes.Black, x, y); 191 x = x + 80; 192 e.Graphics.DrawString(data.ChassisNumber, font, Brushes.Black, x, y); 193 x = x + 60; 194 e.Graphics.DrawString(data.AssembledYmd, font, Brushes.Black, x, y); 195 x = x + 150; 196 e.Graphics.DrawString(data.AssembledHms, font, Brushes.Black, x, y); 197 x = x + 100; 198 e.Graphics.DrawString(data.StructNumber, font, Brushes.Black, x, y); 199 x = x + 100; 200 e.Graphics.DrawString(data.YyyyMm, font, Brushes.Black, x, y); 201 y = y + 20; 202 } 203 catch (Exception ex) 204 { 205 MessageBox.Show("描画出来ませんでした。", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information); 206 System.Diagnostics.Debug.WriteLine(ex.ToString()); 207 e.HasMorePages = false; 208 } 209 } 210 211 e.Graphics.DrawString(String.Format("{0}ページ", page), font, Brushes.Black, 365, 1110); 212 page++; 213 214 if (productList.Count <= (page - 1) * 50) 215 { 216 e.HasMorePages = false; 217 page = 1; 218 return; 219 } 220 }
###試したこと
開いたファイルを消したら読み込みができないんじゃないかと思い実践しようと思ったのですが他のプログラムで開いていたら消去が出来なかったです。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/09/15 07:01
退会済みユーザー
2017/09/15 07:04
2017/09/15 07:07
退会済みユーザー
2017/09/15 07:33
2017/09/15 07:44
退会済みユーザー
2017/09/15 07:52
2017/09/15 08:05
退会済みユーザー
2017/09/15 08:16
2017/09/15 08:28