質問編集履歴

1

コードを修正致しました。

2020/08/27 04:40

投稿

fight51722
fight51722

スコア9

test CHANGED
File without changes
test CHANGED
@@ -24,6 +24,10 @@
24
24
 
25
25
  下記に途中段階のコードを記載します。
26
26
 
27
+ このコードを実行すると、「ユーザーはログインできませんでした。」
28
+
29
+ というエラーメッセージが出ます。
30
+
27
31
 
28
32
 
29
33
  ```ここに言語を入力
@@ -72,89 +76,103 @@
72
76
 
73
77
  }
74
78
 
75
-
79
+ //----------------------------------------------------------------------------------------------------------
76
80
 
77
81
  private void button1_Click(object sender, EventArgs e)
78
82
 
79
83
  {
80
84
 
85
+ SqlConnection conn = new SqlConnection();
86
+
87
+ conn.ConnectionString =
88
+
81
- using (var reader = new StreamReader(@"C:\Users\yuhor\OneDrive\デクトップ\拠点.csv"))
89
+ @"Data Source=DESKTOP-U6R2QM8\SQLEXPRESS;Initial Catalog=マタ機能;";
90
+
91
+ try
82
92
 
83
93
  {
84
94
 
95
+
96
+
97
+ conn.Open();
98
+
99
+
100
+
101
+ SqlCommand command = conn.CreateCommand();
102
+
103
+ SqlTransaction transaction = conn.BeginTransaction();
104
+
105
+
106
+
107
+ command.Connection = conn;
108
+
109
+ command.Transaction = transaction;
110
+
111
+
112
+
113
+ try
114
+
115
+ {
116
+
117
+
118
+
119
+ StreamReader sr = new StreamReader(@"C:\Users\yuhor\OneDrive\デスクトップ\拠点.csv");
120
+
121
+ sr.ReadLine();
122
+
123
+ while (sr.Peek() > -1)
124
+
125
+ {
126
+
85
- string file = reader.ReadToEnd();
127
+ string line = sr.ReadLine();
128
+
129
+ command.CommandType = CommandType.StoredProcedure;
130
+
131
+ command.CommandText = "Insert into 拠点 (コード, 拠点)";
132
+
133
+ command.ExecuteNonQuery();
134
+
135
+ }
136
+
137
+ transaction.Commit();
138
+
139
+ sr.Close();
140
+
141
+
142
+
143
+ }
144
+
145
+ catch
146
+
147
+ {
148
+
149
+ transaction.Rollback();
150
+
151
+ }
86
152
 
87
153
  }
88
154
 
89
-
90
-
91
-
92
-
93
- SqlConnection conn = new SqlConnection();
94
-
95
- conn.ConnectionString =
96
-
97
- @"Data Source=DESKTOP-U6R2QM8\SQLEXPRESS;Initial Catalog=マスタ機能;";
98
-
99
-
100
-
101
- conn.Open();
102
-
103
-
104
-
105
- SqlCommand command = conn.CreateCommand();
106
-
107
- SqlTransaction transaction =conn.BeginTransaction();
108
-
109
-
110
-
111
- command.Connection = conn;
155
+ catch(Exception ex)
112
-
113
- command.Transaction = transaction;
114
-
115
-
116
-
117
- try
118
156
 
119
157
  {
120
158
 
121
- command.CommandText =
122
-
123
- "Insert into 拠点 (コード, 拠点)";
159
+ MessageBox.Show(ex.Message, "データベース接続エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
124
-
125
- command.ExecuteNonQuery();
126
-
127
-
128
-
129
- transaction.Commit();
130
-
131
-
132
160
 
133
161
  }
134
162
 
135
- catch(Exception)
163
+ finally
136
-
164
+
137
- {
165
+ {
138
-
166
+
139
- transaction.Rollback();
167
+ conn.Close();
168
+
169
+ }
140
170
 
141
171
 
142
172
 
143
- }
173
+ }
144
-
145
- finally
174
+
146
-
147
- {
175
+
148
-
149
- conn.Close();
150
-
151
- }
152
-
153
-
154
-
155
- }
156
-
157
-
158
176
 
159
177
  }
160
178